rxidb
Version:
RxJS wrapper for IndexedDB
1 lines • 8.85 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define(["rxjs","rxjs/operators"],e):"object"==typeof exports?exports.rxidb=e(require("rxjs"),require("rxjs/operators")):t.rxidb=e(t.rxjs,t["rxjs/operators"])}(window,function(t,e){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=5)}([function(e,r){e.exports=t},function(t,r){t.exports=e},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(0),o=r(1),i=r(6),u=function(){function t(t,e){var r=this;this.name=t,this._db=e,this._dataUpdate$=new n.Subject,this._dataUpdateKey$=new n.Subject,this.keyPath$=this.tx("readwrite").pipe(o.map(function(t){return t.objectStore(r.name)}),o.map(function(t){return t.keyPath})),this.data$=n.merge(this.getAll(),this._dataUpdate$).pipe(o.shareReplay(1)),this._dataTs$=new n.BehaviorSubject(-1),this.dataTs$=this._dataTs$.asObservable()}return t.prototype.clear=function(){var t=this;return this.tx("readwrite").pipe(o.map(function(e){return e.objectStore(t.name)}),o.map(function(t){return t.clear()}),o.switchMap(function(t){return i.rxifyRequest(t)}),o.switchMap(function(e){return t._refreshDataStream(e)}),o.take(1),o.mapTo(void 0))},t.prototype.cursor=function(t,e,r){var n=this;return void 0===t&&(t="readonly"),this.tx(t).pipe(o.map(function(t){return t.objectStore(n.name)}),o.map(function(t){return t.openCursor(e,r)}),o.switchMap(function(t){return i.rxifyRequest(t)}),i.resultFromIDBEvent)},t.prototype.delete=function(t){var e=this;return this.tx("readwrite").pipe(o.map(function(t){return t.objectStore(e.name)}),o.map(function(e){return e.delete(t)}),o.switchMap(function(t){return i.rxifyRequest(t)}),o.switchMap(function(r){return e._refreshDataStream(r,t)}),o.take(1),o.mapTo(void 0))},t.prototype.reset=function(t){var e=this;return this.tx("readwrite").pipe(o.map(function(t){return t.objectStore(e.name)}),o.switchMap(function(t){return i.rxifyRequest(t.clear()).pipe(o.take(1),o.mapTo(t))}),o.map(function(e){return t.map(function(t){return i.rxifyRequest(e.put(t)).pipe(o.take(1),i.resultFromIDBEvent)})}),o.switchMap(function(t){return n.forkJoin(t)}),o.switchMap(function(t){return e._refreshDataStream(t,"__each__")}),o.take(1))},t.prototype.get=function(t){var e=this;return n.merge(n.of(null),this._getUpdatesFor(t)).pipe(o.switchMap(function(){return e.tx()}),o.map(function(t){return t.objectStore(e.name)}),o.map(function(e){return e.get(t)}),o.switchMap(function(t){return i.rxifyRequest(t)}),i.resultFromIDBEvent)},t.prototype.getAll=function(){var t=this.cursor(),e=new n.Subject;return t.pipe(o.tap(function(t){return t?t.continue():e.next()}),o.filter(function(t){return!!t}),o.map(function(t){return t.value}),o.bufferWhen(function(){return e}),o.take(1))},t.prototype.set=function(t,e){var r=this;return this.tx("readwrite").pipe(o.map(function(t){return t.objectStore(r.name)}),o.map(function(r){return r.put(t,e)}),o.switchMap(function(t){return i.rxifyRequest(t)}),i.resultFromIDBEvent,o.switchMap(function(t){return r._refreshDataStream(t,e)}))},t.prototype.tx=function(t){return void 0===t&&(t="readonly"),this._db.tx(this.name,t)},t.prototype._refreshDataStream=function(t,e){var r=this;return void 0===e&&(e="__each__"),this.getAll().pipe(o.take(1),o.tap(function(t){r._dataTs$.next(Date.now()),r._dataUpdate$.next(t),r._dataUpdateKey$.next(e)}),o.mapTo(t))},t.prototype._getUpdatesFor=function(t){return this._dataUpdateKey$.pipe(o.filter(function(e){return t===e}))},t}();e.RxIDBStore=u},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(0),o=r(1),i=r(2),u=function(){function t(t){this.idb=t,this._storeCache={}}return t.prototype.close=function(){this.idb.close()},t.prototype.get=function(t){return this._storeCache[t]?this._storeCache[t]:this._storeCache[t]=new i.RxIDBStore(t,this)},t.prototype.tx=function(t,e){var r=this;return void 0===e&&(e="readonly"),n.of(null).pipe(o.map(function(){return r.idb.transaction(t,e)}))},t.prototype.transaction=function(t,e){return void 0===e&&(e="readonly"),this.idb.transaction(t,e)},t}();e.RxIDB=u},function(t,e,r){"use strict";var n,o=this&&this.__extends||(n=function(t,e){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)},function(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)});Object.defineProperty(e,"__esModule",{value:!0});var i=function(t){function e(e,r,n){var o=t.call(this,e,r)||this;return o._store=n,o}return o(e,t),e.prototype.createIndex=function(t,e,r){return this._store.createIndex(t,e,r)},e.prototype.fill=function(t){var e=this;return Array.from(t).forEach(function(t){return e._store.put(t)}),this},e}(r(2).RxIDBStore);e.RxIDBUpgradeStore=i;var u=function(){function t(t,e){this._rxidb=t,this.db=e.target.result,this.oldVersion=e.oldVersion,this.version=e.newVersion}return t.prototype.create=function(t,e){var r=this.db.createObjectStore(t,e);return new i(t,this._rxidb,r)},t.prototype.delete=function(t){this.db.deleteObjectStore(t)},t.prototype.has=function(t){return this.db.objectStoreNames.contains(t)},t}();e.RxIDBUpgrade=u},function(t,e,r){"use strict";function n(t){for(var r in t)e.hasOwnProperty(r)||(e[r]=t[r])}Object.defineProperty(e,"__esModule",{value:!0}),n(r(3)),n(r(2)),n(r(4));var o=r(7);e.openDB=o.openDB,e.dropDB=o.dropDB},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(0),o=r(1);e.rxifyRequest=function(t,e,r){void 0===e&&(e=["onsuccess"]),void 0===r&&(r=["onerror"]);var o=new n.Subject;return e.forEach(function(e){return t[e]=function(t){return o.next(t)}}),r.forEach(function(e){return t[e]=function(t){return o.error(t)}}),o},e.resultFromIDBEvent=function(t){return t.pipe(o.map(function(t){return t.target.result}))}},function(t,e,r){"use strict";var n=this&&this.__values||function(t){var e="function"==typeof Symbol&&t[Symbol.iterator],r=0;return e?e.call(t):{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}}},o=this&&this.__read||function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)u.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return u};Object.defineProperty(e,"__esModule",{value:!0});var i=r(0),u=r(1),a=r(3),c=r(4);function s(t,e,r){var n,o=new i.Subject,s=new i.Subject,f=function(t){return o.error(t)},d=function(t){return o.error(t)},l=function(t){n=n||new a.RxIDB(t.target.result),o.next(n),o.complete(),e===n.idb.version?(s.next(),s.complete()):s.error("Upgrade error")};return i.of(null).pipe(u.map(function(){return indexedDB.open(t,e)}),u.switchMap(function(t){return Object.assign(t,{onerror:f,onblocked:d,onsuccess:l,onupgradeneeded:function(t){return e=t,n=n||new a.RxIDB(e.target.result),void p(new c.RxIDBUpgrade(n,e),r).subscribe(function(){return s.next()},function(t){return s.error(t)},function(){return s.complete()});var e}}),i.forkJoin([o,s]).pipe(u.map(function(){return n}))}))}function p(t,e){return e&&e.length?i.forkJoin(e.map(function(e){return f(t,e)})):i.of(null)}function f(t,e){var r,u;void 0===e&&(e=new Map);var a=t.oldVersion,c=[i.of(!0)];try{for(var s=n(e),p=s.next();!p.done;p=s.next()){var f=o(p.value,2),d=f[0],l=f[1];if(a<d){var h=l(t);h instanceof i.Observable&&c.push(h)}}}catch(t){r={error:t}}finally{try{p&&!p.done&&(u=s.return)&&u.call(s)}finally{if(r)throw r.error}}return c}function d(t){return i.Observable.create(function(e){var r=indexedDB.deleteDatabase(t);r.onblocked=function(t){return e.error(t)},r.onsuccess=function(){return e.next(void 0)},r.onerror=function(t){return e.error(t)}}).pipe(u.take(1))}e.openDB=s,e.onUpgradeNeeded=p,e.setupLayers=f,e.dropDB=d,e.rxidb={openDB:s,dropDB:d}}])});