(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,n;return function e(t,n,r){function i(s,u){if(!n[s]){if(!t[s]){var a=typeof require=="function"&&require;if(!u&&a)return a(s,!0);if(o)return o(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var f=n[s]={exports:{}};t[s][0].call(f.exports,function(e){var n=t[s][1][e];return i(n?n:e)},f,f.exports,e,t,n,r)}return n[s].exports}var o=typeof require=="function"&&require;for(var s=0;s<r.length;s++)i(r[s]);return i}({1:[function(e,t,n){function r(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}t.exports=r;r.EventEmitter=r;r.prototype._events=undefined;r.prototype._maxListeners=undefined;r.defaultMaxListeners=10;r.prototype.setMaxListeners=function(e){if(!o(e)||e<0||isNaN(e))throw TypeError("n must be a positive number");this._maxListeners=e;return this};r.prototype.emit=function(e){var t,n,r,o,a,l;if(!this._events)this._events={};if(e==="error"){if(!this._events.error||s(this._events.error)&&!this._events.error.length){t=arguments[1];if(t instanceof Error){throw t}else{var f=new Error('Uncaught, unspecified "error" event. ('+t+")");f.context=t;throw f}}}n=this._events[e];if(u(n))return false;if(i(n)){switch(arguments.length){case 1:n.call(this);break;case 2:n.call(this,arguments[1]);break;case 3:n.call(this,arguments[1],arguments[2]);break;default:o=Array.prototype.slice.call(arguments,1);n.apply(this,o)}}else if(s(n)){o=Array.prototype.slice.call(arguments,1);l=n.slice();r=l.length;for(a=0;a<r;a++)l[a].apply(this,o)}return true};r.prototype.addListener=function(e,t){var n;if(!i(t))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",e,i(t.listener)?t.listener:t);if(!this._events[e])this._events[e]=t;else if(s(this._events[e]))this._events[e].push(t);else this._events[e]=[this._events[e],t];if(s(this._events[e])&&!this._events[e].warned){if(!u(this._maxListeners)){n=this._maxListeners}else{n=r.defaultMaxListeners}if(n&&n>0&&this._events[e].length>n){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};r.prototype.on=r.prototype.addListener;r.prototype.once=function(e,t){if(!i(t))throw TypeError("listener must be a function");var n=false;function r(){this.removeListener(e,r);if(!n){n=true;t.apply(this,arguments)}}r.listener=t;this.on(e,r);return this};r.prototype.removeListener=function(e,t){var n,r,o,u;if(!i(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;n=this._events[e];o=n.length;r=-1;if(n===t||i(n.listener)&&n.listener===t){delete this._events[e];if(this._events.removeListener)this.emit("removeListener",e,t)}else if(s(n)){for(u=o;u-- >0;){if(n[u]===t||n[u].listener&&n[u].listener===t){r=u;break}}if(r<0)return this;if(n.length===1){n.length=0;delete this._events[e]}else{n.splice(r,1)}if(this._events.removeListener)this.emit("removeListener",e,t)}return this};r.prototype.removeAllListeners=function(e){var t,n;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}n=this._events[e];if(i(n)){this.removeListener(e,n)}else if(n){while(n.length)this.removeListener(e,n[n.length-1])}delete this._events[e];return this};r.prototype.listeners=function(e){var t;if(!this._events||!this._events[e])t=[];else if(i(this._events[e]))t=[this._events[e]];else t=this._events[e].slice();return t};r.prototype.listenerCount=function(e){if(this._events){var t=this._events[e];if(i(t))return 1;else if(t)return t.length}return 0};r.listenerCount=function(e,t){return e.listenerCount(t)};function i(e){return typeof e==="function"}function o(e){return typeof e==="number"}function s(e){return typeof e==="object"&&e!==null}function u(e){return e===void 0}},{}],2:[function(e,t,n){if(typeof Object.create==="function"){t.exports=function e(t,n){t.super_=n;t.prototype=Object.create(n.prototype,{constructor:{value:t,enumerable:false,writable:true,configurable:true}})}}else{t.exports=function e(t,n){t.super_=n;var r=function(){};r.prototype=n.prototype;t.prototype=new r;t.prototype.constructor=t}}},{}],3:[function(e,t,n){t.exports=r;function r(e){var t;var n;var r;if(e!=null&&typeof e!=="function")throw new Error("cb must be a function");if(e==null&&typeof Promise!=="undefined"){t=new Promise(function(e,t){n=e;r=t})}function i(i,o){if(t){if(i)r(i);else n(o)}else{if(e)e(i,o);else if(i)throw i}}i.promise=t;return i}},{}],"/":[function(e,t,n){t.exports=a;var r=e("events").EventEmitter;var i=e("inherits");var o=e("promisize");var s=typeof window==="undefined"?self:window;var u=s.indexedDB||s.mozIndexedDB||s.webkitIndexedDB||s.msIndexedDB;a.INDEXEDDB_SUPPORT=u!=null;a.BROADCAST_SUPPORT=s.BroadcastChannel!=null;i(a,r);function a(e,t,n){var i=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!u)throw new Error("IndexedDB not supported");if(typeof t==="function")return new a(e,null,t);if(!(i instanceof a))return new a(e,t,n);if(!t)t={};r.call(i);i._db=null;i._closed=false;i._channel=null;i._waiters=[];var o=t.channel||s.BroadcastChannel;if(o){i._channel=new o(e);i._channel.onmessage=y}var l=u.open(e);l.onerror=c;l.onsuccess=p;l.onupgradeneeded=d;i.on("newListener",_);function c(e){f(e);i._close(e.target.error);if(n)n(e.target.error)}function h(e){f(e);i._close(e.target.error)}function p(e){if(i._closed){e.target.result.close()}else{i._db=e.target.result;i._db.onclose=v;i._db.onerror=h;for(var t in i._waiters)i._waiters[t]._init(null);i._waiters=null;if(n)n(null);i.emit("open")}}function d(e){var t=e.target.result;t.createObjectStore("kv",{autoIncrement:true})}function v(){i._close()}function _(e){if(e!=="add"&&e!=="set"&&e!=="remove")return;if(!i._channel)return i.emit("error",new Error("No BroadcastChannel support"))}function y(e){if(e.data.method==="add")i.emit("add",e.data);else if(e.data.method==="set")i.emit("set",e.data);else if(e.data.method==="remove")i.emit("remove",e.data)}}a.prototype.get=function(e,t){return this.transaction("readonly").get(e,t)};a.prototype.set=function(e,t,n){return this.transaction("readwrite").set(e,t,n)};a.prototype.json=function(e,t){return this.transaction("readonly").json(e,t)};a.prototype.keys=function(e,t){return this.transaction("readonly").keys(e,t)};a.prototype.values=function(e,t){return this.transaction("readonly").values(e,t)};a.prototype.remove=function(e,t){return this.transaction("readwrite").remove(e,t)};a.prototype.clear=function(e){return this.transaction("readwrite").clear(e)};a.prototype.count=function(e,t){return this.transaction("readonly").count(e,t)};a.prototype.add=function(e,t,n){return this.transaction("readwrite").add(e,t,n)};a.prototype.iterator=function(e,t){return this.transaction("readonly").iterator(e,t)};a.prototype.transaction=function(e){if(this._closed)throw new Error("Database is closed");var t=new l(this,e);if(this._db)t._init(null);else this._waiters.push(t);return t};a.prototype.close=function(){this._close()};a.prototype._close=function(e){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;if(e)this.emit("error",e);this.emit("close");for(var t in this._waiters)this._waiters[t]._init(e||new Error("Database is closed"));this._waiters=null;this.removeAllListeners()};function l(e,t){this._kvStore=e;this._mode=t||"readwrite";this._objectStore=null;this._waiters=null;this.finished=false;this.onfinish=null;if(this._mode!=="readonly"&&this._mode!=="readwrite"){throw new Error('mode must be either "readonly" or "readwrite"')}}l.prototype._init=function(e){var t=this;if(t.finished)return;if(e)return t._close(e);var n=t._kvStore._db.transaction("kv",t._mode);n.oncomplete=i;n.onerror=o;t._objectStore=n.objectStore("kv");for(var r in t._waiters)t._waiters[r](null,t._objectStore);t._waiters=null;function i(){t._close(null)}function o(e){f(e);t._close(e.target.error)}};l.prototype._getObjectStore=function(e){if(this.finished)throw new Error("Transaction is finished");if(this._objectStore)return e(null,this._objectStore);this._waiters=this._waiters||[];this._waiters.push(e)};l.prototype.set=function(e,t,n){var r=this;if(e==null||t==null)throw new Error("A key and value must be given");n=o(n);r._getObjectStore(function(i,o){if(i)return n(i);try{var s=o.put(t,e)}catch(e){return n(e)}s.onerror=f.bind(this,n);s.onsuccess=function(){if(r._kvStore._channel){r._kvStore._channel.postMessage({method:"set",key:e,value:t})}n(null)}});return n.promise};l.prototype.add=function(e,t,n){var r=this;if(t==null&&e!=null)return r.add(undefined,e,n);if(typeof t==="function"||t==null&&n==null)return r.add(undefined,e,t);if(t==null)throw new Error("A value must be provided as an argument");n=o(n);r._getObjectStore(function(i,o){if(i)return n(i);try{var s=e==null?o.add(t):o.add(t,e)}catch(e){return n(e)}s.onerror=f.bind(this,n);s.onsuccess=function(){if(r._kvStore._channel){r._kvStore._channel.postMessage({method:"add",key:e,value:t})}n(null)}});return n.promise};l.prototype.get=function(e,t){var n=this;if(e==null)throw new Error("A key must be given as an argument");t=o(t);n._getObjectStore(function(n,r){if(n)return t(n);try{var i=r.get(e)}catch(e){return t(e)}i.onerror=f.bind(this,t);i.onsuccess=function(e){t(null,e.target.result)}});return t.promise};l.prototype.json=function(e,t){var n=this;if(typeof e==="function")return n.json(null,e);t=o(t);var r={};n.iterator(e,function(e,n){if(e)return t(e);if(n){r[n.key]=n.value;n.continue()}else{t(null,r)}});return t.promise};l.prototype.keys=function(e,t){var n=this;if(typeof e==="function")return n.keys(null,e);t=o(t);var r=[];n.iterator(e,function(e,n){if(e)return t(e);if(n){r.push(n.key);n.continue()}else{t(null,r)}});return t.promise};l.prototype.values=function(e,t){var n=this;if(typeof e==="function")return n.values(null,e);t=o(t);var r=[];n.iterator(e,function(e,n){if(e)return t(e);if(n){r.push(n.value);n.continue()}else{t(null,r)}});return t.promise};l.prototype.remove=function(e,t){var n=this;if(e==null)throw new Error("A key must be given as an argument");t=o(t);n._getObjectStore(function(r,i){if(r)return t(r);try{var o=i.delete(e)}catch(e){return t(e)}o.onerror=f.bind(this,t);o.onsuccess=function(){if(n._kvStore._channel){n._kvStore._channel.postMessage({method:"remove",key:e})}t(null)}});return t.promise};l.prototype.clear=function(e){var t=this;e=o(e);t._getObjectStore(function(t,n){if(t)return e(t);try{var r=n.clear()}catch(t){return e(t)}r.onerror=f.bind(this,e);r.onsuccess=function(){e(null)}});return e.promise};l.prototype.count=function(e,t){var n=this;if(typeof e==="function")return n.count(null,e);t=o(t);n._getObjectStore(function(n,r){if(n)return t(n);try{var i=e==null?r.count():r.count(e)}catch(e){return t(e)}i.onerror=f.bind(this,t);i.onsuccess=function(e){t(null,e.target.result)}});return t.promise};l.prototype.iterator=function(e,t){var n=this;if(typeof e==="function")return n.iterator(null,e);if(typeof t!=="function")throw new Error("A function must be given");n._getObjectStore(function(n,r){if(n)return t(n);try{var i=e==null?r.openCursor():r.openCursor(e)}catch(e){return t(e)}i.onerror=f.bind(this,t);i.onsuccess=function(e){var n=e.target.result;t(null,n)}})};l.prototype.abort=function(){if(this.finished)throw new Error("Transaction is finished");if(this._objectStore)this._objectStore.transaction.abort();this._close(new Error("Transaction aborted"))};l.prototype._close=function(e){if(this.finished)return;this.finished=true;this._kvStore=null;this._objectStore=null;for(var t in this._waiters)this._waiters[t](e||new Error("Transaction is finished"));if(this.onfinish)this.onfinish(e);this.onfinish=null;this._waiters=null};function f(e,t){if(t==null)return f(null,e);t.preventDefault();t.stopPropagation();if(e)e(t.target.error)}},{events:1,inherits:2,promisize:3}]},{},[])("/")});
