This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62f0712
commit a956b1c
Showing
4 changed files
with
67 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Sample, assuming continue() returns Promise<IDBCursor?> | ||
|
||
// Without the waitUntil() calls the tx will commit eagerly. | ||
|
||
tx.waitUntil(store.openCursor(range).then(iterate)); | ||
|
||
function iterate(cursor) { | ||
if (!cursor) | ||
return; | ||
|
||
console.log(cursor.key, cursor.primaryKey, cursor.value); | ||
tx.waitUntil(cursor.continue().then(iterate)); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// If you want explicit control over the lifetime of the transaction: | ||
|
||
IDBDatabase.prototype.explicitCommitTransaction = function() { | ||
var resolveP; | ||
var p = new Promise(function(resolve) { | ||
resolveP = resolve; | ||
}); | ||
var tx = this.transaction.apply(this, arguments); | ||
tx.commit = function() { | ||
resolveP(); | ||
}; | ||
tx.waitUntil(p); | ||
return tx; | ||
}; | ||
|
||
var tx = db.explicitCommitTransaction('store', 'readwrite'); | ||
// tx will wait indefinitely until the following call is made: | ||
tx.commit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Minimal async key/value store. For simplicity, it doesn't keep a connection open. | ||
|
||
function SimpleStorage(name) { | ||
this.name = name; | ||
} | ||
|
||
SimpleStorage.prototype = { | ||
_open: function() { | ||
var r = indexedDB.open(this.name); | ||
r.upgradeneeded = function(e) { e.target.result.createObjectStore('store'); }; | ||
return r.promise(); | ||
}, | ||
|
||
get: function(key) { | ||
return this._open().then(function(db) { | ||
return db.tx('store').objectStore('store').get(key); | ||
}); | ||
}, | ||
|
||
set: function(key, value) { | ||
return this._open().then(function(db) { | ||
return db.tx('store', 'readwrite').objectStore('store').put(value, key); | ||
}); | ||
} | ||
}; |