Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Del now returns the number of keys requested to be deleted.

We can't rely on e.target.result to be undefined when a key wasn't deleted as the spec says that the result should always be undefined. So instead we will just return the number of keys requested to be deleted.  Fixes #6
  • Loading branch information...
commit d304268fd0e2e996f2b09d28bb5e07eea75496f8 1 parent 85931c4
@matthewpblog matthewpblog authored
Showing with 21 additions and 38 deletions.
  1. +14 −19 build/gazel.js
  2. +1 −1  build/gazel.min.js
  3. +5 −8 src/del.js
  4. +1 −10 test/del.js
View
33 build/gazel.js
@@ -404,32 +404,27 @@ Client.prototype.del = function() {
else
args.splice(args.length - 1);
- var keys = args;
+ var keys = args,
+ deleted = keys.length;
this.register('write', function(uuid, cb) {
openDatabase(function(db) {
- var tx = self.trans.pull(db, uuid, IDBTransaction.READ_WRITE);
-
- var os = tx.objectStore(gazel.osName),
- left = keys.length,
- deleted = 0;
+ var tx = self.trans.pull(db, uuid, IDBTransaction.READ_WRITE),
+ os = tx.objectStore(gazel.osName),
+ left = keys.length;
while(keys.length > 0) {
-
(function() {
-
- var key = keys.shift();
- var req = os.delete(key);
- req.onerror = self.handleError.bind(self);
- req.onsuccess = function(e) {
- left--;
-
- if(e.target.result) deleted++;
-
- if(left === 0)
- cb.call(self, deleted);
- };
+ var key = keys.shift();
+ var req = os.delete(key);
+ req.onerror = self.handleError.bind(self);
+ req.onsuccess = function(e) {
+ left--;
+
+ if(left === 0)
+ cb.call(self, deleted);
+ };
})();
}
});
View
2  build/gazel.min.js
@@ -1 +1 @@
-((function(){function g(a,b){for(b=a="";a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):"-");return b}function j(){this.chain=[],this.inMulti=!1,this.returned=[],this.trans=f.create(i,!0),this.transMap=f.create(h,!0),this.events=f.create(h,!0)}function m(b,c){if(k){b(k);return}if(l){setTimeout(function(){m(b,c)},100);return}l=!0;var d=window.indexedDB.open(a.dbName,a.version);d.onupgradeneeded=function(b){var c=b.target.result;c.objectStoreNames.contains(a.osName)||c.createObjectStore(a.osName)};var e;e=d.onsuccess=function(f){var g=f.target.result;if(g.setVersion&&Number(g.version)!==a.version){var h=g.setVersion(String(a.version));h.onsuccess=function(a){var b={};b.target={},b.target.result=a.target.result.db,d.onupgradeneeded(b),e(b);return},h.onerror=c,h.onfailure=c,h.onblocked=c;return}k=g,l=!1,b(k)},d.onerror=c}"use strict";var a=a||{},b=function(a){return typeof a!="undefined"&&a!=null},c=function(a){return!isNaN(a)&&a%1==0};window.indexedDB=window.indexedDB||window.mozIndexedDB||window.msIndexedDB||window.webkitIndexedDB||window.oIndexedDB,window.IDBTransaction=window.IDBTransaction||window.webkitIDBTransaction;var d=Array.prototype.slice,e=Array.prototype.splice,f=Object.create(null);f.create=function(a,b,c){if(typeof b!="undefined"||typeof c!="undefined"||a instanceof Array){typeof b=="boolean"&&(c=b,b=undefined),a instanceof Array||(a=[a]);var d={};for(var e in b)d[e]={value:b[e],writable:!0,enumerable:!0,configurable:!0};var f,g={},h=a.pop();do{var i=a.pop();if(i){var j={};for(var e in h)j[e]=h[e],g[e]=Object.getOwnPropertyDescriptor(j,e);h=Object.create(i,g)}}while(a.length>0);f=Object.create(h,d);if(c){var k=Array.prototype.slice.call(arguments).slice(typeof b=="undefined"?2:3);f.init.apply(f,k)}return f}return Object.create(a)},typeof exports!="undefined"&&(exports.create=f.create);var h=f.create(Object.prototype,{init:function(){return this.items={},this},prop:function(a){return":"+a},get:function(a,b){var c=this.prop(a),d=this.items;return d.hasOwnProperty(c)?d[c]:b},set:function(a,b){var c=this.prop(a);return this.items[c]=b,b},count:function(){return Object.keys(this.items).length},has:function(a){var b=this.prop(a);return this.items.hasOwnProperty(b)},del:function(a){var b=this.prop(a),c=this.items;c.hasOwnProperty(b)&&delete c[b]},keys:function(){return Object.keys(this.items).map(function(a){return a.substring(1)})}}),i=f.create(h,{add:function(){var a=g();return this.set(a,undefined),a},abortAll:function(){var a=this,b=a.keys();b.forEach(function(b){var c=a.get(b);c&&c.abort(),a.del(b)})},pull:function(b,c,d){var e=this.get(c);return e||(e=b.transaction([a.osName],d),e.onerror=onerror,this.set(c,e)),e}});j.prototype={register:function(a,b,c){if(this.inMulti){var e=this.transMap.get(a);e||(e=this.trans.add(),this.transMap.set(a,e)),this.chain.push({uuid:e,action:b});return}var f=this,e=f.trans.add();b(e,function(){var a=d.call(arguments);f.trans.del(e),(c||function(){}).apply(null,a)})},flush:function(){var a=d.call(arguments)||[];this.returned.push(a);if(this.chain.length===0){this.complete();return}var b=this.chain.shift();b.action.call(this,b.uuid,this.flush)},multi:function(){return this.chain=[],this.inMulti=!0,this},exec:function(a){this.inMulti=!1,this.complete=function(){var b=this,c=this.returned;this.complete=null,this.chain=null,this.returned=[],this.transMap.keys().forEach(function(a){var c=b.transMap.get(a);b.trans.del(c)}),this.transMap=f.create(h,!0),a(c)};var b=this.chain.shift();b.action.call(this,b.uuid,this.flush)},on:function(a,b){var c=this.events.get(a);c||(c=[],this.events.set(a,c)),c.push(b)}},j.prototype.discard=function(a){try{this.trans.abortAll(),(a||function(){})("OK")}catch(b){this.handleError(b)}},j.prototype.handleError=function(){var a=Array.prototype.slice.call(arguments),b=this.events.get("error")||[];b.forEach(function(b){b.apply(null,a)})},j.prototype.get=function(b,c){var d=this;return this.register("read",function(c,e){m(function(f){var g=d.trans.pull(f,c,IDBTransaction.READ_ONLY),h=g.objectStore(a.osName).get(b);h.onerror=d.handleError.bind(d),h.onsuccess=function(a){e.call(d,a.target.result)}},d.handleError.bind(d))},c),this},j.prototype.set=function(b,c,d){var e=this;return this.register("write",function(d,f){m(function(g){var h=e.trans.pull(g,d,IDBTransaction.READ_WRITE),i=h.objectStore(a.osName).put(c,b);i.onerror=e.handleError.bind(e),i.onsuccess=function(a){var c=a.target.result===b?"OK":"ERR";f.call(e,c)}},e.handleError.bind(e))},d),this},j.prototype.incrby=function(d,e,f){var g=this;return this.register("write",function(f,h){m(function(i){var j=g.trans.pull(i,f,IDBTransaction.READ_WRITE),k=j.objectStore(a.osName);(function l(a){if(!b(a)){var f=k.get(d);f.onerror=g.handleError.bind(g),f.onsuccess=function(a){l(typeof a.target.result=="undefined"?0:a.target.result)};return}if(!c(a)){g.handleError("ERROR: Cannot increment a non-integer value.");return}var i=a+e,f=k.put(i,d);f.onerror=g.handleError.bind(g),f.onsuccess=function(a){var b=a.target.result===d?i:"ERR";h.call(g,b)}})()},g.handleError.bind(g))},f),this},j.prototype.incr=function(a,b){return this.incrby(a,1,b)},j.prototype.decrby=function(a,b,c){return this.incrby(a,-b,c)},j.prototype.decr=function(a,b){return this.incrby(a,-1,b)},j.prototype.del=function(){var b=this,c=d.call(arguments),e=c[c.length>0?c.length-1:0];typeof e!="function"?e=undefined:c.splice(c.length-1);var f=c;return this.register("write",function(c,d){m(function(e){var g=b.trans.pull(e,c,IDBTransaction.READ_WRITE),h=g.objectStore(a.osName),i=f.length,j=0;while(f.length>0)(function(){var a=f.shift(),c=h.delete(a);c.onerror=b.handleError.bind(b),c.onsuccess=function(a){i--,a.target.result&&j++,i===0&&d.call(b,j)}})()})},e),this},a.print=function(){var a=Array.prototype.slice.call(arguments);if(a.length===0)return;var b=a[0]instanceof Array?a[0]:[a[0]];b.forEach(function(a){console.log(a)})},a.version=1,a.dbName="gazeldb",a.osName="gazelos",a.compatible=b(window.indexedDB)&&b(window.IDBTransaction),a.createClient=function(){return new j},this.gazel=a;var k,l=!1})).call(this);
+(function(){function g(a,b){for(b=a="";a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):"-");return b}function j(){this.chain=[],this.inMulti=!1,this.returned=[],this.trans=f.create(i,!0),this.transMap=f.create(h,!0),this.events=f.create(h,!0)}function m(b,c){if(k){b(k);return}if(l){setTimeout(function(){m(b,c)},100);return}l=!0;var d=window.indexedDB.open(a.dbName,a.version);d.onupgradeneeded=function(b){var c=b.target.result;c.objectStoreNames.contains(a.osName)||c.createObjectStore(a.osName)};var e;e=d.onsuccess=function(f){var g=f.target.result;if(g.setVersion&&Number(g.version)!==a.version){var h=g.setVersion(String(a.version));h.onsuccess=function(a){var b={};b.target={},b.target.result=a.target.result.db,d.onupgradeneeded(b),e(b);return},h.onerror=c,h.onfailure=c,h.onblocked=c;return}k=g,l=!1,b(k)},d.onerror=c}"use strict";var a=a||{},b=function(a){return typeof a!="undefined"&&a!=null},c=function(a){return!isNaN(a)&&a%1==0};window.indexedDB=window.indexedDB||window.mozIndexedDB||window.msIndexedDB||window.webkitIndexedDB||window.oIndexedDB,window.IDBTransaction=window.IDBTransaction||window.webkitIDBTransaction;var d=Array.prototype.slice,e=Array.prototype.splice,f=Object.create(null);f.create=function(a,b,c){if(typeof b!="undefined"||typeof c!="undefined"||a instanceof Array){typeof b=="boolean"&&(c=b,b=undefined),a instanceof Array||(a=[a]);var d={};for(var e in b)d[e]={value:b[e],writable:!0,enumerable:!0,configurable:!0};var f,g={},h=a.pop();do{var i=a.pop();if(i){var j={};for(var e in h)j[e]=h[e],g[e]=Object.getOwnPropertyDescriptor(j,e);h=Object.create(i,g)}}while(a.length>0);f=Object.create(h,d);if(c){var k=Array.prototype.slice.call(arguments).slice(typeof b=="undefined"?2:3);f.init.apply(f,k)}return f}return Object.create(a)},typeof exports!="undefined"&&(exports.create=f.create);var h=f.create(Object.prototype,{init:function(){return this.items={},this},prop:function(a){return":"+a},get:function(a,b){var c=this.prop(a),d=this.items;return d.hasOwnProperty(c)?d[c]:b},set:function(a,b){var c=this.prop(a);return this.items[c]=b,b},count:function(){return Object.keys(this.items).length},has:function(a){var b=this.prop(a);return this.items.hasOwnProperty(b)},del:function(a){var b=this.prop(a),c=this.items;c.hasOwnProperty(b)&&delete c[b]},keys:function(){return Object.keys(this.items).map(function(a){return a.substring(1)})}}),i=f.create(h,{add:function(){var a=g();return this.set(a,undefined),a},abortAll:function(){var a=this,b=a.keys();b.forEach(function(b){var c=a.get(b);c&&c.abort(),a.del(b)})},pull:function(b,c,d){var e=this.get(c);return e||(e=b.transaction([a.osName],d),e.onerror=onerror,this.set(c,e)),e}});j.prototype={register:function(a,b,c){if(this.inMulti){var e=this.transMap.get(a);e||(e=this.trans.add(),this.transMap.set(a,e)),this.chain.push({uuid:e,action:b});return}var f=this,e=f.trans.add();b(e,function(){var a=d.call(arguments);f.trans.del(e),(c||function(){}).apply(null,a)})},flush:function(){var a=d.call(arguments)||[];this.returned.push(a);if(this.chain.length===0){this.complete();return}var b=this.chain.shift();b.action.call(this,b.uuid,this.flush)},multi:function(){return this.chain=[],this.inMulti=!0,this},exec:function(a){this.inMulti=!1,this.complete=function(){var b=this,c=this.returned;this.complete=null,this.chain=null,this.returned=[],this.transMap.keys().forEach(function(a){var c=b.transMap.get(a);b.trans.del(c)}),this.transMap=f.create(h,!0),a(c)};var b=this.chain.shift();b.action.call(this,b.uuid,this.flush)},on:function(a,b){var c=this.events.get(a);c||(c=[],this.events.set(a,c)),c.push(b)}},j.prototype.discard=function(a){try{this.trans.abortAll(),(a||function(){})("OK")}catch(b){this.handleError(b)}},j.prototype.handleError=function(){var a=Array.prototype.slice.call(arguments),b=this.events.get("error")||[];b.forEach(function(b){b.apply(null,a)})},j.prototype.get=function(b,c){var d=this;return this.register("read",function(c,e){m(function(f){var g=d.trans.pull(f,c,IDBTransaction.READ_ONLY),h=g.objectStore(a.osName).get(b);h.onerror=d.handleError.bind(d),h.onsuccess=function(a){e.call(d,a.target.result)}},d.handleError.bind(d))},c),this},j.prototype.set=function(b,c,d){var e=this;return this.register("write",function(d,f){m(function(g){var h=e.trans.pull(g,d,IDBTransaction.READ_WRITE),i=h.objectStore(a.osName).put(c,b);i.onerror=e.handleError.bind(e),i.onsuccess=function(a){var c=a.target.result===b?"OK":"ERR";f.call(e,c)}},e.handleError.bind(e))},d),this},j.prototype.incrby=function(d,e,f){var g=this;return this.register("write",function(f,h){m(function(i){var j=g.trans.pull(i,f,IDBTransaction.READ_WRITE),k=j.objectStore(a.osName);(function l(a){if(!b(a)){var f=k.get(d);f.onerror=g.handleError.bind(g),f.onsuccess=function(a){l(typeof a.target.result=="undefined"?0:a.target.result)};return}if(!c(a)){g.handleError("ERROR: Cannot increment a non-integer value.");return}var i=a+e,f=k.put(i,d);f.onerror=g.handleError.bind(g),f.onsuccess=function(a){var b=a.target.result===d?i:"ERR";h.call(g,b)}})()},g.handleError.bind(g))},f),this},j.prototype.incr=function(a,b){return this.incrby(a,1,b)},j.prototype.decrby=function(a,b,c){return this.incrby(a,-b,c)},j.prototype.decr=function(a,b){return this.incrby(a,-1,b)},j.prototype.del=function(){var b=this,c=d.call(arguments),e=c[c.length>0?c.length-1:0];typeof e!="function"?e=undefined:c.splice(c.length-1);var f=c,g=f.length;return this.register("write",function(c,d){m(function(e){var h=b.trans.pull(e,c,IDBTransaction.READ_WRITE),i=h.objectStore(a.osName),j=f.length;while(f.length>0)(function(){var a=f.shift(),c=i.delete(a);c.onerror=b.handleError.bind(b),c.onsuccess=function(a){j--,j===0&&d.call(b,g)}})()})},e),this},a.print=function(){var a=Array.prototype.slice.call(arguments);if(a.length===0)return;var b=a[0]instanceof Array?a[0]:[a[0]];b.forEach(function(a){console.log(a)})},a.version=1,a.dbName="gazeldb",a.osName="gazelos",a.compatible=b(window.indexedDB)&&b(window.IDBTransaction),a.createClient=function(){return new j},this.gazel=a;var k,l=!1}).call(this);
View
13 src/del.js
@@ -8,16 +8,15 @@ Client.prototype.del = function() {
else
args.splice(args.length - 1);
- var keys = args;
+ var keys = args,
+ deleted = keys.length;
this.register('write', function(uuid, cb) {
openDatabase(function(db) {
- var tx = self.trans.pull(db, uuid, IDBTransaction.READ_WRITE);
-
- var os = tx.objectStore(gazel.osName),
- left = keys.length,
- deleted = 0;
+ var tx = self.trans.pull(db, uuid, IDBTransaction.READ_WRITE),
+ os = tx.objectStore(gazel.osName),
+ left = keys.length;
while(keys.length > 0) {
(function() {
@@ -27,8 +26,6 @@ Client.prototype.del = function() {
req.onsuccess = function(e) {
left--;
- if(e.target.result) deleted++;
-
if(left === 0)
cb.call(self, deleted);
};
View
11 test/del.js
@@ -23,16 +23,7 @@ describe('Del', function() {
return res === 1;
});
});
-
- it('doesn\'t delete non-existing keys', function(done) {
- // Delete it once to clear clutter.
- client.del('unreal', function(first) {
- client.del('unreal', function(sec) {
- done(assert.equal(sec, 0));
- });
- });
- });
-
+
it('deletes multiple keys if given', function(done) {
var keys = [ 'del_one', 'del_two', 'del_three' ];
var mClient = gazel.createClient(),
Please sign in to comment.
Something went wrong with that request. Please try again.