UNPKG

y-localstorage

Version:

a simple Yjs storage provider persisting in localStorage (for educational purposes)

3 lines (2 loc) 5.92 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 o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,t}var o=/*#__PURE__*/r(e);function s(e,t){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},s(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+""}var a=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;exports.LocalStorageProvider=/*#__PURE__*/function(e){function t(t,r,o){var s;void 0===o&&(o=5),(s=e.call(this)||this)._DocPrefix=void 0,s._sharedDoc=void 0,s._UpdateCounter=1,s._CounterLimit=5,s._pendingUpdates=0,s._completedUpdates=0,s._SubDocMap=new Map,s._DocPrefix=t+"-",s._sharedDoc=r,s._UpdateCounter=0,s._CounterLimit=o;try{s._applyStoredUpdates()}catch(e){s._breakdownWith("could not restore document from persistence",e)}return s._storeUpdate=s._storeUpdate.bind(s),r.on("update",s._storeUpdate),s._manageSubDocs=s._manageSubDocs.bind(s),r.on("subdocs",s._manageSubDocs),s.destroy=s.destroy.bind(s),r.on("destroy",s.destroy),s}var r,n;n=e,(r=t).prototype=Object.create(n.prototype),r.prototype.constructor=r,s(r,n);var c,d,u=t.prototype;return u.destroy=function(){null!=this._sharedDoc&&(this._removeStoredUpdatesStartingWith(0),this._removeStoredSubDocs(),this._sharedDoc.off("update",this._storeUpdate),this._sharedDoc.off("subdocs",this._manageSubDocs),this._sharedDoc.off("destroy",this.destroy),this.isSynced||(this._pendingUpdates=0,this.emit("sync-aborted",[this,1])),this._sharedDoc=void 0)},t.destroyPersistenceNamed=function(e){for(var t=[],r=0,o=localStorage.length;r<o;r++){var s=localStorage.key(r);(s.startsWith(e+"-")||s.startsWith(e+"."))&&t.push(s)}t.forEach(function(e){localStorage.removeItem(e)})},u._applyStoredUpdates=function(){var e=this,t=this._DocPrefix.length,r=this._StorageKeys();r.length>0?(this._pendingUpdates+=r.length,this._reportProgress(),r.forEach(function(r){var s=parseInt(r.slice(t),10);s>e._UpdateCounter&&(e._UpdateCounter=s);var i=new Uint8Array(JSON.parse(localStorage.getItem(r)));o.applyUpdate(e._sharedDoc,i,e),e._completedUpdates++,e._reportProgress()})):(this._UpdateCounter=1,this._reportProgress())},u._storeUpdate=function(e,t){if(null!=this._sharedDoc&&t!==this){this._pendingUpdates++,this._reportProgress();try{this._UpdateCounter<this._CounterLimit?(localStorage.setItem(this._DocPrefix+this._UpdateCounter,JSON.stringify(Array.from(e))),this._UpdateCounter++):(localStorage.setItem(this._DocPrefix+0,JSON.stringify(Array.from(o.encodeStateAsUpdate(this._sharedDoc)))),this._removeStoredUpdatesStartingWith(1))}catch(e){this._breakdownWith("could not persist document update",e)}this._completedUpdates++,this._reportProgress()}},u._removeStoredUpdatesStartingWith=function(e){var t=this._DocPrefix.length,r=void 0;this._StorageKeys().forEach(function(o){if(parseInt(o.slice(t),10)>=e)try{localStorage.removeItem(o)}catch(e){r=e}}),null!=r&&console.warn("y-localstorage: could not clean-up localstorage, reason: "+r),this._UpdateCounter=e},u._removeStoredSubDocs=function(){this._removeStoredSubDoc()},u._removeStoredSubDoc=function(e){var t=void 0;(null==e?this._StorageSubKeys():this._StorageSubKeysFor(e)).forEach(function(e){try{localStorage.removeItem(e)}catch(e){t=e}}),null!=t&&console.warn("y-localstorage: could not clean-up localstorage, reason: "+t)},u._breakdown=function(){this._sharedDoc=void 0,this.isSynced||(this._pendingUpdates=0,this.emit("sync-aborted",[this,1])),this._SubDocMap.forEach(function(e){return e._breakdown()})},u._breakdownWith=function(e,t){throw this._breakdown(),new Error(e+(null==t?"":", reason: "+t))},u._manageSubDocs=function(e){var r=this,o=e.removed,s=e.loaded;null!=o&&o.forEach(function(e){var t=r._SubDocMap.get(e);null!=t&&t._breakdown(),r._SubDocMap.delete(e),null!=r._sharedDoc&&r._sharedDoc.guid!==e.guid&&Array.from(r._sharedDoc.getSubdocs().values()).every(function(t){return t.guid!==e.guid})&&r._removeStoredSubDoc(e)}),null!=s&&s.forEach(function(e){!function(e){if(!r._SubDocMap.has(e)&&r._sharedDoc.guid!==e.guid){var o=new t(r._DocPrefix.slice(0,-1)+"."+e.guid,e,r._CounterLimit);r._SubDocMap.set(e,o)}}(e)})},u._reportProgress=function(){switch(!0){case 0===this._pendingUpdates:this._completedUpdates=0,this.emit("synced",[this]);break;case 0===this._completedUpdates: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]);break;default:this.emit("sync-continued",[this,this._completedUpdates/this._pendingUpdates])}},u._StorageKeys=function(){return this._StorageSubKeysFor()},u._StorageSubKeys=function(){for(var e=this._DocPrefix.slice(0,-1)+".",t=e.length,r=[],o=0,s=localStorage.length;o<s;o++){var i=localStorage.key(o);i.startsWith(e)&&!0===a.test(i.slice(t))&&r.push(i)}return r},u._StorageSubKeysFor=function(e){for(var t=null==e?this._DocPrefix:this._DocPrefix.slice(0,-1)+"."+e.guid+"-",r=t.length,o=[],s=0,i=localStorage.length;s<i;s++){var a=localStorage.key(s);a.startsWith(t)&&!0===/^\d+$/.test(a.slice(r))&&o.push(a)}return o},c=t,(d=[{key:"isSynced",get:function(){return 0===this._pendingUpdates}}])&&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,i(o.key),o)}}(c.prototype,d),Object.defineProperty(c,"prototype",{writable:!1}),c}(t.Observable); //# sourceMappingURL=y-localstorage.js.map