UNPKG

@manojadams/session-store

Version:

State management with session-storage/local-storage

3 lines (2 loc) 1.47 kB
class s{constructor(s,t){this._storeType=void 0,this._sessionData=void 0,this._name=void 0,this._createdAt=void 0,this._updatedAt=void 0,this.isReady=void 0,this._name=s||this.constructor.name,this._storeType=t,this._sessionData={},this._createdAt=new Date,this._updatedAt=new Date,this.isReady=!1}init(s){const t=(this._storeType||sessionStorage).getItem(this._name);t&&(this._sessionData=JSON.parse(t),this.isReady=!0)}getData(s){return this._sessionData[s]}setData(s,t){this._sessionData[s]=t,this._updatedAt=new Date}_cleanup(){const s=this._sessionData._createdAt||this._createdAt.toISOString(),t=this._updatedAt.toISOString();this._sessionData._createdAt=s,this._sessionData._updatedAt=t}destroy(){this._cleanup(),(this._storeType||sessionStorage).setItem(this._name,JSON.stringify(this._sessionData))}remove(){const s=this._storeType||sessionStorage;Object.keys(this._sessionData).forEach(s=>{this._sessionData[s]=void 0}),s.removeItem(this._name)}}class t extends s{constructor(s,...t){super(s),this.isStoreReady=void 0,this._deps=void 0,this.isStoreReady=!1,this._deps=t}setStoreType(s){this._storeType=s}async init(){if(super.init(),!this.isStoreReady&&this._deps)for(let s=0;s<this._deps.length;s++)this._deps[s].isStoreReady||await this._deps[s].init()}destroy(){if(super.destroy(),this._deps)for(let s=0;s<this._deps.length;s++)this._deps[s].destroy();this.isReady=!1,this.isStoreReady=!1}}export{t as default}; //# sourceMappingURL=index.modern.mjs.map