y-localstorage
Version:
a simple Yjs storage provider persisting in localStorage (for educational purposes)
3 lines (2 loc) • 6.15 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("yjs"),require("lib0/observable")):"function"==typeof define&&define.amd?define(["exports","yjs","lib0/observable"],t):t((e||self).yLocalstorage={},e.yjs,e.observable)}(this,function(e,t,o){function r(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(o){if("default"!==o){var r=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(t,o,r.get?r:{enumerable:!0,get:function(){return e[o]}})}}),t.default=e,t}var s=/*#__PURE__*/r(t);function i(e,t){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},i(e,t)}function a(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var o=t.call(e,"string");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}var n=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;e.LocalStorageProvider=/*#__PURE__*/function(e){function t(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(e){s._breakdownWith("could not restore document from persistence",e)}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 o,r;r=e,(o=t).prototype=Object.create(r.prototype),o.prototype.constructor=o,i(o,r);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=[],o=0,r=localStorage.length;o<r;o++){var s=localStorage.key(o);(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,o=this._StorageKeys();o.length>0?(this._pendingUpdates+=o.length,this._reportProgress(),o.forEach(function(o){var r=parseInt(o.slice(t),10);r>e._UpdateCounter&&(e._UpdateCounter=r);var i=new Uint8Array(JSON.parse(localStorage.getItem(o)));s.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(s.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,o=void 0;this._StorageKeys().forEach(function(r){if(parseInt(r.slice(t),10)>=e)try{localStorage.removeItem(r)}catch(e){o=e}}),null!=o&&console.warn("y-localstorage: could not clean-up localstorage, reason: "+o),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 o=this,r=e.removed,s=e.loaded;null!=r&&r.forEach(function(e){var t=o._SubDocMap.get(e);null!=t&&t._breakdown(),o._SubDocMap.delete(e),null!=o._sharedDoc&&o._sharedDoc.guid!==e.guid&&Array.from(o._sharedDoc.getSubdocs().values()).every(function(t){return t.guid!==e.guid})&&o._removeStoredSubDoc(e)}),null!=s&&s.forEach(function(e){!function(e){if(!o._SubDocMap.has(e)&&o._sharedDoc.guid!==e.guid){var r=new t(o._DocPrefix.slice(0,-1)+"."+e.guid,e,o._CounterLimit);o._SubDocMap.set(e,r)}}(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,o=[],r=0,s=localStorage.length;r<s;r++){var i=localStorage.key(r);i.startsWith(e)&&!0===n.test(i.slice(t))&&o.push(i)}return o},u._StorageSubKeysFor=function(e){for(var t=null==e?this._DocPrefix:this._DocPrefix.slice(0,-1)+"."+e.guid+"-",o=t.length,r=[],s=0,i=localStorage.length;s<i;s++){var a=localStorage.key(s);a.startsWith(t)&&!0===/^\d+$/.test(a.slice(o))&&r.push(a)}return r},c=t,(d=[{key:"isSynced",get:function(){return 0===this._pendingUpdates}}])&&function(e,t){for(var o=0;o<t.length;o++){var r=t[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,a(r.key),r)}}(c.prototype,d),Object.defineProperty(c,"prototype",{writable:!1}),c}(o.Observable)});
//# sourceMappingURL=y-localstorage.umd.js.map