UNPKG

y-localstorage

Version:

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

3 lines (2 loc) 5.64 kB
import*as t from"yjs";import{Observable as e}from"lib0/observable";function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function r(t){var e=function(t){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var o=e.call(t,"string");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}var s=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,i=/*#__PURE__*/function(e){function i(t,o,r){var s;void 0===r&&(r=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=o,s._UpdateCounter=0,s._CounterLimit=r;try{s._applyStoredUpdates()}catch(t){s._breakdownWith("could not restore document from persistence",t)}return s._storeUpdate=s._storeUpdate.bind(s),o.on("update",s._storeUpdate),s._manageSubDocs=s._manageSubDocs.bind(s),o.on("subdocs",s._manageSubDocs),s.destroy=s.destroy.bind(s),o.on("destroy",s.destroy),s}var a,n;n=e,(a=i).prototype=Object.create(n.prototype),a.prototype.constructor=a,o(a,n);var c,d,h=i.prototype;return h.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)},i.destroyPersistenceNamed=function(t){for(var e=[],o=0,r=localStorage.length;o<r;o++){var s=localStorage.key(o);(s.startsWith(t+"-")||s.startsWith(t+"."))&&e.push(s)}e.forEach(function(t){localStorage.removeItem(t)})},h._applyStoredUpdates=function(){var e=this,o=this._DocPrefix.length,r=this._StorageKeys();r.length>0?(this._pendingUpdates+=r.length,this._reportProgress(),r.forEach(function(r){var s=parseInt(r.slice(o),10);s>e._UpdateCounter&&(e._UpdateCounter=s);var i=new Uint8Array(JSON.parse(localStorage.getItem(r)));t.applyUpdate(e._sharedDoc,i,e),e._completedUpdates++,e._reportProgress()})):(this._UpdateCounter=1,this._reportProgress())},h._storeUpdate=function(e,o){if(null!=this._sharedDoc&&o!==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(t.encodeStateAsUpdate(this._sharedDoc)))),this._removeStoredUpdatesStartingWith(1))}catch(t){this._breakdownWith("could not persist document update",t)}this._completedUpdates++,this._reportProgress()}},h._removeStoredUpdatesStartingWith=function(t){var e=this._DocPrefix.length,o=void 0;this._StorageKeys().forEach(function(r){if(parseInt(r.slice(e),10)>=t)try{localStorage.removeItem(r)}catch(t){o=t}}),null!=o&&console.warn("y-localstorage: could not clean-up localstorage, reason: "+o),this._UpdateCounter=t},h._removeStoredSubDocs=function(){this._removeStoredSubDoc()},h._removeStoredSubDoc=function(t){var e=void 0;(null==t?this._StorageSubKeys():this._StorageSubKeysFor(t)).forEach(function(t){try{localStorage.removeItem(t)}catch(t){e=t}}),null!=e&&console.warn("y-localstorage: could not clean-up localstorage, reason: "+e)},h._breakdown=function(){this._sharedDoc=void 0,this.isSynced||(this._pendingUpdates=0,this.emit("sync-aborted",[this,1])),this._SubDocMap.forEach(function(t){return t._breakdown()})},h._breakdownWith=function(t,e){throw this._breakdown(),new Error(t+(null==e?"":", reason: "+e))},h._manageSubDocs=function(t){var e=this,o=t.removed,r=t.loaded;null!=o&&o.forEach(function(t){var o=e._SubDocMap.get(t);null!=o&&o._breakdown(),e._SubDocMap.delete(t),null!=e._sharedDoc&&e._sharedDoc.guid!==t.guid&&Array.from(e._sharedDoc.getSubdocs().values()).every(function(e){return e.guid!==t.guid})&&e._removeStoredSubDoc(t)}),null!=r&&r.forEach(function(t){!function(t){if(!e._SubDocMap.has(t)&&e._sharedDoc.guid!==t.guid){var o=new i(e._DocPrefix.slice(0,-1)+"."+t.guid,t,e._CounterLimit);e._SubDocMap.set(t,o)}}(t)})},h._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])}},h._StorageKeys=function(){return this._StorageSubKeysFor()},h._StorageSubKeys=function(){for(var t=this._DocPrefix.slice(0,-1)+".",e=t.length,o=[],r=0,i=localStorage.length;r<i;r++){var a=localStorage.key(r);a.startsWith(t)&&!0===s.test(a.slice(e))&&o.push(a)}return o},h._StorageSubKeysFor=function(t){for(var e=null==t?this._DocPrefix:this._DocPrefix.slice(0,-1)+"."+t.guid+"-",o=e.length,r=[],s=0,i=localStorage.length;s<i;s++){var a=localStorage.key(s);a.startsWith(e)&&!0===/^\d+$/.test(a.slice(o))&&r.push(a)}return r},c=i,(d=[{key:"isSynced",get:function(){return 0===this._pendingUpdates}}])&&function(t,e){for(var o=0;o<e.length;o++){var s=e[o];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,r(s.key),s)}}(c.prototype,d),Object.defineProperty(c,"prototype",{writable:!1}),c}(e);export{i as LocalStorageProvider}; //# sourceMappingURL=y-localstorage.esm.js.map