Sanity-check decaffeinate app.DB

pull/1441/head
Simon Legner 1 year ago
parent dc3dd67bca
commit 8103c2899d

@ -1,22 +1,6 @@
// TODO: This file was created by bulk-decaffeinate. app.DB = class DB {
// Sanity-check the conversion and remove this comment. static NAME = "docs";
/* static VERSION = 15;
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
(function () {
let NAME = undefined;
let VERSION = undefined;
app.DB = class DB {
static initClass() {
NAME = "docs";
VERSION = 15;
}
constructor() { constructor() {
this.versionMultipler = $.isIE() ? 1e5 : 1e9; this.versionMultipler = $.isIE() ? 1e5 : 1e9;
@ -38,8 +22,8 @@
try { try {
this.open = true; this.open = true;
const req = indexedDB.open( const req = indexedDB.open(
NAME, DB.NAME,
VERSION * this.versionMultipler + this.userVersion(), DB.VERSION * this.versionMultipler + this.userVersion(),
); );
req.onsuccess = (event) => this.onOpenSuccess(event); req.onsuccess = (event) => this.onOpenSuccess(event);
req.onerror = (event) => this.onOpenError(event); req.onerror = (event) => this.onOpenError(event);
@ -123,7 +107,7 @@
} }
onVersionError() { onVersionError() {
const req = indexedDB.open(NAME); const req = indexedDB.open(DB.NAME);
req.onsuccess = (event) => { req.onsuccess = (event) => {
return this.handleVersionMismatch(event.target.result.version); return this.handleVersionMismatch(event.target.result.version);
}; };
@ -134,10 +118,10 @@
} }
handleVersionMismatch(actualVersion) { handleVersionMismatch(actualVersion) {
if (Math.floor(actualVersion / this.versionMultipler) !== VERSION) { if (Math.floor(actualVersion / this.versionMultipler) !== DB.VERSION) {
this.fail("version"); this.fail("version");
} else { } else {
this.setUserVersion(actualVersion - VERSION * this.versionMultipler); this.setUserVersion(actualVersion - DB.VERSION * this.versionMultipler);
this.db(); this.db();
} }
} }
@ -179,7 +163,7 @@
} catch (error) {} } catch (error) {}
} }
for (var doc of Array.from(app.docs.all())) { for (var doc of app.docs.all()) {
if (!$.arrayDelete(objectStoreNames, doc.slug)) { if (!$.arrayDelete(objectStoreNames, doc.slug)) {
try { try {
db.createObjectStore(doc.slug); db.createObjectStore(doc.slug);
@ -187,7 +171,7 @@
} }
} }
for (var name of Array.from(objectStoreNames)) { for (var name of objectStoreNames) {
try { try {
db.deleteObjectStore(name); db.deleteObjectStore(name);
} catch (error2) {} } catch (error2) {}
@ -217,11 +201,7 @@
}; };
txn.onerror = (event) => { txn.onerror = (event) => {
event.preventDefault(); event.preventDefault();
if ( if (txn.error?.name === "NotFoundError" && _retry) {
(txn.error != null ? txn.error.name : undefined) ===
"NotFoundError" &&
_retry
) {
this.migrate(); this.migrate();
setTimeout(() => { setTimeout(() => {
return this.store(doc, data, onSuccess, onError, false); return this.store(doc, data, onSuccess, onError, false);
@ -266,11 +246,7 @@
}; };
txn.onerror = function (event) { txn.onerror = function (event) {
event.preventDefault(); event.preventDefault();
if ( if (txn.error?.name === "NotFoundError" && _retry) {
(txn.error != null ? txn.error.name : undefined) ===
"NotFoundError" &&
_retry
) {
this.migrate(); this.migrate();
setTimeout(() => { setTimeout(() => {
return this.unstore(doc, onSuccess, onError, false); return this.unstore(doc, onSuccess, onError, false);
@ -289,8 +265,8 @@
} }
version(doc, fn) { version(doc, fn) {
let version; let version = this.cachedVersion(doc);
if ((version = this.cachedVersion(doc)) != null) { if (version != null) {
fn(version); fn(version);
return; return;
} }
@ -326,8 +302,8 @@
} }
versions(docs, fn) { versions(docs, fn) {
let versions; let versions = this.cachedVersions(docs);
if ((versions = this.cachedVersions(docs))) { if (versions) {
fn(versions); fn(versions);
return; return;
} }
@ -366,7 +342,7 @@
return; return;
} }
const result = {}; const result = {};
for (var doc of Array.from(docs)) { for (var doc of docs) {
result[doc.slug] = this.cachedVersion(doc); result[doc.slug] = this.cachedVersion(doc);
} }
return result; return result;
@ -374,7 +350,9 @@
load(entry, onSuccess, onError) { load(entry, onSuccess, onError) {
if (this.shouldLoadWithIDB(entry)) { if (this.shouldLoadWithIDB(entry)) {
return this.loadWithIDB(entry, onSuccess, () => this.loadWithXHR(entry, onSuccess, onError)); return this.loadWithIDB(entry, onSuccess, () =>
this.loadWithXHR(entry, onSuccess, onError),
);
} else { } else {
return this.loadWithXHR(entry, onSuccess, onError); return this.loadWithXHR(entry, onSuccess, onError);
} }
@ -470,13 +448,13 @@
return; return;
} }
for (slug of Array.from(docs)) { for (slug of docs) {
if (!app.docs.findBy("slug", slug)) { if (!app.docs.findBy("slug", slug)) {
this.corruptedDocs.push(slug); this.corruptedDocs.push(slug);
} }
} }
for (slug of Array.from(this.corruptedDocs)) { for (slug of this.corruptedDocs) {
$.arrayDelete(docs, slug); $.arrayDelete(docs, slug);
} }
@ -496,7 +474,7 @@
} }
}; };
for (var doc of Array.from(docs)) { for (var doc of docs) {
txn.objectStore(doc).get("index").onsuccess = (event) => { txn.objectStore(doc).get("index").onsuccess = (event) => {
if (!event.target.result) { if (!event.target.result) {
this.corruptedDocs.push(event.target.source.name); this.corruptedDocs.push(event.target.source.name);
@ -528,8 +506,7 @@
shouldLoadWithIDB(entry) { shouldLoadWithIDB(entry) {
return ( return (
this.useIndexedDB && this.useIndexedDB && (!this.cachedDocs || this.cachedDocs[entry.doc.slug])
(!this.cachedDocs || this.cachedDocs[entry.doc.slug])
); );
} }
@ -551,9 +528,7 @@
reset() { reset() {
try { try {
if (typeof indexedDB !== "undefined" && indexedDB !== null) { indexedDB?.deleteDatabase(DB.NAME);
indexedDB.deleteDatabase(NAME);
}
} catch (error) {} } catch (error) {}
} }
@ -581,7 +556,4 @@
userVersion() { userVersion() {
return app.settings.get("schema"); return app.settings.get("schema");
} }
}; };
app.DB.initClass();
return app.DB;
})();

Loading…
Cancel
Save