UNPKG

y-localforage

Version:

a simple Yjs storage provider using localForage for persistence

3 lines (2 loc) 10 kB
var e=require("yjs"),t=require("lib0/observable");function r(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,t}var n=/*#__PURE__*/r(e);function o(e,t){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},o(e,t)}function i(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function s(e,t,r){if(!e.s){if(r instanceof u){if(!r.s)return void(r.o=s.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(s.bind(null,e,t),s.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}const u=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(t,r){const n=new e,o=this.s;if(o){const e=1&o?t:r;if(e){try{s(n,1,e(this.v))}catch(e){s(n,2,e)}return n}return this}return this.o=function(e){try{const o=e.v;1&e.s?s(n,1,t?t(o):o):r?s(n,1,r(o)):s(n,2,o)}catch(e){s(n,2,e)}},n},e}();function c(e){return e instanceof u&&1&e.s}function a(e,t,r){for(var n;;){var o=e();if(c(o)&&(o=o.v),!o)return i;if(o.then){n=0;break}var i=r();if(i&&i.then){if(!c(i)){n=1;break}i=i.s}if(t){var a=t();if(a&&a.then&&!c(a)){n=2;break}}}var d=new u,h=s.bind(null,d,2);return(0===n?o.then(_):1===n?i.then(f):a.then(p)).then(void 0,h),d;function f(n){i=n;do{if(t&&(a=t())&&a.then&&!c(a))return void a.then(p).then(void 0,h);if(!(o=e())||c(o)&&!o.v)return void s(d,1,i);if(o.then)return void o.then(_).then(void 0,h);c(i=r())&&(i=i.v)}while(!i||!i.then);i.then(f).then(void 0,h)}function _(e){e?(i=r())&&i.then?i.then(f).then(void 0,h):f(i):s(d,1,i)}function p(){(o=e())?o.then?o.then(_).then(void 0,h):_(o):s(d,1,i)}}function d(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}exports.LocalForageProvider=/*#__PURE__*/function(e){function t(t,r,n,o){var i;return void 0===n&&(n=500),(i=e.call(this)||this)._Store=void 0,i._sharedDoc=void 0,i._SuperProvider=void 0,i._isBusy=!1,i._UpdateLimit=500,i._pendingUpdates=0,i._completedUpdates=0,i._enqueuedUpdates=[],i._SubDocMap=new Map,i._Store=t,i._sharedDoc=r,i._SuperProvider=o,i._isBusy=!1,i._UpdateLimit=n,i._storeUpdate=i._storeUpdate.bind(i),r.on("update",i._storeUpdate),i._manageSubDocs=i._manageSubDocs.bind(i),r.on("subdocs",i._manageSubDocs),i.destroy=i.destroy.bind(i),r.on("destroy",i.destroy),i._applyStoredUpdates(),i}var r,s;s=e,(r=t).prototype=Object.create(s.prototype),r.prototype.constructor=r,o(r,s);var u,c,h=t.prototype;return h.SubDocIsSynced=function(e){var t=this._SubDocMap.get(e);return null!=t&&t.isSynced},h.destroy=function(){try{var e=this;return null==e._Store?Promise.resolve():(e._sharedDoc.off("update",e._storeUpdate),e._sharedDoc.off("subdocs",e._manageSubDocs),e._sharedDoc.off("destroy",e.destroy),e.isSynced||(e._pendingUpdates=0,e.emit("sync-aborted",[e,1])),Promise.resolve(null==e._SuperProvider?e._StorageKeys():e._StorageSubKeysFor(e._sharedDoc)).then(function(t){var r=e._Store;e._Store=void 0;var n=0,o=t.length,i=a(function(){return n<o},function(){return n++},function(){return Promise.resolve(r.removeItem(t[n])).then(function(){})});if(i&&i.then)return i.then(function(){})}))}catch(e){return Promise.reject(e)}},h._applyStoredUpdates=function(){try{var e,t=function(t){if(e)return t;r._isBusy=!1,r._enqueuedUpdates.length>0&&r._storeUpdatesAndCompact()},r=this;r._isBusy=!0;var o=d(function(){return r._pendingUpdates=1,Promise.resolve(null==r._SuperProvider?r._StorageKeys():r._StorageSubKeysFor(r._sharedDoc)).then(function(t){return r._pendingUpdates--,function(){if(t.length>0){var o=function(e){r._sharedDoc.emit("load",[r])};r._pendingUpdates+=t.length,r._reportProgress();var i=0,s=t.length,u=a(function(){return i<s},function(){return i++},function(){if(null!=r._Store)return Promise.resolve(r._Store.getItem(t[i])).then(function(e){n.applyUpdate(r._sharedDoc,e,r),r._completedUpdates++,r._reportProgress()});e=1});return u&&u.then?u.then(o):o()}r._reportProgress()}()})},function(e){r._breakdownWith("could not restore document from persistence",e)});return Promise.resolve(o&&o.then?o.then(t):t(o))}catch(e){return Promise.reject(e)}},h._storeUpdate=function(e,t){null!=this._Store&&t!==this&&(this._pendingUpdates++,this._reportProgress(),this._enqueuedUpdates.push(e),this._isBusy||this._storeUpdatesAndCompact())},h._storeUpdatesAndCompact=function(){try{var e=this;return null==e._Store?Promise.resolve():(e._isBusy=!0,Promise.resolve(null==e._SuperProvider?e._StorageKeys():e._StorageSubKeysFor(e._sharedDoc)).then(function(t){var r;function n(t){if(r)return t;e._isBusy=!1}var o=a(function(){return!r&&null!=e._Store&&e._enqueuedUpdates.length>0},void 0,function(){function n(n){if(r)return n;var o=function(){if(t.length>=e._UpdateLimit){var r=d(function(){return Promise.resolve(e._compactUpdates(t)).then(function(){})},function(t){e._breakdownWith("could not compact document updates",t)});if(r&&r.then)return r.then(function(){})}}();return o&&o.then?o.then(function(){}):void 0}var o=d(function(){return Promise.resolve(e._storeNextUpdateAmong(t)).then(function(){null==e._Store&&(r=1)})},function(t){e._breakdownWith("could not persist document update",t)});return o&&o.then?o.then(n):n(o)});return o&&o.then?o.then(n):n(o)}))}catch(e){return Promise.reject(e)}},h._storeNextUpdateAmong=function(e){try{var t=this,r=t._newUpdateKeyAmong(e);return e.push(r),Promise.resolve(t._Store.setItem(r,t._enqueuedUpdates[0])).then(function(){t._enqueuedUpdates.shift(),t._completedUpdates++,t._reportProgress()})}catch(e){return Promise.reject(e)}},h._compactUpdates=function(e){try{var t=this,r=t._enqueuedUpdates.length>0;t._pendingUpdates-=t._enqueuedUpdates.length,t._enqueuedUpdates=[];var o=t._newUpdateKeyAmong(e);return Promise.resolve(t._Store.setItem(o,n.encodeStateAsUpdate(t._sharedDoc))).then(function(){var n;function i(i){if(n)return i;e.splice(0,e.length,o),r&&t._reportProgress()}if(null!=t._Store){var s=0,u=e.length,c=a(function(){return!n&&s<u},function(){return s++},function(){return Promise.resolve(t._Store.removeItem(e[s])).then(function(){null==t._Store&&(n=1)})});return c&&c.then?c.then(i):i(c)}})}catch(e){return Promise.reject(e)}},h._newUpdateKeyAmong=function(e){for(var t=(null==this._SuperProvider?"":this._sharedDoc.guid)+"@"+Date.now(),r=0,n=t+"-"+r;e.indexOf(n)>=0;)n=t+"-"+ ++r;return n},h._removeStoredSubDoc=function(e){try{var t=this;return Promise.resolve(t._StorageSubKeysFor(e)).then(function(r){var n;return d(function(){var e=0,o=r.length;return a(function(){return!n&&e<o},function(){return e++},function(){return Promise.resolve(t._Store.removeItem(r[e])).then(function(){null==t._Store&&(n=1)})})},function(r){t._breakdownWith("could not remove persistence for subdoc "+e.guid,r)})})}catch(e){return Promise.reject(e)}},h._breakdown=function(){this._Store=void 0,this._isBusy=!1,this.isSynced||(this._enqueuedUpdates=[],this._pendingUpdates=0,this.emit("sync-aborted",[this,1])),this._SubDocMap.forEach(function(e){return e._breakdown()})},h._breakdownWith=function(e,t){throw this._breakdown(),new Error(e+(null==t?"":", reason: "+t))},h._manageSubDocs=function(e){try{var r=function(){null!=s&&s.forEach(function(e){o(e)})},n=this,o=function(e){if(!n._SubDocMap.has(e)&&n._sharedDoc.guid!==e.guid){var r=new t(n._Store,e,n._UpdateLimit,n);n._SubDocMap.set(e,r)}},i=e.removed,s=e.loaded,u=function(){if(null!=i){var e=Array.from(i.values()),t=0,r=e.length;return a(function(){return t<r},function(){return t++},function(){var r=e[t],o=n._SubDocMap.get(r);null!=o&&o._breakdown(),n._SubDocMap.delete(r);var i=function(){if(null!=n._sharedDoc&&n._sharedDoc.guid!==r.guid&&Array.from(n._sharedDoc.getSubdocs().values()).every(function(e){return e.guid!==r.guid}))return Promise.resolve(n._removeStoredSubDoc(r)).then(function(){})}();if(i&&i.then)return i.then(function(){})})}}();return Promise.resolve(u&&u.then?u.then(r):r())}catch(e){return Promise.reject(e)}},h._reportProgress=function(){switch(!0){case 0===this._pendingUpdates:this._completedUpdates=0,this.emit("synced",[this]),this._sharedDoc.emit("sync",[this]),null!=this._SuperProvider&&this._SuperProvider.emit("subdoc-synced",[this,this._sharedDoc]);break;case 0===this._completedUpdates&&1===this._pendingUpdates:this.emit("sync-started",[this,0]);break;case this._completedUpdates===this._pendingUpdates:this.emit("sync-finished",[this,1]),this._pendingUpdates=this._completedUpdates=0,this.emit("synced",[this]),this._sharedDoc.emit("sync",[this]),null!=this._SuperProvider&&this._SuperProvider.emit("subdoc-synced",[this,this._sharedDoc]);break;default:this.emit("sync-continued",[this,this._completedUpdates/this._pendingUpdates])}},h._StorageKeys=function(){try{return Promise.resolve(this._Store.keys()).then(function(e){return e.filter(function(e){return e.startsWith("@")})})}catch(e){return Promise.reject(e)}},h._StorageSubKeys=function(){try{return Promise.resolve(this._Store.keys()).then(function(e){return e.filter(function(e){return!e.startsWith("@")})})}catch(e){return Promise.reject(e)}},h._StorageSubKeysFor=function(e){try{var t=e.guid+"@";return Promise.resolve(this._Store.keys()).then(function(e){return e.filter(function(e){return e.startsWith(t)})})}catch(e){return Promise.reject(e)}},u=t,(c=[{key:"isSynced",get:function(){return 0===this._pendingUpdates}},{key:"isFullySynced",get:function(){return 0===this._pendingUpdates&&Array.from(this._SubDocMap.values()).every(function(e){return e.isSynced})}}])&&function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,i(n.key),n)}}(u.prototype,c),Object.defineProperty(u,"prototype",{writable:!1}),u}(t.Observable); //# sourceMappingURL=y-localforage.js.map