diff --git a/package.json b/package.json index 10f057a..ffe086d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "absurd-sql", - "version": "0.0.51", + "version": "0.0.52", "main": "./dist/index.js", "scripts": { "build": "rm -rf dist && rollup -c rollup.config.js", diff --git a/src/examples/bench/main.worker.js b/src/examples/bench/main.worker.js index 5b5b955..f0680a1 100644 --- a/src/examples/bench/main.worker.js +++ b/src/examples/bench/main.worker.js @@ -14,7 +14,9 @@ let recordProfile = false; let useRawIDB = false; let memoryBackend = new MemoryBackend({}); -let idbBackend = new IndexedDBBackend(); +let idbBackend = new IndexedDBBackend(() => { + console.error('Unable to write!'); +}); let sqlFS; // Helper methods diff --git a/src/indexeddb/backend.js b/src/indexeddb/backend.js index eaf4d7b..d38f6aa 100644 --- a/src/indexeddb/backend.js +++ b/src/indexeddb/backend.js @@ -5,6 +5,10 @@ import { FileOps } from './file-ops'; import { FileOpsFallback } from './file-ops-fallback'; export default class IndexedDBBackend { + constructor(onFallbackFailure) { + this.onFallbackFailure = onFallbackFailure; + } + createFile(filename) { let ops; if (typeof SharedArrayBuffer !== 'undefined') { @@ -14,7 +18,7 @@ export default class IndexedDBBackend { // SharedArrayBuffer is not supported. Use the fallback methods // which provide a somewhat working version, but doesn't // support mutations across connections (tabs) - ops = new FileOpsFallback(filename); + ops = new FileOpsFallback(filename, this.onFallbackFailure); } let file = new File(filename, ops); diff --git a/src/indexeddb/file-ops-fallback.js b/src/indexeddb/file-ops-fallback.js index e46957a..5b1b1fb 100644 --- a/src/indexeddb/file-ops-fallback.js +++ b/src/indexeddb/file-ops-fallback.js @@ -37,8 +37,10 @@ async function openDb(name) { // happen async; the args to `write` must be closed over so they don't // change class Persistance { - constructor() { + constructor(onFallbackFailure) { this._openDb = null; + this.hasAlertedFailure = false; + this.onFallbackFailure = onFallbackFailure; } async getDb() { @@ -79,15 +81,9 @@ class Persistance { req.onsuccess = e => { let cursor = e.target.result; if (cursor) { - console.log('reading', cursor.key); blocks.set(cursor.key, cursor.value); cursor.continue(); } else { - console.log( - 'read all', - blocks.get(-1), - new Uint8Array(blocks.get(0)) - ); resolve(blocks); } }; @@ -96,7 +92,6 @@ class Persistance { async write(writes, cachedFirstBlock, hasLocked) { let db = await this.getDb(this.dbName); - console.log('fallback: writing'); // We need grab a readwrite lock on the db, and then read to check // to make sure we can write to it @@ -108,27 +103,22 @@ class Persistance { req.onsuccess = e => { if (hasLocked) { if (!isSafeToWrite(req.result, cachedFirstBlock)) { - // TODO: We need to send a message to users somehow - console.log("OH NO WE CAN'T WRITE"); - reject('screwed'); + if (this.onFallbackFailure && !this.hasAlertedFailure) { + this.hasAlertedFailure = true; + this.onFallbackFailure(); + } + reject(new Error('Fallback mode unable to write file changes')); return; } } // Flush all the writes - console.log('flushing writes', writes.length); for (let write of writes) { store.put(write.value, write.key); } - trans.onsuccess = () => { - console.log('done writing'); - resolve(); - }; - trans.onerror = () => { - console.log('Flushing writes failed'); - reject(); - }; + trans.onsuccess = () => resolve(); + trans.onerror = () => reject(); }; req.onerror = reject; }); @@ -136,7 +126,7 @@ class Persistance { } export class FileOpsFallback { - constructor(filename) { + constructor(filename, onFallbackFailure) { this.filename = filename; this.dbName = this.filename.replace(/\//g, '-'); this.cachedFirstBlock = null; @@ -145,7 +135,7 @@ export class FileOpsFallback { this.lockType = 0; this.transferBlockOwnership = false; - this.persistance = new Persistance(); + this.persistance = new Persistance(onFallbackFailure); } async readIfFallback() {