Skip to content

Commit

Permalink
Add a failure callback in the fallback mode if unable to write
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Aug 18, 2021
1 parent 0e34e15 commit 35b9c86
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/examples/bench/main.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/indexeddb/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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);
Expand Down
34 changes: 12 additions & 22 deletions src/indexeddb/file-ops-fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
};
Expand All @@ -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
Expand All @@ -108,35 +103,30 @@ 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;
});
}
}

export class FileOpsFallback {
constructor(filename) {
constructor(filename, onFallbackFailure) {
this.filename = filename;
this.dbName = this.filename.replace(/\//g, '-');
this.cachedFirstBlock = null;
Expand All @@ -145,7 +135,7 @@ export class FileOpsFallback {
this.lockType = 0;
this.transferBlockOwnership = false;

this.persistance = new Persistance();
this.persistance = new Persistance(onFallbackFailure);
}

async readIfFallback() {
Expand Down

0 comments on commit 35b9c86

Please sign in to comment.