Skip to content

Commit

Permalink
Merge pull request mozilla#665 from mykmelez/store-sync
Browse files Browse the repository at this point in the history
add storeSync function to ensure data synced to IDB store
  • Loading branch information
Marco committed Dec 2, 2014
2 parents 067e0a8 + bbcaf1d commit d5076a7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
34 changes: 34 additions & 0 deletions libs/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ var fs = (function() {
window.setZeroTimeout(function() { cb(value) });
} else {
var transaction = this.db.transaction(Store.DBSTORENAME, "readonly");
if (DEBUG_FS) { console.log("get " + key + " initiated"); }
var objectStore = transaction.objectStore(Store.DBSTORENAME);
var req = objectStore.get(key);
req.onerror = function() {
console.error("Error getting " + key + ": " + req.error.name);
};
transaction.oncomplete = (function() {
if (DEBUG_FS) { console.log("get " + key + " completed"); }
var value = req.result;
if (value === undefined) {
value = null;
Expand All @@ -52,33 +54,60 @@ var fs = (function() {
this.map.set(key, value);

var transaction = this.db.transaction(Store.DBSTORENAME, "readwrite");
if (DEBUG_FS) { console.log("put " + key + " initiated"); }
var objectStore = transaction.objectStore(Store.DBSTORENAME);
var req = objectStore.put(value, key);
req.onerror = function() {
console.error("Error putting " + key + ": " + req.error.name);
};
transaction.oncomplete = function() {
if (DEBUG_FS) { console.log("put " + key + " completed"); }
};
};

Store.prototype.removeItem = function(key) {
this.map.delete(key);

var transaction = this.db.transaction(Store.DBSTORENAME, "readwrite");
if (DEBUG_FS) { console.log("delete " + key + " initiated"); }
var objectStore = transaction.objectStore(Store.DBSTORENAME);
var req = objectStore.delete(key);
req.onerror = function() {
console.error("Error deleting " + key + ": " + req.error.name);
};
transaction.oncomplete = function() {
if (DEBUG_FS) { console.log("delete " + key + " completed"); }
};
};

Store.prototype.clear = function() {
this.map.clear();

var transaction = this.db.transaction(Store.DBSTORENAME, "readwrite");
if (DEBUG_FS) { console.log("clear initiated"); }
var objectStore = transaction.objectStore(Store.DBSTORENAME);
var req = objectStore.clear();
req.onerror = function() {
console.error("Error clearing store: " + req.error.name);
};
transaction.oncomplete = function() {
if (DEBUG_FS) { console.log("clear completed"); }
};
}

Store.prototype.sync = function(cb) {
// Process a readwrite transaction to ensure previous writes have completed,
// so we leave the datastore in a consistent state. This is a bit hacky;
// we should instead monitor ongoing transactions and call our callback
// once they've all completed.
var transaction = this.db.transaction(Store.DBSTORENAME, "readwrite");
if (DEBUG_FS) { console.log("get \"\" initiated"); }
var objectStore = transaction.objectStore(Store.DBSTORENAME);
objectStore.get("");
transaction.oncomplete = function() {
if (DEBUG_FS) { console.log("get \"\" completed"); }
cb();
};
}

var store = new Store();
Expand Down Expand Up @@ -560,6 +589,10 @@ var fs = (function() {
initRootDir(cb || function() {});
}

function storeSync(cb) {
store.sync(cb);
}

return {
dirname: dirname,
init: init,
Expand All @@ -583,5 +616,6 @@ var fs = (function() {
rename: rename,
stat: stat,
clear: clear,
storeSync: storeSync,
};
})();
2 changes: 1 addition & 1 deletion tests/automation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ casper.test.begin("unit tests", 7 + gfxTests.length, function(test) {
casper
.thenOpen("http://localhost:8000/tests/fstests.html")
.waitForText("DONE", function() {
test.assertTextExists("DONE: 126 PASS, 0 FAIL", "run fs.js unit tests");
test.assertTextExists("DONE: 127 PASS, 0 FAIL", "run fs.js unit tests");
});

casper
Expand Down
10 changes: 10 additions & 0 deletions tests/fstests.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,16 @@ tests.push(function() {
});
});

tests.push(function() {
fs.storeSync(function() {
// There's nothing we can check, since the sync status of the store
// is private to the fs module, but we have at least confirmed that the call
// resulted in the callback being called.
ok(true, "storeSync callback called");
next();
});
});

fs.init(function() {
fs.clear(function() {
next();
Expand Down

0 comments on commit d5076a7

Please sign in to comment.