idb-kv-store
Advanced tools
Comparing version 3.3.0 to 4.0.0
@@ -1,1 +0,1 @@ | ||
(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var t;if(typeof window!=="undefined"){t=window}else if(typeof global!=="undefined"){t=global}else if(typeof self!=="undefined"){t=self}else{t=this}t.IdbKvStore=e()}})(function(){var e,t,r;return function e(t,r,n){function s(o,u){if(!r[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var l=new Error("Cannot find module '"+o+"'");throw l.code="MODULE_NOT_FOUND",l}var c=r[o]={exports:{}};t[o][0].call(c.exports,function(e){var r=t[o][1][e];return s(r?r:e)},c,c.exports,e,t,r,n)}return r[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<n.length;o++)s(n[o]);return s}({1:[function(e,t,r){function n(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}t.exports=n;n.EventEmitter=n;n.prototype._events=undefined;n.prototype._maxListeners=undefined;n.defaultMaxListeners=10;n.prototype.setMaxListeners=function(e){if(!i(e)||e<0||isNaN(e))throw TypeError("n must be a positive number");this._maxListeners=e;return this};n.prototype.emit=function(e){var t,r,n,i,a,l;if(!this._events)this._events={};if(e==="error"){if(!this._events.error||o(this._events.error)&&!this._events.error.length){t=arguments[1];if(t instanceof Error){throw t}else{var c=new Error('Uncaught, unspecified "error" event. ('+t+")");c.context=t;throw c}}}r=this._events[e];if(u(r))return false;if(s(r)){switch(arguments.length){case 1:r.call(this);break;case 2:r.call(this,arguments[1]);break;case 3:r.call(this,arguments[1],arguments[2]);break;default:i=Array.prototype.slice.call(arguments,1);r.apply(this,i)}}else if(o(r)){i=Array.prototype.slice.call(arguments,1);l=r.slice();n=l.length;for(a=0;a<n;a++)l[a].apply(this,i)}return true};n.prototype.addListener=function(e,t){var r;if(!s(t))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",e,s(t.listener)?t.listener:t);if(!this._events[e])this._events[e]=t;else if(o(this._events[e]))this._events[e].push(t);else this._events[e]=[this._events[e],t];if(o(this._events[e])&&!this._events[e].warned){if(!u(this._maxListeners)){r=this._maxListeners}else{r=n.defaultMaxListeners}if(r&&r>0&&this._events[e].length>r){this._events[e].warned=true;console.error("(node) warning: possible EventEmitter memory "+"leak detected. %d listeners added. "+"Use emitter.setMaxListeners() to increase limit.",this._events[e].length);if(typeof console.trace==="function"){console.trace()}}}return this};n.prototype.on=n.prototype.addListener;n.prototype.once=function(e,t){if(!s(t))throw TypeError("listener must be a function");var r=false;function n(){this.removeListener(e,n);if(!r){r=true;t.apply(this,arguments)}}n.listener=t;this.on(e,n);return this};n.prototype.removeListener=function(e,t){var r,n,i,u;if(!s(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;r=this._events[e];i=r.length;n=-1;if(r===t||s(r.listener)&&r.listener===t){delete this._events[e];if(this._events.removeListener)this.emit("removeListener",e,t)}else if(o(r)){for(u=i;u-- >0;){if(r[u]===t||r[u].listener&&r[u].listener===t){n=u;break}}if(n<0)return this;if(r.length===1){r.length=0;delete this._events[e]}else{r.splice(n,1)}if(this._events.removeListener)this.emit("removeListener",e,t)}return this};n.prototype.removeAllListeners=function(e){var t,r;if(!this._events)return this;if(!this._events.removeListener){if(arguments.length===0)this._events={};else if(this._events[e])delete this._events[e];return this}if(arguments.length===0){for(t in this._events){if(t==="removeListener")continue;this.removeAllListeners(t)}this.removeAllListeners("removeListener");this._events={};return this}r=this._events[e];if(s(r)){this.removeListener(e,r)}else if(r){while(r.length)this.removeListener(e,r[r.length-1])}delete this._events[e];return this};n.prototype.listeners=function(e){var t;if(!this._events||!this._events[e])t=[];else if(s(this._events[e]))t=[this._events[e]];else t=this._events[e].slice();return t};n.prototype.listenerCount=function(e){if(this._events){var t=this._events[e];if(s(t))return 1;else if(t)return t.length}return 0};n.listenerCount=function(e,t){return e.listenerCount(t)};function s(e){return typeof e==="function"}function i(e){return typeof e==="number"}function o(e){return typeof e==="object"&&e!==null}function u(e){return e===void 0}},{}],2:[function(e,t,r){if(typeof Object.create==="function"){t.exports=function e(t,r){t.super_=r;t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:false,writable:true,configurable:true}})}}else{t.exports=function e(t,r){t.super_=r;var n=function(){};n.prototype=r.prototype;t.prototype=new n;t.prototype.constructor=t}}},{}],"/":[function(e,t,r){t.exports=u;var n=e("events").EventEmitter;var s=e("inherits");var i=typeof window==="undefined"?self:window;var o=i.indexedDB||i.mozIndexedDB||i.webkitIndexedDB||i.msIndexedDB;u.INDEXEDDB_SUPPORT=o!=null;u.BROADCAST_SUPPORT=i.BroadcastChannel!=null;s(u,n);function u(e,t,r){var s=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!o)throw new Error("IndexedDB not supported");if(typeof t==="function")return new u(e,null,t);if(!(this instanceof u))return new u(e,t,r);if(!t)t={};n.call(s);s._name=e;s._db=null;s._closed=false;s._queue=[];s._Channel=t.channel||i.BroadcastChannel;s._channel=null;var a=o.open(e);a.onerror=l;a.onsuccess=c;a.onupgradeneeded=f;s.on("newListener",v);function l(e){s.close();s._handleError(e,r)}function c(e){if(s._closed){e.target.result.close()}else{s._db=e.target.result;s._db.onclose=h;s._drainQueue();if(r)r(null);s.emit("open")}}function f(e){if(s._closed)return;var t=e.target.result;t.createObjectStore("kv",{autoIncrement:true})}function h(){s.close()}function v(e){if(e!=="change")return;if(!s._Channel)return s.emit("error",new Error("No BroadcastChannel support"));s._channel=new s._Channel(s._name);s._channel.onmessage=d;s.removeListener("newListener",v)}function d(e){s.emit("change",e.data)}}u.prototype.get=function(e,t){var r=this;if(r._closed)throw new Error("Database is closed");var n=a(t);if(!r._db){r._queue.push([r.get,e,n.cb])}else if(Array.isArray(e)){var s=[];var i=false;var o=0;e.forEach(function(t,u){r.get(e[u],function(t,r){if(i)return;if(t){i=true;n.cb(t)}s[u]=r;o++;if(o===e.length)n.cb(null,s)})})}else{var u=r._db.transaction("kv","readonly");var l=u.objectStore("kv").get(e);l.onsuccess=function(e){n.cb(null,e.target.result)};u.onerror=function(e){r._handleError(e,n.cb)}}return n.promise};u.prototype.set=function(e,t,r){var n=this;if(n._closed)throw new Error("Database is closed");var s=a(r);if(!n._db){n._queue.push([n.set,e,t,s.cb])}else{var i=n._db.transaction("kv","readwrite");var o=i.objectStore("kv").put(t,e);o.onsuccess=function(){if(n._channel){n._channel.postMessage({method:"set",key:e,value:t})}s.cb(null)};i.onerror=function(e){n._handleError(e,s.cb)}}return s.promise};u.prototype.json=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=a(e);if(!t._db){t._queue.push([t.json,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var i={};s.onsuccess=function(e){var t=e.target.result;if(t){i[t.key]=t.value;t.continue()}else{r.cb(null,i)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};u.prototype.keys=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=a(e);if(!t._db){t._queue.push([t.keys,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var i=[];s.onsuccess=function(e){var t=e.target.result;if(t){i.push(t.key);t.continue()}else{r.cb(null,i)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};u.prototype.remove=function(e,t){var r=this;if(r._closed)throw new Error("Database is closed");var n=a(t);if(!r._db){r._queue.push([r.remove,e,n.cb])}else{var s=r._db.transaction("kv","readwrite");var i=s.objectStore("kv").delete(e);i.onsuccess=function(t){if(r._channel){r._channel.postMessage({method:"remove",key:e})}n.cb(null)};s.onerror=function(e){r._handleError(e,n.cb)}}return n.promise};u.prototype.clear=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=a(e);if(!t._db){t._queue.push([t.clear,r.cb])}else{var n=t._db.transaction("kv","readwrite");var s=n.objectStore("kv").clear();s.onsuccess=function(e){r.cb(null)};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};u.prototype.count=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=a(e);if(!t._db){t._queue.push([t.count,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").count();s.onsuccess=function(e){r.cb(null,e.target.result)};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};u.prototype.add=function(e,t,r){var n=this;if(n._closed)throw new Error("Database is closed");if(typeof t==="function"||arguments.length===1)return n.add(undefined,e,t);var s=a(r);if(!n._db){n._queue.push([n.add,e,t,s.cb])}else{var i=n._db.transaction("kv","readwrite");var o=e==null?i.objectStore("kv").add(t):i.objectStore("kv").add(t,e);o.onsuccess=function(r){if(n._channel){n._channel.postMessage({method:"add",key:e,value:t})}s.cb(null)};i.onerror=function(e){n._handleError(e,s.cb)}}return s.promise};u.prototype.close=function(){if(this._closed)return;this._closed=true;if(this._db)this._db.close();if(this._channel)this._channel.close();this._db=null;this._channel=null;this._queue=null;this.emit("close");this.removeAllListeners()};u.prototype._drainQueue=function(){var e=this;for(var t=0;t<e._queue.length;t++){var r=e._queue[t];var n=r.splice(1);r[0].apply(e,n)}e._queue=null};u.prototype._handleError=function(e,t){var r=e.target.error;e.preventDefault();if(t){t(r)}else{this.emit("error",r)}};function a(e){var t={cb:e};if(typeof Promise==="function"&&e==null){t.promise=new Promise(function(e,r){t.cb=function(t,n){if(t)return r(t);else return e(n)}})}if(!t.cb)t.cb=function e(){};return t}},{events:1,inherits:2}]},{},[])("/")}); | ||
(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var t;if(typeof window!=="undefined"){t=window}else if(typeof global!=="undefined"){t=global}else if(typeof self!=="undefined"){t=self}else{t=this}t.IdbKvStore=e()}})(function(){var e,t,r;return function e(t,r,n){function s(i,a){if(!r[i]){if(!t[i]){var u=typeof require=="function"&&require;if(!a&&u)return u(i,!0);if(o)return o(i,!0);var l=new Error("Cannot find module '"+i+"'");throw l.code="MODULE_NOT_FOUND",l}var c=r[i]={exports:{}};t[i][0].call(c.exports,function(e){var r=t[i][1][e];return s(r?r:e)},c,c.exports,e,t,r,n)}return r[i].exports}var o=typeof require=="function"&&require;for(var i=0;i<n.length;i++)s(n[i]);return s}({1:[function(e,t,r){function n(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}t.exports=n;n.EventEmitter=n;n.prototype._events=undefined;n.prototype._maxListeners=undefined;n.defaultMaxListeners=10;n.prototype.setMaxListeners=function(e){if(!o(e)||e<0||isNaN(e))throw TypeError("n must be a positive number");this._maxListeners=e;return this};n.prototype.emit=function(e){var t,r,n,o,u,l;if(!this._events)this._events={};if(e==="error"){if(!this._events.error||i(this._events.error)&&!this._events.error.length){t=arguments[1];if(t instanceof Error){throw t}else{var c=new Error('Uncaught, unspecified "error" event. ('+t+")");c.context=t;throw c}}}r=this._events[e];if(a(r))return false;if(s(r)){switch(arguments.length){case 1:r.call(this);break;case 2:r.call(this,arguments[1]);break;case 3:r.call(this,arguments[1],arguments[2]);break;default:o=Array.prototype.slice.call(arguments,1);r.apply(this,o)}}else if(i(r)){o=Array.prototype.slice.call(arguments,1);l=r.slice();n=l.length;for(u=0;u<n;u++)l[u].apply(this,o)}return true};n.prototype.addListener=function(e,t){var r;if(!s(t))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",e,s(t.listener)?t.listener:t);if(!this._events[e])this._events[e]=t;else if(i(this._events[e]))this._events[e].push(t);else this._events[e]=[this._events[e],t];if(i(this._events[e])&&!this._events[e].warned){if(!a(this._maxListeners)){r=this._maxListeners}else{r=n.defaultMaxListeners}if(r&&r>0&&this._events[e].length>r){this._events[e].warned=true;console.error("(node) warning: possible EventEmitter memory "+"leak detected. %d listeners added. "+"Use emitter.setMaxListeners() to increase limit.",this._events[e].length);if(typeof console.trace==="function"){console.trace()}}}return this};n.prototype.on=n.prototype.addListener;n.prototype.once=function(e,t){if(!s(t))throw TypeError("listener must be a function");var r=false;function n(){this.removeListener(e,n);if(!r){r=true;t.apply(this,arguments)}}n.listener=t;this.on(e,n);return this};n.prototype.removeListener=function(e,t){var r,n,o,a;if(!s(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;r=this._events[e];o=r.length;n=-1;if(r===t||s(r.listener)&&r.listener===t){delete this._events[e];if(this._events.removeListener)this.emit("removeListener",e,t)}else if(i(r)){for(a=o;a-- >0;){if(r[a]===t||r[a].listener&&r[a].listener===t){n=a;break}}if(n<0)return this;if(r.length===1){r.length=0;delete this._events[e]}else{r.splice(n,1)}if(this._events.removeListener)this.emit("removeListener",e,t)}return this};n.prototype.removeAllListeners=function(e){var t,r;if(!this._events)return this;if(!this._events.removeListener){if(arguments.length===0)this._events={};else if(this._events[e])delete this._events[e];return this}if(arguments.length===0){for(t in this._events){if(t==="removeListener")continue;this.removeAllListeners(t)}this.removeAllListeners("removeListener");this._events={};return this}r=this._events[e];if(s(r)){this.removeListener(e,r)}else if(r){while(r.length)this.removeListener(e,r[r.length-1])}delete this._events[e];return this};n.prototype.listeners=function(e){var t;if(!this._events||!this._events[e])t=[];else if(s(this._events[e]))t=[this._events[e]];else t=this._events[e].slice();return t};n.prototype.listenerCount=function(e){if(this._events){var t=this._events[e];if(s(t))return 1;else if(t)return t.length}return 0};n.listenerCount=function(e,t){return e.listenerCount(t)};function s(e){return typeof e==="function"}function o(e){return typeof e==="number"}function i(e){return typeof e==="object"&&e!==null}function a(e){return e===void 0}},{}],2:[function(e,t,r){if(typeof Object.create==="function"){t.exports=function e(t,r){t.super_=r;t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:false,writable:true,configurable:true}})}}else{t.exports=function e(t,r){t.super_=r;var n=function(){};n.prototype=r.prototype;t.prototype=new n;t.prototype.constructor=t}}},{}],"/":[function(e,t,r){t.exports=a;var n=e("events").EventEmitter;var s=e("inherits");var o=typeof window==="undefined"?self:window;var i=o.indexedDB||o.mozIndexedDB||o.webkitIndexedDB||o.msIndexedDB;a.INDEXEDDB_SUPPORT=i!=null;a.BROADCAST_SUPPORT=o.BroadcastChannel!=null;s(a,n);function a(e,t,r){var s=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!i)throw new Error("IndexedDB not supported");if(typeof t==="function")return new a(e,null,t);if(!(this instanceof a))return new a(e,t,r);if(!t)t={};n.call(s);s._name=e;s._db=null;s._closed=false;s._queue=[];s._Channel=t.channel||o.BroadcastChannel;s._channel=null;var u=i.open(e);u.onerror=l;u.onsuccess=c;u.onupgradeneeded=f;s.on("newListener",v);function l(e){s.close();s._handleError(e,r)}function c(e){if(s._closed){e.target.result.close()}else{s._db=e.target.result;s._db.onclose=h;s._drainQueue();if(r)r(null);s.emit("open")}}function f(e){if(s._closed)return;var t=e.target.result;t.createObjectStore("kv",{autoIncrement:true})}function h(){s.close()}function v(e){if(e!=="add"&&e!=="set"&&e!=="remove")return;if(!s._Channel)return s.emit("error",new Error("No BroadcastChannel support"));s._channel=new s._Channel(s._name);s._channel.onmessage=d;s.removeListener("newListener",v)}function d(e){if(e.data.method==="add")s.emit("add",e.data);else if(e.data.method==="set")s.emit("set",e.data);else if(e.data.method==="remove")s.emit("remove",e.data)}}a.prototype.get=function(e,t){var r=this;if(r._closed)throw new Error("Database is closed");var n=u(t);if(!r._db){r._queue.push([r.get,e,n.cb])}else if(Array.isArray(e)){var s=[];var o=false;var i=0;e.forEach(function(t,a){r.get(e[a],function(t,r){if(o)return;if(t){o=true;n.cb(t)}s[a]=r;i++;if(i===e.length)n.cb(null,s)})})}else{var a=r._db.transaction("kv","readonly");var l=a.objectStore("kv").get(e);l.onsuccess=function(e){n.cb(null,e.target.result)};a.onerror=function(e){r._handleError(e,n.cb)}}return n.promise};a.prototype.set=function(e,t,r){var n=this;if(n._closed)throw new Error("Database is closed");var s=u(r);if(!n._db){n._queue.push([n.set,e,t,s.cb])}else{var o=n._db.transaction("kv","readwrite");var i=o.objectStore("kv").put(t,e);i.onsuccess=function(){if(n._channel){n._channel.postMessage({method:"set",key:e,value:t})}s.cb(null)};o.onerror=function(e){n._handleError(e,s.cb)}}return s.promise};a.prototype.json=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.json,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var o={};s.onsuccess=function(e){var t=e.target.result;if(t){o[t.key]=t.value;t.continue()}else{r.cb(null,o)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.keys=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.keys,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var o=[];s.onsuccess=function(e){var t=e.target.result;if(t){o.push(t.key);t.continue()}else{r.cb(null,o)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.values=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.values,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var o=[];s.onsuccess=function(e){var t=e.target.result;if(t){o.push(t.value);t.continue()}else{r.cb(null,o)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.remove=function(e,t){var r=this;if(r._closed)throw new Error("Database is closed");var n=u(t);if(!r._db){r._queue.push([r.remove,e,n.cb])}else{var s=r._db.transaction("kv","readwrite");var o=s.objectStore("kv").delete(e);o.onsuccess=function(t){if(r._channel){r._channel.postMessage({method:"remove",key:e})}n.cb(null)};s.onerror=function(e){r._handleError(e,n.cb)}}return n.promise};a.prototype.clear=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.clear,r.cb])}else{var n=t._db.transaction("kv","readwrite");var s=n.objectStore("kv").clear();s.onsuccess=function(e){r.cb(null)};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.count=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.count,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").count();s.onsuccess=function(e){r.cb(null,e.target.result)};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.add=function(e,t,r){var n=this;if(n._closed)throw new Error("Database is closed");if(typeof t==="function"||arguments.length===1)return n.add(undefined,e,t);var s=u(r);if(!n._db){n._queue.push([n.add,e,t,s.cb])}else{var o=n._db.transaction("kv","readwrite");var i=e==null?o.objectStore("kv").add(t):o.objectStore("kv").add(t,e);i.onsuccess=function(r){if(n._channel){n._channel.postMessage({method:"add",key:e,value:t})}s.cb(null)};o.onerror=function(e){n._handleError(e,s.cb)}}return s.promise};a.prototype.close=function(){if(this._closed)return;this._closed=true;if(this._db)this._db.close();if(this._channel)this._channel.close();this._db=null;this._channel=null;this._queue=null;this.emit("close");this.removeAllListeners()};a.prototype._drainQueue=function(){var e=this;for(var t=0;t<e._queue.length;t++){var r=e._queue[t];var n=r.splice(1);r[0].apply(e,n)}e._queue=null};a.prototype._handleError=function(e,t){var r=e.target.error;e.preventDefault();if(t){t(r)}else{this.emit("error",r)}};function u(e){var t={cb:e};if(typeof Promise==="function"&&e==null){t.promise=new Promise(function(e,r){t.cb=function(t,n){if(t)return r(t);else return e(n)}})}if(!t.cb)t.cb=function e(){};return t}},{events:1,inherits:2}]},{},[])("/")}); |
36
index.js
@@ -65,3 +65,3 @@ module.exports = IdbKvStore | ||
function onNewListener (event) { | ||
if (event !== 'change') return | ||
if (event !== 'add' && event !== 'set' && event !== 'remove') return | ||
if (!self._Channel) return self.emit('error', new Error('No BroadcastChannel support')) | ||
@@ -74,3 +74,5 @@ self._channel = new self._Channel(self._name) | ||
function onChange (event) { | ||
self.emit('change', event.data) | ||
if (event.data.method === 'add') self.emit('add', event.data) | ||
else if (event.data.method === 'set') self.emit('set', event.data) | ||
else if (event.data.method === 'remove') self.emit('remove', event.data) | ||
} | ||
@@ -208,2 +210,32 @@ } | ||
IdbKvStore.prototype.values = function (cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
if (!self._db) { | ||
self._queue.push([self.values, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readonly') | ||
var request = transaction.objectStore('kv').openCursor() | ||
var values = [] | ||
request.onsuccess = function (event) { | ||
var cursor = event.target.result | ||
if (cursor) { | ||
values.push(cursor.value) | ||
cursor.continue() | ||
} else { | ||
defer.cb(null, values) | ||
} | ||
} | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
} | ||
} | ||
return defer.promise | ||
} | ||
IdbKvStore.prototype.remove = function (key, cb) { | ||
@@ -210,0 +242,0 @@ var self = this |
{ | ||
"name": "idb-kv-store", | ||
"version": "3.3.0", | ||
"version": "4.0.0", | ||
"description": "Persistent key-value store for web browsers backed by IndexedDB", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# idb-kv-store [![Build Status](https://travis-ci.org/xuset/idb-kv-store.svg?branch=master)](https://travis-ci.org/xuset/idb-kv-store) [![npm](https://img.shields.io/npm/v/idb-kv-store.svg)](https://npmjs.org/package/idb-kv-store) | ||
Persistent key-value store for web browsers backed by IndexedDB | ||
#### Simple key-value store backed by IndexedDB | ||
@@ -9,3 +9,3 @@ [![Sauce Test Status](https://saucelabs.com/browser-matrix/xuset-idb-kv.svg)](https://saucelabs.com/u/xuset-idb-kv) | ||
Additionally, the 'change' event allows users to listen for database changes that occur in different instances, windows, or workers. | ||
Additionally, mutation events allow users to listen for database changes that occur in different instances, windows, or workers. | ||
@@ -17,3 +17,3 @@ This module can be used with [browserify](http://browserify.org/) or the [idbkvstore.min.js](https://raw.githubusercontent.com/xuset/idb-kv-store/master/idbkvstore.min.js) script can be included which will attach `IdbKvStore` to `window`. | ||
```js | ||
var store = new IdbKvStore('your stores name') | ||
var store = new IdbKvStore('the name of store') | ||
@@ -31,8 +31,16 @@ // Store the value 'def' at key 'abc' | ||
```js | ||
var store = new IdbKvStore('your stores name') | ||
store.get('abc').then(value => { | ||
console.log('key=abc value=' + value) | ||
}) | ||
``` | ||
// Store the value 'def' at key 'abc' | ||
store.set('abc', 'def') | ||
.then(() => store.get('abc')) | ||
.then((value) => console.log('key=abc value=' + value)) | ||
Listen for database mutation events | ||
```js | ||
store.on('add', change => { | ||
console.log('key=' + change.key, 'value=' + change.value) | ||
}) | ||
// 'add' fails if the key already exists | ||
someOtherStore.add('foo', 'bar') | ||
``` | ||
@@ -77,2 +85,6 @@ | ||
### `store.values([cb])` | ||
Retrieves the list of values stored. When the list is retrieved, `cb` is called with `cb(err, keys)`. If `cb` is undefined then a promise is returned. | ||
### `store.json([cb])` | ||
@@ -96,3 +108,3 @@ | ||
Detects native BroadcastChannel support. If the BroadcastChannel api is not present then the 'change' event will never be emitted. | ||
Detects native BroadcastChannel support. If the BroadcastChannel api is not present then the mutation events will not be emitted. | ||
@@ -105,12 +117,31 @@ ## Events | ||
### `store.on('change', function (change) {})` | ||
### `store.on('add', function (change) {})` | ||
When another instance makes a modifying change to the database this event is emitted on all instances of the same database except for the instance that initiated the operation. `change` has the following properties: | ||
Emitted when another instance adds an item to the database by calling `store.add(...)`. The `change` object has the following properties: | ||
* change.method - Either: 'add', 'set', 'remove' | ||
* change.key - the key that was modifed | ||
* change.value - the new value stored at `key`. Only defined for 'add' and 'set' | ||
* `change.method` - always set to 'add' | ||
* `change.key` - the key that value was added to | ||
* `change.value` - the new value | ||
For the 'change' event to be emitted the browser must have implemented the BroadcastChannel api . If the api does not exist, then setting a listener for this event will throw an error. To detect if native BroadcastChannel support exists, see: `IdbKvStore.BROADCAST_SUPPORT`. | ||
See [Supported Browsers](#Supported Browsers) for more info on which browsers support this event. | ||
### `store.on('set', function (change) {})` | ||
Emitted when another instance sets the value of a key in the database by calling `store.set(...)`. The `change` object has the following properties: | ||
* `change.method` - always set to 'set' | ||
* `change.key` - the key that value was set to | ||
* `change.value` - the new value | ||
See [Supported Browsers](#Supported Browsers) for more info on which browsers support this event. | ||
### `store.on('remove', function (change) {})` | ||
Emitted when another instance removes an item from the database by calling `store.remove(...)`. The `change` object has the following properties: | ||
* `change.method` - always set to 'remove' | ||
* `change.key` - the key of the value that was removed | ||
See [Supported Browsers](#Supported Browsers) for more info on which browsers support this event. | ||
### `store.on('close', function () {})` | ||
@@ -122,6 +153,12 @@ | ||
Emitted if any unhandled error occures. If an error occures in a function that was passed a callback, the error will be propagated through the callback instead of this event. If there is no callback to handle the error, then this event is emitted. | ||
Emitted if any unhandled error occures. | ||
## Supported Browsers | ||
idb-kv-store supports all browsers that have implemented the IndexedDB api. However, the mutation events, add/set/remove, are only available in browsers that have implemented the BroadcastChannel api. Attempting to listen on a add/set/remove event in a browser that does not support the BroadcastChannel api will cause an error to be emitted. `IdbKvStore.BROADCAST_SUPPORT` will indicate if the browser supports this api. | ||
The list of browsers that support BroadcastChannels can be found on [caniuse.com](http://caniuse.com/#search=broadcastchannel) | ||
## License | ||
MIT. Copyright (c) Austin Middleton. |
61
test.js
@@ -261,3 +261,3 @@ var IdbKvStore = require('.') | ||
test('listen on "change" fails if not supported', function (t) { | ||
test('listen on add/set/remove events fail if not supported', function (t) { | ||
t.timeoutAfter(3000) | ||
@@ -267,11 +267,25 @@ if (IdbKvStore.BROADCAST_SUPPORT) return t.end() | ||
var store = createStore() | ||
store.on('error', function (err) { | ||
store.once('error', addError) | ||
store.on('add', function () {}) | ||
function addError (err) { | ||
t.ok(err instanceof Error) | ||
store.once('error', setError) | ||
store.on('set', function () {}) | ||
} | ||
function setError (err) { | ||
t.ok(err instanceof Error) | ||
store.once('error', removeError) | ||
store.on('remove', function () {}) | ||
} | ||
function removeError (err) { | ||
t.ok(err instanceof Error) | ||
t.end() | ||
}) | ||
store.on('change', function () {}) | ||
} | ||
}) | ||
test('change event', function (t) { | ||
test('add/set/remove events', function (t) { | ||
t.timeoutAfter(3000) | ||
@@ -283,7 +297,10 @@ if (!IdbKvStore.BROADCAST_SUPPORT) return t.end() | ||
storeA.on('change', function (change) { | ||
t.fail() | ||
}) | ||
storeA.on('add', fail) | ||
storeA.on('set', fail) | ||
storeA.on('remove', fail) | ||
storeB.once('change', onAdd) | ||
storeB.on('add', onAdd) | ||
storeB.on('set', onSet) | ||
storeB.on('remove', onRemove) | ||
storeA.add('foo', 'bar', function (err) { | ||
@@ -299,3 +316,3 @@ t.equal(err, null) | ||
}, change) | ||
storeB.once('change', onSet) | ||
storeA.set('foo', 'barbar', function (err) { | ||
@@ -312,3 +329,3 @@ t.equal(err, null) | ||
}, change) | ||
storeB.once('change', onRemove) | ||
storeA.remove('foo', function (err) { | ||
@@ -326,2 +343,6 @@ t.equal(err, null) | ||
} | ||
function fail () { | ||
t.fail() | ||
} | ||
}) | ||
@@ -345,2 +366,18 @@ | ||
test('values()', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
store.add('A', function (err) { | ||
t.equal(err, null) | ||
store.add('B', function (err) { | ||
t.equal(err, null) | ||
store.values(function (err, values) { | ||
t.equal(err, null) | ||
t.deepEqual(values, ['A', 'B']) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
function createStore (cb) { | ||
@@ -347,0 +384,0 @@ var name = '' + (Math.round(9e16 * Math.random())) |
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
39344
704
158