Comparing version 1.0.1 to 1.1.0
@@ -8,6 +8,4 @@ 'use strict'; | ||
function promisifyRequest(obj, method, args) { | ||
var request; | ||
var p = new Promise(function(resolve, reject) { | ||
request = obj[method].apply(obj, args) | ||
function promisifyRequest(request) { | ||
return new Promise(function(resolve, reject) { | ||
request.onsuccess = function() { | ||
@@ -21,3 +19,11 @@ resolve(request.result); | ||
}); | ||
} | ||
function promisifyRequestCall(obj, method, args) { | ||
var request; | ||
var p = new Promise(function(resolve, reject) { | ||
request = obj[method].apply(obj, args); | ||
promisifyRequest(request).then(resolve, reject); | ||
}); | ||
p.request = request; | ||
@@ -27,4 +33,4 @@ return p; | ||
function promisifyCursorRequest(obj, method, args) { | ||
var p = promisifyRequest(obj, method, args); | ||
function promisifyCursorRequestCall(obj, method, args) { | ||
var p = promisifyRequestCall(obj, method, args); | ||
return p.then(function(value) { | ||
@@ -50,3 +56,3 @@ if (!value) return; | ||
ProxyClass.prototype[prop] = function() { | ||
return promisifyRequest(this[targetProp], prop, arguments); | ||
return promisifyRequestCall(this[targetProp], prop, arguments); | ||
}; | ||
@@ -69,3 +75,3 @@ }); | ||
ProxyClass.prototype[prop] = function() { | ||
return promisifyCursorRequest(this[targetProp], prop, arguments); | ||
return promisifyCursorRequestCall(this[targetProp], prop, arguments); | ||
}; | ||
@@ -122,10 +128,8 @@ }); | ||
var args = arguments; | ||
return new Promise(function(resolve, reject) { | ||
return Promise.resolve().then(function() { | ||
cursor._cursor[methodName].apply(cursor._cursor, args); | ||
cursor._request.onsuccess = function() { | ||
resolve(new Cursor(cursor._cursor), cursor._request); | ||
}; | ||
cursor._request.onerror = function() { | ||
reject(cursor._request.error); | ||
}; | ||
return promisifyRequest(cursor._request).then(function(value) { | ||
if (!value) return; | ||
return new Cursor(value, cursor._request); | ||
}); | ||
}); | ||
@@ -253,5 +257,30 @@ }; | ||
// polyfill getAll | ||
[Index, ObjectStore].forEach(function(Constructor) { | ||
if (Constructor.prototype.getAll) return; | ||
Constructor.prototype.getAll = function(query, count) { | ||
var instance = this; | ||
var items = []; | ||
return new Promise(function(resolve) { | ||
instance.iterateCursor(query, function(cursor) { | ||
if (!cursor) { | ||
resolve(items); | ||
return; | ||
} | ||
items.push(cursor.value); | ||
if (count !== undefined && items.length == count) { | ||
resolve(items); | ||
return; | ||
} | ||
cursor.continue(); | ||
}); | ||
}); | ||
}; | ||
}); | ||
var exp = { | ||
open: function(name, version, upgradeCallback) { | ||
var p = promisifyRequest(indexedDB, 'open', [name, version]); | ||
var p = promisifyRequestCall(indexedDB, 'open', [name, version]); | ||
var request = p.request; | ||
@@ -270,3 +299,3 @@ | ||
delete: function(name) { | ||
return promisifyRequest(indexedDB, 'deleteDatabase', [name]); | ||
return promisifyRequestCall(indexedDB, 'deleteDatabase', [name]); | ||
} | ||
@@ -273,0 +302,0 @@ }; |
{ | ||
"name": "idb", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "IndexedDB but with promises", | ||
@@ -5,0 +5,0 @@ "main": "lib/idb.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21983
432