Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Unified Diff: third_party/WebKit/LayoutTests/storage/indexeddb/resources/promise-idb.js

Issue 2449563002: [IndexedDB] Add Observer Tests (Closed)
Patch Set: Fixed errors in other tests, and marked observer tests as long Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/storage/indexeddb/resources/promise-idb.js
diff --git a/third_party/WebKit/LayoutTests/storage/indexeddb/resources/promise-idb.js b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/promise-idb.js
new file mode 100644
index 0000000000000000000000000000000000000000..091bf3e98a5d87a089ebd38ed40c285b20b328d5
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/promise-idb.js
@@ -0,0 +1,77 @@
+// Quick and dirty promise wrapper of IDB.
+
+var pdb = {
+ _transformRequestToPromise: function(thisobj, func, argArray) {
+ return new Promise(function(resolve, reject) {
+ var request = func.apply(thisobj, argArray);
+ request.onsuccess = function() {
+ resolve(request.result);
+ };
+ request.onerror = reject;
+ })
+ },
+
+ transact: function(db, objectStores, style) {
+ return Promise.resolve(db.transaction(objectStores, style));
+ },
+
+ openCursor: function(txn, indexOrObjectStore, keyrange, callback) {
+ return new Promise(function(resolve, reject) {
+ var request = indexOrObjectStore.openCursor(keyrange);
+ request.onerror = reject;
+ request.onsuccess = function() {
+ var cursor = request.result;
+ var cont = false;
+ var control = {
+ continue: function() {cont = true;}
+ };
+ if (cursor) {
+ callback(control, cursor.value);
+ if (cont) {
+ cursor.continue();
+ } else {
+ resolve(txn);
+ }
+ } else {
+ resolve(txn);
+ }
+ };
+ });
+ },
+
+ get: function(indexOrObjectStore, key) {
+ return this._transformRequestToPromise(indexOrObjectStore, indexOrObjectStore.get, [key]);
+ },
+
+ count: function(indexOrObjectStore, key) {
+ return this._transformRequestToPromise(indexOrObjectStore, indexOrObjectStore.count, [key]);
+ },
+
+ put: function(objectStore, key, value) {
+ return this._transformRequestToPromise(objectStore, objectStore.put, [key, value]);
+ },
+
+ add: function(objectStore, key, value) {
+ return this._transformRequestToPromise(objectStore, objectStore.add, [key, value]);
+ },
+
+ delete: function(objectStore, key) {
+ return this._transformRequestToPromise(objectStore, objectStore.delete, [key]);
+ },
+
+ clear: function(objectStore) {
+ return this._transformRequestToPromise(objectStore, objectStore.clear, []);
+ },
+
+ getKey: function(index, key) {
+ return this._transformRequestToPromise(index, index.getKey, [key]);
+ },
+
+ waitForTransaction: function(txn) {
+ return new Promise(function(resolve, reject) {
+ txn.oncomplete = resolve;
+ txn.onerror = reject;
+ txn.onabort = reject;
+ });
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698