UNPKG

y-localforage

Version:

a simple Yjs storage provider using localForage for persistence

3 lines (2 loc) 9.77 kB
import*as e from"yjs";import{Observable as t}from"lib0/observable";function r(e,t){return r=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},r(e,t)}function n(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 o(e,t,r){if(!e.s){if(r instanceof i){if(!r.s)return void(r.o=o.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(o.bind(null,e,t),o.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}const i=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(t,r){const n=new e,i=this.s;if(i){const e=1&i?t:r;if(e){try{o(n,1,e(this.v))}catch(e){o(n,2,e)}return n}return this}return this.o=function(e){try{const i=e.v;1&e.s?o(n,1,t?t(i):i):r?o(n,1,r(i)):o(n,2,i)}catch(e){o(n,2,e)}},n},e}();function s(e){return e instanceof i&&1&e.s}function u(e,t,r){for(var n;;){var u=e();if(s(u)&&(u=u.v),!u)return c;if(u.then){n=0;break}var c=r();if(c&&c.then){if(!s(c)){n=1;break}c=c.s}if(t){var a=t();if(a&&a.then&&!s(a)){n=2;break}}}var d=new i,h=o.bind(null,d,2);return(0===n?u.then(_):1===n?c.then(f):a.then(p)).then(void 0,h),d;function f(n){c=n;do{if(t&&(a=t())&&a.then&&!s(a))return void a.then(p).then(void 0,h);if(!(u=e())||s(u)&&!u.v)return void o(d,1,c);if(u.then)return void u.then(_).then(void 0,h);s(c=r())&&(c=c.v)}while(!c||!c.then);c.then(f).then(void 0,h)}function _(e){e?(c=r())&&c.then?c.then(f).then(void 0,h):f(c):o(d,1,c)}function p(){(u=e())?u.then?u.then(_).then(void 0,h):_(u):o(d,1,c)}}function c(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var a=/*#__PURE__*/function(t){function o(e,r,n,o){var i;return void 0===n&&(n=500),(i=t.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=e,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 i,s;s=t,(i=o).prototype=Object.create(s.prototype),i.prototype.constructor=i,r(i,s);var a,d,h=o.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=u(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 t,r=function(e){if(t)return e;n._isBusy=!1,n._enqueuedUpdates.length>0&&n._storeUpdatesAndCompact()},n=this;n._isBusy=!0;var o=c(function(){return n._pendingUpdates=1,Promise.resolve(null==n._SuperProvider?n._StorageKeys():n._StorageSubKeysFor(n._sharedDoc)).then(function(r){return n._pendingUpdates--,function(){if(r.length>0){var o=function(e){n._sharedDoc.emit("load",[n])};n._pendingUpdates+=r.length,n._reportProgress();var i=0,s=r.length,c=u(function(){return i<s},function(){return i++},function(){if(null!=n._Store)return Promise.resolve(n._Store.getItem(r[i])).then(function(t){e.applyUpdate(n._sharedDoc,t,n),n._completedUpdates++,n._reportProgress()});t=1});return c&&c.then?c.then(o):o()}n._reportProgress()}()})},function(e){n._breakdownWith("could not restore document from persistence",e)});return Promise.resolve(o&&o.then?o.then(r):r(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=u(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=c(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=c(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(t){try{var r=this,n=r._enqueuedUpdates.length>0;r._pendingUpdates-=r._enqueuedUpdates.length,r._enqueuedUpdates=[];var o=r._newUpdateKeyAmong(t);return Promise.resolve(r._Store.setItem(o,e.encodeStateAsUpdate(r._sharedDoc))).then(function(){var e;function i(i){if(e)return i;t.splice(0,t.length,o),n&&r._reportProgress()}if(null!=r._Store){var s=0,c=t.length,a=u(function(){return!e&&s<c},function(){return s++},function(){return Promise.resolve(r._Store.removeItem(t[s])).then(function(){null==r._Store&&(e=1)})});return a&&a.then?a.then(i):i(a)}})}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 c(function(){var e=0,o=r.length;return u(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 t=function(){null!=s&&s.forEach(function(e){n(e)})},r=this,n=function(e){if(!r._SubDocMap.has(e)&&r._sharedDoc.guid!==e.guid){var t=new o(r._Store,e,r._UpdateLimit,r);r._SubDocMap.set(e,t)}},i=e.removed,s=e.loaded,c=function(){if(null!=i){var e=Array.from(i.values()),t=0,n=e.length;return u(function(){return t<n},function(){return t++},function(){var n=e[t],o=r._SubDocMap.get(n);null!=o&&o._breakdown(),r._SubDocMap.delete(n);var i=function(){if(null!=r._sharedDoc&&r._sharedDoc.guid!==n.guid&&Array.from(r._sharedDoc.getSubdocs().values()).every(function(e){return e.guid!==n.guid}))return Promise.resolve(r._removeStoredSubDoc(n)).then(function(){})}();if(i&&i.then)return i.then(function(){})})}}();return Promise.resolve(c&&c.then?c.then(t):t())}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)}},a=o,(d=[{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 o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,n(o.key),o)}}(a.prototype,d),Object.defineProperty(a,"prototype",{writable:!1}),a}(t);export{a as LocalForageProvider}; //# sourceMappingURL=y-localforage.esm.js.map