UNPKG

synapse-storage

Version:

Набор инструментов для управления состоянием и апи-запросами

2 lines (1 loc) 42.5 kB
var f=!1,x=new Map;function U(c){let e=0;if(c.length===0)return e.toString(36);for(let t=0;t<c.length;t++){let r=c.charCodeAt(t);e=(e<<5)-e+r,e=e&e}return Math.abs(e).toString(36).substring(0,6)}function P(c,e){if(c===e)return!0;if(c==null||e==null)return!1;if(typeof c!="object"&&typeof c!="function"&&typeof e!="object"&&typeof e!="function")return c===e;if(typeof c!=typeof e)return!1;if(c instanceof Date&&e instanceof Date)return c.getTime()===e.getTime();if(Array.isArray(c)&&Array.isArray(e)){if(c.length!==e.length)return!1;for(let t=0;t<c.length;t++)if(!P(c[t],e[t]))return!1;return!0}if(typeof c=="object"&&typeof e=="object"){let t=Object.keys(c),r=Object.keys(e);return t.length!==r.length?!1:t.every(s=>Object.prototype.hasOwnProperty.call(e,s)?P(c[s],e[s]):!1)}return!1}function G(c,e=P){let t,r,s=!1;return function(a){if(!s||t!==a){let o=c(a);(!s||!e(o,r))&&(r=o),t=a,s=!0}return r}}var R=class{constructor(e,t,r=P,s){this.name=e;this.equals=r;this.logger=s;this.id=e,this.memoizedGetState=this.createMemoizedGetState(t)}id;subscribers=new Set;lastValue;memoizedGetState;createMemoizedGetState(e){let t=null,r=!1;return async()=>{if(r&&t)return t;r=!0;try{return t=e(),await t}finally{r=!1}}}async notify(){try{let e=await this.memoizedGetState();if(this.lastValue===void 0||!this.equals(e,this.lastValue)){this.lastValue=e;let t=Array.from(this.subscribers).map(async r=>{try{await r.notify(e)}catch(s){this.logger?.error(`[${this.id}] \u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u0443\u0432\u0435\u0434\u043E\u043C\u043B\u0435\u043D\u0438\u0438 \u043F\u043E\u0434\u043F\u0438\u0441\u0447\u0438\u043A\u0430`,{error:s})}});await Promise.all(t)}}catch(e){throw this.logger?.error(`[${this.id}] \u041E\u0448\u0438\u0431\u043A\u0430 \u0432 notify()`,{error:e}),e}}subscribe(e){return this.subscribers.add(e),this.lastValue!==void 0?Promise.resolve().then(()=>{try{e.notify(this.lastValue)}catch(t){this.logger?.error(`[${this.id}] \u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u0435\u0440\u0432\u043E\u043D\u0430\u0447\u0430\u043B\u044C\u043D\u043E\u043C \u0443\u0432\u0435\u0434\u043E\u043C\u043B\u0435\u043D\u0438\u0438`,{error:t})}}):this.notify().catch(t=>{this.logger?.error(`[${this.id}] \u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u0435\u0440\u0432\u043E\u043D\u0430\u0447\u0430\u043B\u044C\u043D\u043E\u043C \u0443\u0432\u0435\u0434\u043E\u043C\u043B\u0435\u043D\u0438\u0438`,{error:t})}),()=>{this.subscribers.delete(e)}}cleanup(){this.subscribers.clear(),this.lastValue=void 0}getId(){return this.id}},z=class{constructor(e,t){this.source=e;this.logger=t;this.storageName=e.name,this.source.getState().then(r=>{this.cachedState=r})}storageName;subscriptions=new Map;cachedState;localSelectorCache=new Map;batchUpdateInProgress=!1;pendingUpdates=new Set;generateName(e,t,r){let s=e?"simple":"combined",i="";if(e){let a=t.toString();i=U(a)}else{let a=t.map(n=>n.getId()).join("_"),o=r.toString();i=U(a+o)}return`${this.storageName}_${s}_${i}`}processPendingUpdates(){this.pendingUpdates.size===0||this.batchUpdateInProgress||(this.batchUpdateInProgress=!0,setTimeout(async()=>{try{let e=Array.from(this.pendingUpdates);this.pendingUpdates.clear(),this.cachedState=await this.source.getState();let t=e.map(async r=>{let s=this.subscriptions.get(r);if(s)try{return await s.notify()}catch(i){this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0443\u0432\u0435\u0434\u043E\u043C\u043B\u0435\u043D\u0438\u044F \u043F\u043E\u0434\u043F\u0438\u0441\u0447\u0438\u043A\u0430 ${r}`,{error:i})}return Promise.resolve()});await Promise.all(t)}catch(e){this.logger?.error("\u041E\u0448\u0438\u0431\u043A\u0430 \u043E\u0431\u0440\u0430\u0431\u043E\u0442\u043A\u0438 \u043E\u0436\u0438\u0434\u0430\u044E\u0449\u0438\u0445 \u043E\u0431\u043D\u043E\u0432\u043B\u0435\u043D\u0438\u0439",{error:e})}finally{this.batchUpdateInProgress=!1,this.pendingUpdates.size>0&&this.processPendingUpdates()}},0))}createSelector(e,t,r){let s=!Array.isArray(e),i=s?t||{}:r||{},a=i.name||this.generateName(s,e,s?void 0:t);if(this.localSelectorCache.has(a))return this.localSelectorCache.get(a).api;if(x.has(a)){let d=x.get(a);return d.refCount++,d.api}let o,n,l=[];if(s){let d=G(e,i.equals||P),g=this.createSimpleSelector(d,{...i,name:a,equals:i.equals||P});o=g.api,l=g.unsubscribeFunctions}else{n=e;let d=this.createCombinedSelector(n,t,{...i,name:a,equals:i.equals||P});o=d.api,l=d.unsubscribeFunctions}return this.localSelectorCache.set(a,{api:o,dependencies:n,unsubscribeFunctions:l}),x.set(a,{api:o,refCount:1,unsubscribeFunctions:l}),o}createSimpleSelector(e,t){let r=async()=>{if(this.cachedState)return e(this.cachedState);let n=await this.source.getState();return this.cachedState=n,e(n)},s=new R(t.name,r,t.equals||P,this.logger),i=s.getId();this.subscriptions.set(i,s);let o=[this.source.subscribeToAll(async n=>{n?.type==="storage:update"&&(this.pendingUpdates.add(i),this.processPendingUpdates())})];return{api:{select:()=>r(),subscribe:n=>s.subscribe(n),getId:()=>i},unsubscribeFunctions:o}}createCombinedSelector(e,t,r){let s=G(g=>t(...g),r.equals||P),i=async()=>{let g=await Promise.all(e.map(u=>u.select()));return s(g)},a=new R(r.name,i,r.equals||P,this.logger),o=a.getId();this.subscriptions.set(o,a);let n=null,l=()=>{n!==null&&clearTimeout(n),n=setTimeout(()=>{n=null,a.notify().catch(g=>this.logger?.error(`[${o}] \u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043E\u0431\u044A\u0435\u0434\u0438\u043D\u0435\u043D\u043D\u043E\u043C \u0443\u0432\u0435\u0434\u043E\u043C\u043B\u0435\u043D\u0438\u0438:`,{error:g}))},10)},d=e.map(g=>g.subscribe({notify:()=>{l()}}));return{api:{select:()=>i(),subscribe:g=>a.subscribe(g),getId:()=>o},unsubscribeFunctions:d}}destroy(){this.subscriptions.forEach(t=>t.cleanup()),this.subscriptions.clear(),this.cachedState=void 0,this.pendingUpdates.clear(),this.localSelectorCache.forEach(t=>{t.unsubscribeFunctions.forEach(r=>r())});let e=new Set;this.localSelectorCache.forEach((t,r)=>{e.add(r)}),this.localSelectorCache.clear(),e.forEach(t=>{let r=x.get(t);r&&(r.refCount--,r.refCount<=0&&(r.unsubscribeFunctions.forEach(s=>s()),x.delete(t)))})}};var j=class{constructor(e,t,r="default"){this.parentExecutor=e;this.logger=t;this.storageName=r}plugins=new Map;createContext(e){return{storageName:this.storageName,timestamp:Date.now(),metadata:e}}async add(e){if(this.plugins.has(e.name)){this.logger?.warn(`\u041F\u043B\u0430\u0433\u0438\u043D ${e.name} \u0443\u0436\u0435 \u0431\u044B\u043B \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043E\u0432\u0430\u043D`);return}try{await e.initialize?.(),this.plugins.set(e.name,e),this.logger?.info("\u041F\u043B\u0430\u0433\u0438\u043D \u0434\u043E\u0431\u0430\u0432\u043B\u0435\u043D",{name:e.name})}catch(t){throw t}}async remove(e){let t=this.plugins.get(e);t&&(await t.destroy?.(),this.plugins.delete(e),this.logger?.info("\u041F\u043B\u0430\u0433\u0438\u043D \u0443\u0434\u0430\u043B\u0435\u043D",{name:e}))}get(e){return this.plugins.get(e)}getAll(){return Array.from(this.plugins.values())}async initialize(){for(let e of this.plugins.values())await e.initialize?.()}async destroy(){await Promise.all(Array.from(this.plugins.values()).map(e=>e.destroy?.()??Promise.resolve())),this.plugins.clear()}async executeBeforeSet(e,t){let r=e,s=this.createContext(t);this.parentExecutor&&(r=await this.parentExecutor.executeBeforeSet(r,s));for(let i of this.plugins.values())if(i.onBeforeSet)try{r=await i.onBeforeSet(r,s)}catch(a){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043B\u0430\u0433\u0438\u043D\u0435 ${i.name} onBeforeSet`,{error:a}),a}return r}async executeAfterSet(e,t,r){let s=t,i=this.createContext(r);this.parentExecutor&&(s=await this.parentExecutor.executeAfterSet(e,s,i));for(let a of this.plugins.values())if(a.onAfterSet)try{s=await a.onAfterSet(e,s,i)}catch(o){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043B\u0430\u0433\u0438\u043D\u0435 ${a.name} onAfterSet`,{key:e,error:o}),o}return s}async executeBeforeGet(e,t){let r=e,s=this.createContext(t);this.parentExecutor&&(r=await this.parentExecutor.executeBeforeGet(r,s));for(let i of this.plugins.values())if(i.onBeforeGet)try{r=await i.onBeforeGet(r,s)}catch(a){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043B\u0430\u0433\u0438\u043D\u0435 ${i.name} onBeforeGet`,{key:e,error:a}),a}return r}async executeAfterGet(e,t,r){let s=t,i=this.createContext(r);this.parentExecutor&&(s=await this.parentExecutor.executeAfterGet(e,s,i));for(let a of this.plugins.values())if(a.onAfterGet)try{s=await a.onAfterGet(e,s,i)}catch(o){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043B\u0430\u0433\u0438\u043D\u0435 ${a.name} onAfterGet`,{key:e,error:o}),o}return s}async executeBeforeDelete(e,t){let r=!0,s=this.createContext(t);this.parentExecutor&&(r=await this.parentExecutor.executeBeforeDelete(e,s));for(let i of this.plugins.values())if(i.onBeforeDelete)try{r=await i.onBeforeDelete(e,s)&&r}catch(a){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043B\u0430\u0433\u0438\u043D\u0435 ${i.name} onBeforeDelete`,{key:e,error:a}),a}return r}async executeAfterDelete(e,t){let r=this.createContext(t);this.parentExecutor&&await this.parentExecutor.executeAfterDelete(e,r);for(let s of this.plugins.values())if(s.onAfterDelete)try{await s.onAfterDelete(e,r)}catch(i){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043B\u0430\u0433\u0438\u043D\u0435 ${s.name} onAfterDelete`,{key:e,error:i}),i}}async executeOnClear(e){let t=this.createContext(e);this.parentExecutor&&await this.parentExecutor.executeOnClear(t);for(let r of this.plugins.values())if(r.onClear)try{await r.onClear(t)}catch(s){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043B\u0430\u0433\u0438\u043D\u0435 ${r.name} onClear`,{error:s}),s}}};var _=(s=>(s.IDLE="idle",s.LOADING="loading",s.READY="ready",s.ERROR="error",s))(_||{}),C=(a=>(a.STORAGE_UPDATE="storage:update",a.STORAGE_DELETE="storage:delete",a.STORAGE_PATCH="storage:patch",a.STORAGE_SELECT="storage:select",a.STORAGE_CLEAR="storage:clear",a.STORAGE_DESTROY="storage:destroy",a))(C||{});var M=class{channel;tabId;messageHandlers;syncHandler;debug;syncTimeoutMs=1e3;pendingSyncRequests;constructor(e,t={}){this.channel=new BroadcastChannel(e),this.tabId=crypto.randomUUID(),this.messageHandlers=new Set,this.debug=t.debug??!1,this.pendingSyncRequests=new Map,this.channel.onmessage=this.handleMessage.bind(this),this.channel.onmessageerror=this.handleError.bind(this)}log(...e){this.debug}error(...e){}async handleMessage(e){let t=e.data;if(t.senderId!==this.tabId){if(t.type==="SYNC_REQUEST"){if(this.syncHandler)try{let r=await this.syncHandler();this.postMessage("SYNC_RESPONSE",r,t.senderId)}catch(r){this.error("Error handling sync request:",r)}return}if(t.type==="SYNC_RESPONSE"){let r=this.pendingSyncRequests.get(this.tabId);r&&(clearTimeout(r.timeout),this.pendingSyncRequests.delete(this.tabId),r.resolve(t.payload));return}for(let r of this.messageHandlers)try{await r(t)}catch(s){this.error("Error in message handler:",s)}}}handleError(e){this.error("Channel error:",e)}postMessage(e,t,r){let s={type:e,payload:t,senderId:this.tabId,timestamp:Date.now()};this.channel.postMessage(s)}subscribe(e){return this.messageHandlers.add(e),()=>this.messageHandlers.delete(e)}setSyncHandler(e){this.syncHandler=e}broadcast(e,t){this.postMessage(e,t)}async requestSync(){return new Promise((e,t)=>{let r=setTimeout(()=>{this.pendingSyncRequests.delete(this.tabId),e(null)},this.syncTimeoutMs);this.pendingSyncRequests.set(this.tabId,{resolve:e,reject:t,timeout:r}),this.postMessage("SYNC_REQUEST",{type:"sync"})})}close(){for(let[,e]of this.pendingSyncRequests)clearTimeout(e.timeout),e.reject(new Error("Channel closed"));this.pendingSyncRequests.clear(),this.messageHandlers.clear(),this.syncHandler=void 0,this.channel.close()}};async function H(c,e,t){switch(e){case"set":t?.key!==void 0&&t?.value!==void 0&&(await c.storage.doSet(t.key,t.value),c.storage.notifySubscribers(t.key,t.value));break;case"update":Array.isArray(t?.value)&&(await c.storage.doUpdate(t.value),t.value.forEach(({key:r,value:s})=>{c.storage.notifySubscribers(r,s)}));break;case"delete":t?.key!==void 0&&(await c.storage.doDelete(t.key),c.storage.notifySubscribers(t.key,void 0));break;case"clear":await c.storage.doClear(),c.storage.notifySubscribers("*",{type:"storage:update",value:{},source:"broadcast"});break}c.storage.notifySubscribers("*",{type:"storage:update",key:t?.key,value:t?.value,source:"broadcast"})}async function Y(c,e,t){switch(e){case"set":if(t?.key!==void 0){let r=await c.storage.doGet(t.key);c.storage.notifySubscribers(t.key,r)}break;case"update":if(Array.isArray(t?.value)){for(let{key:r}of t.value){let s=await c.storage.doGet(r);c.storage.notifySubscribers(r,s)}c.storage.notifySubscribers("*",{type:"storage:update",key:t.value.map(({key:r})=>r),value:t.value,source:"broadcast"})}break;case"delete":t?.key!==void 0&&c.storage.notifySubscribers(t.key,void 0);break;case"clear":c.storage.notifySubscribers("*",{type:"storage:update",value:{},source:"broadcast"});break}e!=="update"&&c.storage.notifySubscribers("*",{type:"storage:update",key:t?.key,value:e==="delete"?void 0:t?.value,source:"broadcast"})}var q=c=>{let{storageName:e,storageType:t}=c,r=`${t}-${e}`,s=new M(r,{debug:!0});return{name:"broadcast",setup:i=>(t==="memory"&&(s.setSyncHandler(async()=>{let a=await i.getState();return{type:"update",key:"*",value:Object.entries(a).map(([l,d])=>({key:l,value:d})),metadata:{batchUpdate:!0,timestamp:Date.now()}}}),s.requestSync().then(async a=>{if(a?.type==="update"&&Array.isArray(a.value))try{if(!a.value.every(n=>n&&typeof n=="object"&&"key"in n&&"value"in n))return;await i.storage.doUpdate(a.value),a.value.forEach(({key:n,value:l})=>{i.storage.notifySubscribers(n,l)}),i.storage.notifySubscribers("*",{type:"storage:update",value:a.value,source:"broadcast"})}catch{}})),s.subscribe(async a=>{let{type:o,payload:n}=a;t==="memory"?await H(i,o,n):await Y(i,o,n)})),reducer:i=>a=>async o=>{let n=await a(o);return["set","delete","clear","update"].includes(o.type)&&s.broadcast(o.type,o),n},cleanup:()=>{s.close()}}};var y=class{constructor(e,t=!1){this.value=e;this.isRawKey=t}toString(){return this.value}toJSON(){return this.value}valueOf(){return this.value}isUnparseable(){return this.isRawKey}};var L=(c={})=>{let e=c.batchSize??10,t=c.batchDelay??10,r=new Map,s=new Map,i=u=>u.type==="set"||u.type==="update",a=u=>`${u.type}_${u.key?.toString()||"default"}`,o=u=>{let h=new Map;for(let p of u){let S=`${p.type}_${p.key?.toString()||"default"}`;h.set(S,p)}return Array.from(h.values())},n=u=>{let h=s.get(u);h&&(globalThis.clearTimeout(h),s.delete(u))},l=(u,h)=>{let p=globalThis.setTimeout(h,t);s.set(u,p)},d=async(u,h,p)=>{let S=r.get(u);if(!(!S||S.length===0)){r.delete(u),n(u);try{let I=S.map(m=>m.action),v=o(I);for(let m of v)try{let O=await p(m);S.filter(T=>T.action.type===m.type&&T.action.key?.toString()===m.key?.toString()).forEach(T=>T.resolve(O))}catch(O){S.filter(T=>T.action.type===m.type&&T.action.key?.toString()===m.key?.toString()).forEach(T=>T.reject(O))}}catch(I){S.forEach(v=>v.reject(I))}}},g=async(u,h,p)=>new Promise((S,I)=>{let v=a(u),m=r.get(v);m||(m=[],r.set(v,m)),m.push({action:u,resolve:S,reject:I,timestamp:Date.now()}),n(v),m.length>=e?setImmediate(()=>d(v,h,p)):l(v,()=>d(v,h,p))});return{name:"batching",setup:()=>{},cleanup:async()=>{s.forEach(u=>globalThis.clearTimeout(u)),s.clear(),r.clear()},reducer:u=>h=>async p=>i(p)?g(p,u,h):h(p)}};var F=(c={})=>{let{comparator:e=(s,i)=>{if(s===i)return!0;if(typeof s!="object"||typeof i!="object"||s===null||i===null)return s===i;let a=Object.keys(s),o=Object.keys(i);return a.length!==o.length?!1:a.every(n=>Object.prototype.hasOwnProperty.call(i,n)&&s[n]===i[n])},segments:t=[]}=c,r=new Map;return{name:"shallow-compare",setup:s=>{},reducer:s=>i=>async a=>{if(a.type!=="set"||t.length&&!t.includes(a.metadata?.segment??"default"))return i(a);let o=a.key,n=r.get(o),l=a.value;if(n!==void 0&&e(n,l))return{...n,t:{valueNotChanged:!0,originalValue:n}};let d=await i(a);return r.set(o,l),d}}};var D=class{middlewares=[];api;initialized=!1;dispatchFn;constructor(e){this.api={dispatch:async t=>this.dispatch(t),getState:()=>e.getState(),storage:{doGet:e.doGet.bind(e),doSet:e.doSet.bind(e),doUpdate:e.doUpdate.bind(e),doDelete:e.doDelete.bind(e),doClear:e.doClear.bind(e),doKeys:e.doKeys.bind(e),notifySubscribers:e.notifySubscribers.bind(e)}}}async baseOperation(e){let{processed:t,...r}=e.metadata||{},s={...e,metadata:r};switch(s.type){case"get":return this.api.storage.doGet(s.key);case"set":return await this.api.storage.doSet(s.key,s.value),this.api.storage.doGet(s.key);case"update":return Array.isArray(s.value)?(await this.api.storage.doUpdate(s.value),this.api.storage.doGet("")):s.value;case"delete":return this.api.storage.doDelete(s.key);case"clear":return this.api.storage.doClear();case"init":{let i=await this.api.storage.doGet("");return Object.keys(i||{}).length>0?i:s.value?(await this.api.storage.doSet("",s.value),this.api.storage.doGet("")):i}case"keys":return this.api.storage.doKeys();default:throw new Error(`Unknown action type: ${s.type}`)}}initializeMiddlewares(){if(this.initialized)return;let e=this.baseOperation.bind(this);for(let t of[...this.middlewares].reverse()){let r=e;e=async s=>{if(s.metadata?.processed)return r(s);let i={...s,metadata:{...s.metadata,processed:!0,timestamp:s.metadata?.timestamp||Date.now()}};return t.reducer(this.api)(r)(i)}}this.dispatchFn=e,this.initialized=!0}use(e){e.setup&&e.setup(this.api),this.middlewares.push(e),this.initialized=!1}async dispatch(e){this.initialized||this.initializeMiddlewares();try{return this.dispatchFn(e)}catch(t){throw t}}};function b(c){return c instanceof y&&c.isUnparseable()?[c.toString()]:c.toString().replace(/\[/g,".").replace(/\]/g,"").split(".").filter(Boolean)}function w(c,e){return b(e).reduce((r,s)=>r===void 0?void 0:r[s],c)}function E(c,e,t){if(e==="")return t;let r=b(e);if(e instanceof y&&e.isUnparseable())return c[e.toString()]=t,c;let s=r.pop(),i=r.reduce((a,o)=>{let n=r[r.indexOf(o)+1],l=!Number.isNaN(Number(n));return o in a||(a[o]=l?[]:{}),a[o]},c);return i[s]=t,c}var A=class c{constructor(e,t,r,s){this.config=e;this.pluginExecutor=t;this.eventEmitter=r;this.logger=s;this.name=e.name,this.middlewareModule=new D({getState:this.getState.bind(this),doGet:this.doGet.bind(this),doSet:this.doSet.bind(this),doUpdate:this.doUpdate.bind(this),doDelete:this.doDelete.bind(this),doClear:this.doClear.bind(this),doKeys:this.doKeys.bind(this),notifySubscribers:this.notifySubscribers.bind(this),pluginExecutor:this.pluginExecutor,eventEmitter:this.eventEmitter,logger:this.logger}),this.initializeMiddlewares()}static GLOBAL_SUBSCRIPTION_KEY="*";name;e={status:"idle"};statusSubscribers=new Set;selectorPathCache=new WeakMap;middlewareModule;initializedMiddlewares=null;subscribers=new Map;get initStatus(){return{...this.e}}async waitForReady(){if(this.e.status==="ready")return this;if(this.e.status==="error")throw this.e.error||new Error("Storage initialization failed");return new Promise((e,t)=>{let r=this.onStatusChange(s=>{s.status==="ready"?(r(),e(this)):s.status==="error"&&(r(),t(s.error||new Error("Storage initialization failed")))})})}onStatusChange(e){return this.statusSubscribers.add(e),e(this.initStatus),()=>{this.statusSubscribers.delete(e)}}updateInitStatus(e){let t=this.e.status;this.e={...this.e,...e},t!==this.e.status&&this.logger?.debug(`Storage "${this.name}" status changed: ${t} -> ${this.e.status}`);let r=this.initStatus;this.statusSubscribers.forEach(s=>{try{s(r)}catch(i){this.logger?.error("Error in status change callback",{error:i})}})}async initialize(){if(this.e.status==="ready")return this;if(this.e.status==="loading")return this.waitForReady();this.updateInitStatus({status:"loading",error:void 0});try{let e=await this.doInitialize();return this.updateInitStatus({status:"ready",error:void 0}),e}catch(e){throw this.updateInitStatus({status:"error",error:e instanceof Error?e:new Error(String(e))}),e}}ensureReady(){if(this.e.status!=="ready")throw new Error(`Storage "${this.name}" is not ready. Current status: ${this.e.status}`)}initializeMiddlewares(){this.config.middlewares&&!this.initializedMiddlewares&&(this.initializedMiddlewares=this.config.middlewares(()=>this.getDefaultMiddleware()),this.initializedMiddlewares.forEach(e=>this.middlewareModule.use(e)))}getDefaultMiddleware(){return{batching:(e={})=>L(e),shallowCompare:(e={})=>F(e)}}async initializeWithMiddlewares(){try{let e=await this.getState();!(Object.keys(e).length>0)&&this.config.initialState&&await this.middlewareModule.dispatch({type:"init",value:this.config.initialState})}catch(e){throw this.logger?.error("\u041E\u0448\u0438\u0431\u043A\u0430 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0430\u0446\u0438\u0438 \u0445\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0430",{error:e}),e}}async get(e){this.ensureReady();try{let t={operation:"get",timestamp:Date.now(),key:e},r=await this.middlewareModule.dispatch({type:"get",key:e,metadata:t}),s=await this.pluginExecutor?.executeAfterGet(e,r,t)??r;return await this.emitEvent({type:"storage:select",payload:{key:e,value:s}}),s}catch(t){throw this.logger?.error("Error getting value",{key:e,error:t}),t}}async set(e,t){this.ensureReady();try{let r={operation:"set",timestamp:Date.now(),key:e},s=await this.pluginExecutor?.executeBeforeSet(t,r)??t,i=await this.middlewareModule.dispatch({type:"set",key:e,value:s,metadata:r}),a=i?.t?.valueNotChanged===!0,o;if(a&&i?.t?.originalValue!==void 0?o=i.t.originalValue:o=await this.pluginExecutor?.executeAfterSet(e,i,r)??i,!a){let l=[e.toString()];this.notifySubscribers(e,o),this.notifySubscribers(c.GLOBAL_SUBSCRIPTION_KEY,{type:"storage:update",key:e,value:o,changedPaths:l}),await this.emitEvent({type:"storage:update",payload:{key:e,value:o,changedPaths:l}})}}catch(r){throw this.logger?.error("Error setting value",{key:e,error:r}),r}}async update(e){this.ensureReady();try{let t={operation:"update",timestamp:Date.now()},r=await this.getState(),s=structuredClone(r);e(s);let i=this.findChangedPaths(r,s);if(i.size===0){this.logger?.debug&&this.logger.debug("No changes detected in update");return}this.logger?.debug&&this.logger.debug("Changed paths:",{paths:Array.from(i)});let a=new Set;for(let u of i){let h=u.split(".")[0];a.add(h)}let o=await Promise.all(Array.from(a).map(async u=>{let h={...t,key:u},p=await this.pluginExecutor?.executeBeforeSet(s[u],h)??s[u];return{key:u,value:p}})),n=await this.middlewareModule.dispatch({type:"update",value:o,metadata:{...t,batchUpdate:!0,changedPaths:Array.from(i)}}),l={};Array.isArray(n)?n.forEach(u=>{u&&typeof u=="object"&&"key"in u&&"value"in u&&(l[u.key]=u.value)}):n&&typeof n=="object"&&(l={...n});let d=Object.keys(l).filter(u=>!this.isEqual(r[u],l[u]));if(d.length===0){this.logger?.debug&&this.logger.debug("No actual changes after middleware processing");return}let g={};d.forEach(u=>{g[u]=l[u]}),this.logger?.debug&&this.logger.debug("Notifying subscribers about changes:",{keys:d}),this.notifySubscribers(c.GLOBAL_SUBSCRIPTION_KEY,{type:"storage:update",key:d,value:g,changedPaths:Array.from(i)});for(let u of i)try{let h=u.split(".")[0];if(h in g){let p;if(u===h)p=g[h];else{let S=u.substring(h.length+1);p=w(g[h],S)}p!==void 0&&this.notifySubscribers(u,p)}}catch(h){this.logger?.error("Error notifying path subscribers",{path:u,error:h})}await this.emitEvent({type:"storage:update",payload:{state:g,key:d,changedPaths:Array.from(i)}})}catch(t){throw this.logger?.error("Error updating state",{error:t}),t}}async delete(e){this.ensureReady();try{let t={operation:"delete",timestamp:Date.now(),key:e};if(await this.pluginExecutor?.executeBeforeDelete(e,t)){let r=await this.middlewareModule.dispatch({type:"delete",key:e,metadata:t});await this.pluginExecutor?.executeAfterDelete(e,t);let i=[e.toString()];this.notifySubscribers(e,void 0),this.notifySubscribers(c.GLOBAL_SUBSCRIPTION_KEY,{type:"storage:update",key:e,value:void 0,result:r,changedPaths:i}),await this.emitEvent({type:"storage:update",payload:{key:e,value:void 0,result:r,changedPaths:i}})}}catch(t){throw this.logger?.error("Error deleting value",{key:e,error:t}),t}}async clear(){this.ensureReady();try{this.pluginExecutor?.executeOnClear(),await this.middlewareModule.dispatch({type:"clear"})}catch(e){throw this.logger?.error("Error clearing storage",{error:e}),e}}async keys(){this.ensureReady();try{return await this.middlewareModule.dispatch({type:"keys"})}catch(e){throw this.logger?.error("Error getting keys",{error:e}),e}}async has(e){this.ensureReady();try{return await this.doHas(e)}catch(t){throw this.logger?.error("Error checking value existence",{key:e,error:t}),t}}async getState(){try{return await this.doGet("")||{}}catch(e){throw this.logger?.error("Error getting state",{error:e}),e}}subscribeToAll(e){return this.subscribers.has(c.GLOBAL_SUBSCRIPTION_KEY)||this.subscribers.set(c.GLOBAL_SUBSCRIPTION_KEY,new Set),this.subscribers.get(c.GLOBAL_SUBSCRIPTION_KEY).add(e),()=>{let t=this.subscribers.get(c.GLOBAL_SUBSCRIPTION_KEY);t&&(t.delete(e),t.size===0&&this.subscribers.delete(c.GLOBAL_SUBSCRIPTION_KEY))}}subscribe(e,t){return typeof e=="string"?this.subscribeByKey(e,t):this.subscribeBySelector(e,t)}async destroy(){try{await this.clear(),await this.doDestroy(),this.initializedMiddlewares&&(await Promise.all(this.initializedMiddlewares.map(async e=>{"cleanup"in e&&await e.cleanup?.()})),this.initializedMiddlewares=null),this.statusSubscribers.clear(),this.updateInitStatus({status:"idle"}),await this.emitEvent({type:"storage:destroy"})}catch(e){throw this.logger?.error("Error destroying storage",{error:e}),e}}subscribeByKey(e,t){this.subscribers.has(e)||this.subscribers.set(e,new Set);let r=!1;return this.subscribers.get(e).add(t),this.get(e).then(s=>{try{r||(r=!0,t(s))}catch(i){this.logger?.error("Error in initial callback",{key:e,error:i})}}),()=>{let s=this.subscribers.get(e);s&&(s.delete(t),s.size===0&&this.subscribers.delete(e))}}createDummyState(){let e={get:(t,r)=>(t[r]=t[r]||new Proxy({},e),t[r])};return new Proxy({},e)}isEqual(e,t){if(e===t)return!0;if(e==null||t==null)return e===t;let r=typeof e;if(r!==typeof t)return!1;if(r!=="object")return e===t;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let o=0;o<e.length;o++)if(!this.isEqual(e[o],t[o]))return!1;return!0}let i=Object.keys(e),a=Object.keys(t);return i.length!==a.length?!1:i.every(o=>Object.prototype.hasOwnProperty.call(t,o)&&this.isEqual(e[o],t[o]))}extractPath(e,t){if(this.selectorPathCache.has(e))return this.selectorPathCache.get(e);let r=[],s=(i="")=>({get:(a,o)=>{if(typeof o=="symbol")return Reflect.get(a,o);let n=i?`${i}.${o}`:o;return r.push(n),new Proxy({},s(n))},has:(a,o)=>!0,ownKeys:()=>[],getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0}),apply:(a,o,n)=>new Proxy(()=>{},s(i))});try{e(new Proxy(t,s()))}catch{}return r.length===0?"":(r.sort((i,a)=>a.length-i.length),this.selectorPathCache.set(e,r[0]),r[0])}notifySubscribers(e,t){let r=e.toString(),s=this.subscribers.get(r);s?.size&&new Set(s).forEach(a=>{try{a(t)}catch(o){this.logger?.error("\u041E\u0448\u0438\u0431\u043A\u0430 \u0432 \u043F\u043E\u0434\u043F\u0438\u0441\u0447\u0438\u043A\u0435 \u043D\u0430 \u043A\u043E\u043B\u0431\u044D\u043A",{key:r,error:o})}})}findChangedPaths(e,t,r="",s=new Set,i=new WeakMap){if(e===t)return s;if(typeof e!="object"||typeof t!="object"||e===null||t===null)return e!==t&&s.add(r||""),s;if(i.has(e))return s;i.set(e,!0);let a=new Set([...Object.keys(e||{}),...Object.keys(t||{})]);for(let o of a){let n=e[o],l=t[o];if(n===l)continue;let d=r?`${r}.${o}`:o;n&&l&&typeof n=="object"&&typeof l=="object"&&!Array.isArray(n)&&!Array.isArray(l)?this.findChangedPaths(n,l,d,s,i):Array.isArray(n)&&Array.isArray(l)?this.isEqual(n,l)||s.add(d):this.isEqual(n,l)||s.add(d)}return s}subscribeBySelector(e,t){let r=this.createDummyState(),s=this.extractPath(e,r);this.logger?.debug&&this.logger.debug("Subscribing to path:",{path:s});let i=async a=>{try{if(a==null){let l=await this.getState(),d=e(l);t(d);return}if(typeof a!="object"||a===null){t(a);return}let o=await this.getState(),n=e(o);t(n)}catch(o){this.logger?.error("Error in selector callback",{path:s,error:o}),t(a)}};return s?this.subscribeByKey(s,i):this.subscribeToAll(()=>{this.getState().then(a=>{t(e(a))})})}async emitEvent(e){try{await this.eventEmitter?.emit({...e,metadata:{...e.metadata||{},timestamp:Date.now(),storageName:this.name}})}catch(t){this.logger?.error("Error emitting event",{event:e,error:t})}}};var N=class c{constructor(e,t,r){this.dbName=e;this.logger=r;this.dbVersion=t}static instances=new Map;db=null;initPromise=null;storeNames=new Set;dbVersion;static getInstance(e,t=1,r){c.instances.has(e)||c.instances.set(e,new c(e,t,r));let s=c.instances.get(e);return t>s.dbVersion&&(s.dbVersion=t),s}async initialize(){return this.db?this.db:(this.initPromise||(this.initPromise=this.openDatabase()),this.initPromise)}async ensureStoreExists(e){if(await this.initialize(),this.db.objectStoreNames.contains(e))return this.storeNames.add(e),this.db;this.logger?.debug(`Store "${e}" not found, upgrading database`,{dbName:this.dbName,currentStores:Array.from(this.db.objectStoreNames)}),this.db.close(),this.db=null,this.dbVersion++,this.initPromise=this.openDatabase([e]);let t=await this.initPromise;return this.storeNames.add(e),t}async openDatabase(e=[]){return new Promise((t,r)=>{this.logger?.debug(`Opening database "${this.dbName}" with version ${this.dbVersion}`);let s=indexedDB.open(this.dbName,this.dbVersion);s.onerror=()=>{this.logger?.error(`Failed to open database "${this.dbName}"`,{error:s.error}),r(s.error)},s.onsuccess=()=>{this.db=s.result;for(let i=0;i<this.db.objectStoreNames.length;i++)this.storeNames.add(this.db.objectStoreNames[i]);this.logger?.debug(`Database "${this.dbName}" opened successfully`,{version:this.db.version,stores:Array.from(this.db.objectStoreNames)}),t(this.db)},s.onupgradeneeded=i=>{let a=i.target.result;this.logger?.debug(`Upgrading database "${this.dbName}" to version ${this.dbVersion}`);for(let o of e)a.objectStoreNames.contains(o)||(this.logger?.debug(`Creating store "${o}"`),a.createObjectStore(o))}})}closeDatabase(){this.db&&(this.db.close(),this.db=null,this.initPromise=null)}async deleteDatabase(){return this.closeDatabase(),new Promise((e,t)=>{let r=indexedDB.deleteDatabase(this.dbName);r.onsuccess=()=>{this.logger?.debug(`Database "${this.dbName}" deleted successfully`),c.instances.delete(this.dbName),this.storeNames.clear(),e()},r.onerror=()=>{this.logger?.error(`Failed to delete database "${this.dbName}"`,{error:r.error}),t(r.error)}})}async ensureStoresExist(e){await this.initialize();let t=e.filter(r=>!this.db.objectStoreNames.contains(r));return t.length===0?this.db:(this.logger?.debug(`\u0421\u043E\u0437\u0434\u0430\u043D\u0438\u0435 \u043D\u0435\u0434\u043E\u0441\u0442\u0430\u044E\u0449\u0438\u0445 \u0445\u0440\u0430\u043D\u0438\u043B\u0438\u0449: ${t.join(", ")}`,{dbName:this.dbName,currentStores:Array.from(this.db.objectStoreNames)}),this.db.close(),this.db=null,this.dbVersion++,this.initPromise=this.openDatabase(t),this.initPromise)}getCurrentVersion(){return this.dbVersion}},K=class{constructor(e,t=1,r){this.dbName=e;this.logger=r;this.version=t}db=null;version;getCurrentVersion(){return this.version}openDatabase(e,t=[]){return new Promise((r,s)=>{this.logger?.debug(`\u041E\u0442\u043A\u0440\u044B\u0442\u0438\u0435 \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445 "${this.dbName}" \u0441 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 ${e}`);let i=indexedDB.open(this.dbName,e);i.onerror=()=>{this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u043E\u0442\u043A\u0440\u044B\u0442\u0438\u0438 \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445 "${this.dbName}"`,{error:i.error}),s(i.error)},i.onsuccess=()=>{this.db=i.result,this.version=this.db.version,this.logger?.debug(`\u0411\u0430\u0437\u0430 \u0434\u0430\u043D\u043D\u044B\u0445 "${this.dbName}" \u0443\u0441\u043F\u0435\u0448\u043D\u043E \u043E\u0442\u043A\u0440\u044B\u0442\u0430`,{version:this.db.version,stores:Array.from(this.db.objectStoreNames)}),r(this.db)},i.onupgradeneeded=a=>{let o=a.target.result;this.logger?.debug(`\u041E\u0431\u043D\u043E\u0432\u043B\u0435\u043D\u0438\u0435 \u0431\u0430\u0437\u044B \u0434\u0430\u043D\u043D\u044B\u0445 "${this.dbName}" \u0434\u043E \u0432\u0435\u0440\u0441\u0438\u0438 ${e}`);for(let n of t)o.objectStoreNames.contains(n)||(this.logger?.debug(`\u0421\u043E\u0437\u0434\u0430\u043D\u0438\u0435 \u0445\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0430 "${n}"`),o.createObjectStore(n))}})}async ensureStoresExist(e){this.db||(this.db=await this.openDatabase(this.version));let t=e.filter(r=>!this.db.objectStoreNames.contains(r));return t.length===0?this.db:(this.logger?.debug(`\u041D\u0435\u043E\u0431\u0445\u043E\u0434\u0438\u043C\u043E \u0441\u043E\u0437\u0434\u0430\u0442\u044C \u043E\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044E\u0449\u0438\u0435 \u0445\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0430: ${t.join(", ")}`,{dbName:this.dbName,currentVersion:this.version}),this.db.close(),this.db=null,this.version++,this.db=await this.openDatabase(this.version,t),this.db)}close(){this.db&&(this.db.close(),this.db=null)}},B=class c extends A{DB_NAME;STORE_NAME;DB_VERSION;dbManager;constructor(e,t,r,s){super(e,t,r,s);let i=e.options;this.DB_NAME=i.dbName||"app_storage",this.STORE_NAME=e.name,this.DB_VERSION=i.dbVersion||1,this.dbManager=N.getInstance(this.DB_NAME,this.DB_VERSION,s)}async doInitialize(){try{if(this.logger?.debug(`Initializing IndexedDB storage "${this.STORE_NAME}"`),await this.dbManager.ensureStoreExists(this.STORE_NAME),!(await this.dbManager.initialize()).objectStoreNames.contains(this.STORE_NAME))throw new Error(`Store "${this.STORE_NAME}" not found after initialization`);return this.initializeMiddlewares(),await this.initializeWithMiddlewares(),this.logger?.debug(`IndexedDB storage "${this.STORE_NAME}" initialized successfully`),this}catch(e){throw this.logger?.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0430\u0446\u0438\u0438 IndexedDB "${this.name}"`,{error:e}),e}}static async getCurrentDBVersion(e){return new Promise(t=>{try{let r=indexedDB.open(e);r.onsuccess=()=>{let s=r.result.version;r.result.close(),t(s)},r.onerror=()=>{t(0)}}catch{t(0)}})}static async createStorages(e,t,r){let i=await this.getCurrentDBVersion(e)||1,a=new K(e,i,r),o=Object.values(t).map(l=>l.name);await a.ensureStoresExist(o);let n={};for(let[l,d]of Object.entries(t)){let g=new c({...d,options:{dbName:e,dbVersion:a.getCurrentVersion()}},d.pluginExecutor,d.eventEmitter,r);n[l]=await g.initialize()}return n}async getTransaction(e="readonly"){try{let t=await this.dbManager.ensureStoreExists(this.STORE_NAME);if(!t.objectStoreNames.contains(this.STORE_NAME)){this.logger?.warn(`Object store "${this.STORE_NAME}" not found, attempting to repair`),t.close(),this.dbManager.closeDatabase();let r=await this.dbManager.ensureStoreExists(this.STORE_NAME);if(!r.objectStoreNames.contains(this.STORE_NAME))throw new Error(`Object store "${this.STORE_NAME}" still doesn't exist after repair attempt`);return r.transaction(this.STORE_NAME,e)}return t.transaction(this.STORE_NAME,e)}catch(t){throw this.logger?.error(`Failed to create transaction for store "${this.STORE_NAME}"`,{error:t}),t}}async getObjectStore(e="readonly"){return(await this.getTransaction(e)).objectStore(this.STORE_NAME)}async doGet(e){let t=await this.getObjectStore();if(e==="")return new Promise((s,i)=>{let a=t.getAll();a.onerror=()=>i(a.error),a.onsuccess=()=>{let o=a.result,n=t.getAllKeys();n.onsuccess=()=>{let l=n.result.reduce((d,g,u)=>(g!=="root"&&(d[g]=o[u]),d),{});s(l)},n.onerror=()=>i(n.error)}});if(e instanceof y&&e.isUnparseable())return new Promise((s,i)=>{let a=t.get(e.valueOf());a.onerror=()=>i(a.error),a.onsuccess=()=>s(a.result)});let r=b(e);if(r.length>1){let s=r[0];return new Promise((i,a)=>{let o=t.get(s);o.onerror=()=>a(o.error),o.onsuccess=()=>{let n=o.result;if(!n){i(void 0);return}let l=w(n,r.slice(1).join("."));i(l)}})}return new Promise((s,i)=>{let a=t.get(r[0]);a.onerror=()=>i(a.error),a.onsuccess=()=>s(a.result)})}async doSet(e,t){if(e===""){let i=await this.getObjectStore("readwrite");return new Promise((a,o)=>{let n=i.transaction;n.oncomplete=()=>{a()},n.onerror=()=>{o(n.error)};let l=i.clear();l.onsuccess=()=>{let d=Object.entries(t);for(let[g,u]of d)i.put(u,g)},l.onerror=()=>{o(l.error)}})}let r=await this.getObjectStore("readwrite");if(e instanceof y&&e.isUnparseable()){await this.putValueInStore(r,e.valueOf(),t);return}let s=b(e);if(s.length>1){let i=s[0];return new Promise((a,o)=>{let n=r.get(i);n.onerror=()=>o(n.error),n.onsuccess=()=>{let l=n.result||{},d=E(l,s.slice(1).join("."),t),g=r.put(d,i);g.onerror=()=>o(g.error),g.onsuccess=()=>a()}})}await this.putValueInStore(r,s[0],t)}async putValueInStore(e,t,r){return new Promise((s,i)=>{let a=e.put(r,t.valueOf());a.onerror=()=>i(a.error),a.onsuccess=()=>s()})}async doUpdate(e){let t=new Map,r=[];for(let{key:s,value:i}of e){if(s instanceof y&&s.isUnparseable()){r.push({key:s.valueOf(),value:i});continue}let a=b(s),o=a[0],n=a.slice(1);t.has(o)||t.set(o,[]),t.get(o).push({path:n,value:i})}try{for(let{key:s,value:i}of r){let a=await this.getObjectStore("readwrite");await this.putValueInStore(a,s,i)}for(let[s,i]of t){let o={...await this.doGet(s)||{}};for(let{path:l,value:d}of i)l.length===0?o=d:o=E(o,l.join("."),d);let n=await this.getObjectStore("readwrite");await this.putValueInStore(n,s,o)}}catch(s){throw this.logger?.error("Error during update:",{error:s}),s}}async doDelete(e){let t=await this.getObjectStore("readwrite");if(e instanceof y&&e.isUnparseable())return new Promise((i,a)=>{let o=t.delete(e.valueOf());o.onerror=()=>a(o.error),o.onsuccess=()=>i(!0)});let r=b(e);if(r.length===1)return new Promise((i,a)=>{let o=t.delete(r[0]);o.onerror=()=>a(o.error),o.onsuccess=()=>i(!0)});let s=r[0];return new Promise((i,a)=>{let o=t.get(s);o.onerror=()=>a(o.error),o.onsuccess=()=>{let n=o.result;if(!n){i(!1);return}let l=w(n,r.slice(0,-1).join(".")),d=r[r.length-1];if(!l||!(d in l)){i(!1);return}if(Array.isArray(l)){let u=parseInt(d,10);isNaN(u)?delete l[d]:l.splice(u,1)}else delete l[d];let g=t.put(n,s);g.onerror=()=>a(g.error),g.onsuccess=()=>i(!0)}})}async doClear(){let e=await this.getObjectStore("readwrite");return new Promise((t,r)=>{let s=e.clear();s.onsuccess=()=>t(),s.onerror=()=>r(s.error)})}async doKeys(){let t=(await this.getObjectStore()).getAllKeys();return new Promise((r,s)=>{t.onsuccess=()=>{r(t.result)},t.onerror=()=>s(t.error)})}async doHas(e){return await this.doGet(e)!==void 0}async doDestroy(){try{await this.doClear()}catch(e){throw this.logger?.error(`Error destroying store "${this.STORE_NAME}"`,{error:e}),e}}};var $=class extends A{constructor(e,t,r,s){super(e,t,r,s)}async doInitialize(){try{return this.logger?.debug(`Initializing LocalStorage "${this.name}"`),this.initializeMiddlewares(),await this.initializeWithMiddlewares(),this.logger?.debug(`LocalStorage "${this.name}" initialized successfully`),this}catch(e){throw this.logger?.error("Error initializing LocalStorage",{error:e}),e}}async doGet(e){let t=localStorage.getItem(this.name);if(!t)return;let r=JSON.parse(t);return e instanceof y&&e.isUnparseable()?r[e.valueOf()]:w(r,e)}async doSet(e,t){let r=localStorage.getItem(this.name),s=r?JSON.parse(r):{};if(e instanceof y&&e.isUnparseable()){s[e.valueOf()]=t,localStorage.setItem(this.name,JSON.stringify(s));return}let i=E({...s},e,t);localStorage.setItem(this.name,JSON.stringify(i))}async doDelete(e){let t=localStorage.getItem(this.name);if(!t)return!1;let r=JSON.parse(t);if(e instanceof y&&e.isUnparseable()){let n=e.valueOf();return n in r?(delete r[n],localStorage.setItem(this.name,JSON.stringify(r)),!0):!1}let s=b(e),i=s.slice(0,-1).join("."),a=s[s.length-1],o=i?w(r,i):r;return!o||!(a in o)?!1:(delete o[a],localStorage.setItem(this.name,JSON.stringify(r)),!0)}async doUpdate(e){let t=localStorage.getItem(this.name),r=t?JSON.parse(t):{};for(let{key:s,value:i}of e)s instanceof y&&s.isUnparseable()?r[s.valueOf()]=i:E(r,s,i);localStorage.setItem(this.name,JSON.stringify(r))}async doClear(){localStorage.removeItem(this.name)}async doKeys(){let e=localStorage.getItem(this.name);if(!e)return[];let t=JSON.parse(e);return this.getAllKeys(t)}async doHas(e){return await this.doGet(e)!==void 0}getAllKeys(e){return Object.keys(e)}async doDestroy(){await this.doClear()}};var V=class extends A{storage=new Map;constructor(e,t,r,s){super(e,t,r,s)}async doInitialize(){try{return this.logger?.debug(`Initializing MemoryStorage "${this.name}"`),this.initializeMiddlewares(),await this.initializeWithMiddlewares(),this.logger?.debug(`MemoryStorage "${this.name}" initialized successfully`),this}catch(e){throw this.logger?.error("Error initializing MemoryStorage",{error:e}),e}}async doGet(e){let t=this.storage.get(this.name);if(t)return e instanceof y&&e.isUnparseable()?t[e.valueOf()]:w(t,e)}async doSet(e,t){let r=this.storage.get(this.name)||{};if(e instanceof y&&e.isUnparseable()){r[e.valueOf()]=t,this.storage.set(this.name,r);return}let s=E({...r},e,t);this.storage.set(this.name,s)}async doDelete(e){let t=this.storage.get(this.name);if(!t)return!1;if(e instanceof y&&e.isUnparseable()){let o=e.valueOf();return o in t?(delete t[o],this.storage.set(this.name,t),!0):!1}let r=b(e),s=r.slice(0,-1).join("."),i=r[r.length-1],a=s?w(t,s):t;return!a||!(i in a)?!1:(delete a[i],this.storage.set(this.name,t),!0)}async doUpdate(e){let r={...this.storage.get(this.name)||{}};for(let{key:s,value:i}of e)s instanceof y&&s.isUnparseable()?r[s.valueOf()]=i:E(r,s,i);this.storage.set(this.name,r)}async doClear(){this.storage.delete(this.name)}async doKeys(){let e=this.storage.get(this.name);return e?this.getAllKeys(e):[]}async doHas(e){return await this.doGet(e)!==void 0}async doDestroy(){this.storage.delete(this.name)}getAllKeys(e){return Object.keys(e)}};export{B as IndexedDBStorage,$ as LocalStorage,V as MemoryStorage,z as SelectorModule,C as StorageEvents,j as StoragePluginModule,_ as StorageStatus,q as broadcastMiddleware};