synapse-storage 3.0.12 → 3.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -1
- package/dist/api.js +1 -1
- package/dist/core.js +1 -1
- package/dist/{createSynapse--jDPgRKD.d.ts → createSynapse-CzeX0ONj.d.ts} +1 -1
- package/dist/{dispatcher.module-Dd7-DiCn.d.ts → dispatcher.module-DjMloBXe.d.ts} +2 -5
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/react.d.ts +2 -2
- package/dist/react.js +1 -1
- package/dist/reactive.d.ts +2 -2
- package/dist/reactive.js +1 -1
- package/dist/utils.d.ts +3 -3
- package/dist/utils.js +1 -1
- package/package.json +15 -50
- package/dist/api.cjs +0 -1
- package/dist/core.cjs +0 -1
- package/dist/index.cjs +0 -1
- package/dist/react.cjs +0 -1
- package/dist/reactive.cjs +0 -1
- package/dist/utils.cjs +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Synapse Storage
|
|
2
2
|
|
|
3
|
-
> **🇺🇸 English** | [🇷🇺 Русский](./docs/ru/README.md)
|
|
3
|
+
> **🇺🇸 English** | [🇷🇺 Русский](./docs/ru/README.md) | [📝 ChangeLog](./CHANGELOG.md)
|
|
4
4
|
|
|
5
5
|
State management toolkit + API client
|
|
6
6
|
|
|
@@ -99,6 +99,97 @@ npm install synapse-storage rxjs react react-dom
|
|
|
99
99
|
|
|
100
100
|
---
|
|
101
101
|
|
|
102
|
+
## 📊 Why Synapse Storage?
|
|
103
|
+
|
|
104
|
+
**One library instead of many** - Synapse combines functionality that usually requires multiple dependencies:
|
|
105
|
+
|
|
106
|
+
| What you get | Traditional approach | Synapse Storage |
|
|
107
|
+
|--------------|---------------------|---------|
|
|
108
|
+
| **State Management** | Redux + RTK (~45KB) | ✅ |
|
|
109
|
+
| **HTTP Client + Caching** | React Query (~39KB) | ✅|
|
|
110
|
+
| **Reactive Effects** | Redux-Observable (~25KB) | ✅|
|
|
111
|
+
| **Storage Adapters** | Custom solutions | ✅|
|
|
112
|
+
| **React Integration** | Custom hooks | ✅|
|
|
113
|
+
| **Computed Selectors** | Reselect (~5KB) | ✅|
|
|
114
|
+
| **Middleware System** | Custom implementation | ✅|
|
|
115
|
+
| **Plugin Architecture** | Custom implementation | ✅|
|
|
116
|
+
|
|
117
|
+
### Bundle Size Comparison
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// Traditional stack
|
|
121
|
+
import { configureStore } from '@reduxjs/toolkit' // ~45KB
|
|
122
|
+
import { createApi } from '@reduxjs/toolkit/query' // included in RTK
|
|
123
|
+
import { QueryClient } from '@tanstack/react-query' // ~39KB
|
|
124
|
+
import { createEpicMiddleware } from 'redux-observable' // ~25KB
|
|
125
|
+
// Total: ~109KB + custom implementations
|
|
126
|
+
|
|
127
|
+
// Synapse Storage
|
|
128
|
+
import { createSynapse } from 'synapse-storage' // ~171KB
|
|
129
|
+
// Total: 171KB with ALL features included
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Modular Usage
|
|
133
|
+
|
|
134
|
+
Don't need everything? Import only what you use:
|
|
135
|
+
|
|
136
|
+
| Use Case | Import | Size | Comparison |
|
|
137
|
+
|-------------------|------------------------|------|---------------------------|
|
|
138
|
+
| **Basic state** | `synapse-storage/core` | ~42KB | vs Redux: 45KB |
|
|
139
|
+
| **+ HTTP client** | `+ /api` | +13KB | vs React Query: 39KB |
|
|
140
|
+
| **+ Reactive** | `+ /reactive` | +8KB | vs Redux-Observable: 25KB |
|
|
141
|
+
| **+ React hooks** | `+ /react` | +5KB | vs Custom hooks |
|
|
142
|
+
| **Full package** | all modules | ~171KB |vs 109KB stack + custom |
|
|
143
|
+
|
|
144
|
+
> **🎯 Result:** Similar or better performance with unified API and TypeScript support out of the box
|
|
145
|
+
|
|
146
|
+
## 🧩 Modular Architecture "Like a Constructor"
|
|
147
|
+
|
|
148
|
+
**Use only what you need** - each module works independently:
|
|
149
|
+
|
|
150
|
+
### 🎯 Flexible Usage Scenarios
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
// 📦 Minimal project - storage only
|
|
154
|
+
import { MemoryStorage } from 'synapse-storage/core' // 42KB
|
|
155
|
+
|
|
156
|
+
// 📦 + Add HTTP client when needed
|
|
157
|
+
import { ApiClient } from 'synapse-storage/api' // +13KB
|
|
158
|
+
|
|
159
|
+
// 📦 + Add reactive effects when required
|
|
160
|
+
import { createDispatcher } from 'synapse-storage/reactive' // +8KB
|
|
161
|
+
|
|
162
|
+
// 📦 + Add React hooks for UI
|
|
163
|
+
import { useSelector } from 'synapse-storage/react' // +5KB
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 🔧 Or Create Your Own Implementation
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
// Use core + your solutions
|
|
170
|
+
import { IStorage } from 'synapse-storage/core'
|
|
171
|
+
|
|
172
|
+
// Implement your HTTP client
|
|
173
|
+
class MyApiClient { /* your logic */ }
|
|
174
|
+
|
|
175
|
+
// Implement your React hooks
|
|
176
|
+
const useMyCustomHook = () => { /* your logic */ }
|
|
177
|
+
|
|
178
|
+
// Combine as convenient!
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 🎨 Constructor Approach Benefits
|
|
182
|
+
|
|
183
|
+
- **🚀 Quick Start** - begin with core, add modules as project grows
|
|
184
|
+
- **📦 Optimal Bundle** - don't pay for unused functionality
|
|
185
|
+
- **🔄 Flexibility** - replace any module with your implementation
|
|
186
|
+
- **🛠️ Compatibility** - modules work independently but integrate perfectly
|
|
187
|
+
- **📈 Scalability** - from simple state to full-featured architecture
|
|
188
|
+
|
|
189
|
+
> **💡 Evolution Example:** Started with MemoryStorage → added ApiClient → connected reactive effects → integrated React hooks. **Each step is optional!**
|
|
190
|
+
>
|
|
191
|
+
---
|
|
192
|
+
|
|
102
193
|
## 📁 Documentation Structure
|
|
103
194
|
|
|
104
195
|
```
|
package/dist/api.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var k={debug:(r,...e)=>{process.env.NODE_ENV!=="production"&&console.debug(`[API] ${r}`,...e)},log:(r,...e)=>{process.env.NODE_ENV!=="production"&&console.log(`[API] ${r}`,...e)},info:(r,...e)=>{console.info(`[API] ${r}`,...e)},warn:(r,...e)=>{console.warn(`[API] ${r}`,...e)},error:(r,...e)=>{console.error(`[API] ${r}`,...e)}};function O(r){return`${r?`${r}|`:""}${Math.random().toString(36).substring(2,9)+Date.now().toString(36)}`}function I(r){let e={};return r.forEach((t,a)=>{e[a.toLowerCase()]=t}),e}function T(r={},e={}){return{...r,...e,getFromStorage:r.getFromStorage||(t=>{try{let a=localStorage.getItem(t);return a?JSON.parse(a):void 0}catch(a){console.warn(`[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u0447\u0442\u0435\u043D\u0438\u044F \u0438\u0437 localStorage: ${a}`);return}}),getCookie:r.getCookie||(t=>{try{let a=document.cookie.match(new RegExp(`(?:^|; )${t.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")}=([^;]*)`));return a?decodeURIComponent(a[1]):void 0}catch(a){console.warn(`[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u0447\u0442\u0435\u043D\u0438\u044F cookie: ${a}`);return}})}}async function D(r,e){let t=new Headers,a=e||T({},{});if(r)try{t=await Promise.resolve(r(t,a))}catch(n){console.warn("[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u043F\u043E\u0434\u0433\u043E\u0442\u043E\u0432\u043A\u0435 \u0437\u0430\u0433\u043E\u043B\u043E\u0432\u043A\u043E\u0432",n)}return t}function K(r,e){return async(t,a)=>{let n=new Headers(t);if(r)try{n=await Promise.resolve(r(n,a))}catch(i){console.warn("[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u043F\u043E\u0434\u0433\u043E\u0442\u043E\u0432\u043A\u0435 \u0433\u043B\u043E\u0431\u0430\u043B\u044C\u043D\u044B\u0445 \u0437\u0430\u0433\u043E\u043B\u043E\u0432\u043A\u043E\u0432",i)}if(e)try{n=await Promise.resolve(e(n,a))}catch(i){console.warn("[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u043F\u043E\u0434\u0433\u043E\u0442\u043E\u0432\u043A\u0435 \u0437\u0430\u0433\u043E\u043B\u043E\u0432\u043A\u043E\u0432 \u044D\u043D\u0434\u043F\u043E\u0438\u043D\u0442\u0430",i)}return n}}var q=(s=>(s.Json="json",s.Blob="blob",s.ArrayBuffer="arrayBuffer",s.Text="text",s.FormData="formData",s.Raw="raw",s))(q||{});function Q(r){let e=r.toLowerCase().split(";")[0].trim();if(e.includes("application/json"))return"json";if(e.includes("text/"))return"text";if(e.includes("multipart/form-data"))return"formData";if(e.includes("application/octet-stream")||e.includes("application/pdf")||e.includes("image/")||e.includes("audio/")||e.includes("video/"))return"blob"}function H(r){let e=r.get("content-type")||"",t=r.get("content-disposition")||"",a=e.includes("application/octet-stream")||e.includes("application/pdf")||e.includes("image/")||e.includes("audio/")||e.includes("video/"),n=t.includes("attachment")||t.includes("filename=");return a||n}function L(r){let e=r.get("content-disposition");if(!e)return;let t=e.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);if(t&&t[1])return t[1].replace(/['"]/g,"").trim()}function B(r){let e=r.get("content-type")||"",t=r.get("content-disposition")||"",a=r.get("content-length");return H(r)?{filename:L(r),contentType:e,contentDisposition:t,size:a?parseInt(a,10):void 0}:void 0}async function J(r,e){let t=e,a=r.headers.get("content-type")||"";!t&&a&&(H(r.headers)?t="blob":t=Q(a)),t||(t="json");try{let n;switch((t==="blob"||t==="arrayBuffer")&&(n=B(r.headers)),t){case"json":try{let s=await r.json();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}catch{let l=await r.text();return r.ok?{data:l,fileMetadata:n}:{error:l,fileMetadata:n}}case"text":{let s=await r.text();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"blob":{let s=await r.blob();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"arrayBuffer":{let s=await r.arrayBuffer();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"formData":{let s=await r.formData();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"raw":return r.ok?{data:r,fileMetadata:n}:{error:r,fileMetadata:n};default:let i=await r.blob();return r.ok?{data:i,fileMetadata:n}:{error:i,fileMetadata:n}}}catch(n){return console.error(`[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u0438\u0437\u0432\u043B\u0435\u0447\u0435\u043D\u0438\u044F \u0434\u0430\u043D\u043D\u044B\u0445 \u0438\u0437 \u043E\u0442\u0432\u0435\u0442\u0430 (\u0444\u043E\u0440\u043C\u0430\u0442: ${t})`,n),r.ok?{data:void 0}:{error:n}}}function M(r){let{baseUrl:e,timeout:t=3e4,fetchFn:a=fetch,credentials:n="same-origin"}=r;return async(i,s={},l)=>{let{path:h,method:C,body:o,query:u,responseFormat:f}=i,{signal:b,timeout:R=t,responseFormat:p}=s,A=p||f,m=new URL(h.startsWith("http")?h:`${e}${h}`);u&&Object.entries(u).forEach(([d,c])=>{c!=null&&(Array.isArray(c)?c.forEach(w=>m.searchParams.append(d,String(w))):m.searchParams.append(d,String(c)))});let E;if(o!==void 0)if(o instanceof FormData||o instanceof Blob)E=o;else if(typeof o=="object"&&o!==null)try{E=JSON.stringify(o),l.has("Content-Type")||l.set("Content-Type","application/json")}catch(d){console.error("[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u0441\u0435\u0440\u0438\u0430\u043B\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0435\u043B\u0430 \u0437\u0430\u043F\u0440\u043E\u0441\u0430",d),E=String(o)}else E=String(o);let P,g=new Promise((d,c)=>{R&&(P=window.setTimeout(()=>{c(new Error(`\u041F\u0440\u0435\u0432\u044B\u0448\u0435\u043D\u043E \u0432\u0440\u0435\u043C\u044F \u043E\u0436\u0438\u0434\u0430\u043D\u0438\u044F \u0437\u0430\u043F\u0440\u043E\u0441\u0430 (${R}\u043C\u0441)`))},R))});try{let d=a(m.toString(),{method:C,headers:l,body:E,signal:b,credentials:n}),c=await Promise.race([d,g]),{data:w,error:F,fileMetadata:j}=await J(c,A);return{data:w,error:F,ok:c.ok,status:c.status,statusText:c.statusText,headers:c.headers,fileDownloadResult:j}}catch(d){let c=d;return console.error("[API] \u041E\u0448\u0438\u0431\u043A\u0430 \u0432\u044B\u043F\u043E\u043B\u043D\u0435\u043D\u0438\u044F \u0437\u0430\u043F\u0440\u043E\u0441\u0430",c),{error:c,ok:!1,status:0,statusText:c.message,headers:new Headers}}finally{P&&window.clearTimeout(P)}}}function $(r,e=[]){let t={};return!r||e.length===0||e.forEach(a=>{r.has(a)&&(t[a]=r.get(a)||"")}),t}var v=class{constructor(e,t,a,n,i,s){this.name=e;this.queryStorage=t;this.configCurrentEndpoint=a;this.cacheableHeaderKeys=n;this.globalCacheConfig=i;this.baseQueryConfig=s;this.prepareHeaders=K(s.prepareHeaders,a.prepareHeaders),this.queryFunction=M({baseUrl:s.baseUrl,fetchFn:s.fetchFn,timeout:s.timeout,credentials:s.credentials}),this.cacheableHeaders=[...n||[],...a.includeCacheableHeaderKeys||[]].filter(l=>!a.excludeCacheableHeaderKeys?.includes(l)),this.meta.name=e,this.meta.tags=a.tags??this.meta.tags,this.meta.invalidatesTags=a.invalidatesTags??this.meta.invalidatesTags,this.meta.cache=this.queryStorage.createCacheConfig(this.configCurrentEndpoint)??this.meta.cache}endpointSubscribers=new Set;fetchCounts=0;meta={cache:!1,invalidatesTags:[],name:"",tags:[]};queryFunction;cacheableHeaders;prepareHeaders;request(e,t){this.fetchCounts++;let a=O(this.name),n=new AbortController,i=new Set,s={status:"idle",requestParams:e,headers:{},error:void 0,data:void 0,fromCache:!1},l=T({requestParams:e},t?.context||{}),h=o=>{Object.assign(s,o),i.forEach(u=>{u({...s})})},C=new Promise(async(o,u)=>{try{let f=await D(this.prepareHeaders,l),b=$(f,t?.cacheableHeaderKeys?t.cacheableHeaderKeys:this.cacheableHeaders),R=this.queryStorage.shouldCache(this.configCurrentEndpoint,t),[p,A]=this.queryStorage.createCacheKey(this.name,{...e,...b}),m;if(R&&(m=await this.queryStorage.getCachedResult(p)),m)h({fromCache:!0,status:"success",data:m.data,error:void 0,headers:m.headers,requestParams:e}),o({...m,fromCache:!0});else{h({fromCache:!1,status:"loading"});let E=this.configCurrentEndpoint.request(e,t?.context),P={...t,signal:n.signal},g=await this.queryFunction(E,P,f);if(g.ok){let{headers:d,...c}=g;if(R){let w=this.queryStorage.createCacheConfig(this.configCurrentEndpoint);await this.queryStorage.setCachedResult(p,{...c,headers:I(d)},w,A??{},this.configCurrentEndpoint.tags??[],this.configCurrentEndpoint.invalidatesTags??[])}h({fromCache:!1,status:"success",data:g.data,error:void 0,headers:g.headers,requestParams:e}),this.endpointSubscribers.forEach(w=>{let F={status:"success",fetchCounts:this.fetchCounts,meta:this.meta,cacheableHeaders:this.cacheableHeaders,error:void 0};w(F)}),o({...g,fromCache:!1})}else h({fromCache:!1,status:"error",data:void 0,error:g.error,headers:g.headers,requestParams:e}),this.endpointSubscribers.forEach(d=>{let c={status:"error",fetchCounts:this.fetchCounts,meta:this.meta,cacheableHeaders:this.cacheableHeaders,error:g.error};d(c)}),u(g.error)}}catch(f){h({fromCache:!1,status:"error",data:void 0,error:f,headers:void 0,requestParams:e}),u(f)}});return{id:a,subscribe(o,u={}){let{autoUnsubscribe:f=!0}=u;i.add(o),o(s);let b=()=>i.delete(o);return f&&C.finally(()=>{b()}),b},wait:()=>C,waitWithCallbacks(o={}){let{idle:u,loading:f,success:b,error:R}=o;return this.subscribe(p=>{switch(p.status){case"idle":u?.(p);break;case"loading":f?.(p);break;case"success":b?.(p.data,p);break;case"error":R?.(p.error,p);break}},{autoUnsubscribe:!0}),C},abort:()=>{n&&!n.signal.aborted&&n.abort()},then:(o,u)=>C.then(o,u),catch:o=>C.catch(o),finally:o=>C.finally(o)}}subscribe(e){this.endpointSubscribers.add(e);let t={status:"idle",fetchCounts:this.fetchCounts,meta:this.meta,cacheableHeaders:this.cacheableHeaders,error:void 0};return e(t),()=>this.endpointSubscribers.delete(e)}reset(){return this.fetchCounts=0,Promise.resolve()}destroy(){this.endpointSubscribers.clear()}};var x=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 y=class{static createMetadata(e=0,t=[]){let a=Date.now(),n=e>0?a+e:1/0;return{createdAt:a,updatedAt:a,expiresAt:n,tags:t,createdAtDateTime:this.formatDateTime(a),updatedAtDateTime:this.formatDateTime(a),expiresAtDateTime:n===1/0?"never":this.formatDateTime(n)}}static formatDateTime(e){return new Date(e).toISOString()}static isExpired(e){return Date.now()>e.expiresAt}static updateMetadata(e){return{...e,updatedAt:Date.now()}}static createKey(...e){return new x(e.join("_"))}static createApiKey(e,t){if(!t)return[new x(e,!0),t];let a=Object.entries(t).sort(([n],[i])=>n.localeCompare(i)).map(([n,i])=>`${n}=${i}`).join("&");return[new x(`${e}_${a}`,!0),t]}static hasAnyTag(e,t=[]){return!e.tags||!t.length?!1:t.some(a=>e.tags?.includes(a))}};var S=class{constructor(e,t){this.storageExternal=e;this.globalCacheConfig=t}storage=null;cleanupInterval=null;defaultCacheOptions={ttl:5*60*1e3,cleanup:{enabled:!0,interval:10*60*1e3},invalidateOnError:!0};async initialize(){return await this.createStorage(),this.startCleanupInterval(),this}async createStorage(){try{let e=this.storageExternal;await e.initialize(),this.storage=e}catch(e){throw console.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",e),e}}startCleanupInterval(){this.cleanupInterval&&(clearInterval(this.cleanupInterval),this.cleanupInterval=null);let e=typeof this.globalCacheConfig=="object"?this.globalCacheConfig.cleanup:this.defaultCacheOptions.cleanup;e?.enabled&&e.interval&&(this.cleanupInterval=setInterval(()=>{this.cleanup().catch(t=>console.error("\u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u043E\u0447\u0438\u0441\u0442\u043A\u0435 \u043A\u044D\u0448\u0430:",t))},e.interval))}getStorage(){return this.storage}createCacheKey(e,t){return y.createApiKey(e,t)}async getCachedResult(e){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");let t=await this.storage.get(e);if(!t)return;if(y.isExpired(t.metadata)){await this.storage.delete(e);return}let a={...t,metadata:y.updateMetadata(t.metadata)};return await this.storage.set(e,a),t.data}async setCachedResult(e,t,a,n,i,s){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");s?.length&&await this.invalidateCacheByTags(s);let l=y.createMetadata(a.ttl,i),h={data:t,metadata:l,params:n};await this.storage.set(e,h)}shouldCache(e,t){return!(this.globalCacheConfig===!1||e?.cache===!1||typeof e?.cache=="object"&&e?.cache.ttl===0||t?.disableCache===!0||this.globalCacheConfig===void 0&&e?.cache===void 0)}createCacheConfig(e){let t=this.defaultCacheOptions;if(typeof this.globalCacheConfig=="object"&&(t=this.globalCacheConfig),typeof e?.cache=="object"){let a=e.cache;t={...t,...a}}return t}async invalidateCacheByTags(e){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");let t=await this.storage.keys();for(let a of t){let n=await this.storage.get(a);n&&y.hasAnyTag(n.metadata,e)&&await this.storage.delete(a)}}async invalidateCache(e){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");await this.storage.delete(e)}async cleanup(){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");let e=await this.storage.keys();for(let t of e){let a=await this.storage.get(t);a&&y.isExpired(a.metadata)&&await this.storage.delete(t)}}async destroy(){this.cleanupInterval&&(window.clearInterval(this.cleanupInterval),this.cleanupInterval=null),this.storage&&(await this.storage.destroy(),this.storage=null)}};var U=class{queryStorage;cacheableHeaderKeys;globalCacheConfig;baseQueryConfig;storageExternal;createEndpoints;endpoints={};constructor(e){this.cacheableHeaderKeys=e.cacheableHeaderKeys,this.globalCacheConfig=e.cache,this.baseQueryConfig=e.baseQuery,this.storageExternal=e.storage,this.createEndpoints=e.endpoints}async init(){return this.queryStorage=await new S(this.storageExternal,this.globalCacheConfig).initialize(),await this.initializeEndpoints(),this}async initializeEndpoints(){let e=a=>a,t=await this.createEndpoints(e)||{};for(let[a,n]of Object.entries(t)){let i=a;this.endpoints[i]=new v(a,this.queryStorage,n,this.cacheableHeaderKeys,this.globalCacheConfig,this.baseQueryConfig)}}getEndpoints(){return this.endpoints}async request(e,t,a){let i=this.getEndpoints()[e];if(!i)throw new Error(`\u042D\u043D\u0434\u043F\u043E\u0438\u043D\u0442 ${String(e)} \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D`);try{return await i.request(t,a).wait()}catch(s){throw k.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0437\u0430\u043F\u0440\u043E\u0441\u0430 \u043A ${String(e)}`,{error:s,params:t}),s}}async destroy(){await Promise.all(Object.values(this.endpoints).map(async e=>(e.destroy(),Promise.resolve()))),this.endpoints={},await this.queryStorage.destroy()}};export{U as ApiClient,q as ResponseFormat,k as apiLogger,O as createUniqueId,I as headersToObject};
|
|
1
|
+
var k={debug:(r,...e)=>{process.env.NODE_ENV},log:(r,...e)=>{process.env.NODE_ENV},info:(r,...e)=>{},warn:(r,...e)=>{},error:(r,...e)=>{}};function O(r){return`${r?`${r}|`:""}${Math.random().toString(36).substring(2,9)+Date.now().toString(36)}`}function I(r){let e={};return r.forEach((t,a)=>{e[a.toLowerCase()]=t}),e}function T(r={},e={}){return{...r,...e,getFromStorage:r.getFromStorage||(t=>{try{let a=localStorage.getItem(t);return a?JSON.parse(a):void 0}catch{return}}),getCookie:r.getCookie||(t=>{try{let a=document.cookie.match(new RegExp(`(?:^|; )${t.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")}=([^;]*)`));return a?decodeURIComponent(a[1]):void 0}catch{return}})}}async function D(r,e){let t=new Headers,a=e||T({},{});if(r)try{t=await Promise.resolve(r(t,a))}catch{}return t}function K(r,e){return async(t,a)=>{let n=new Headers(t);if(r)try{n=await Promise.resolve(r(n,a))}catch{}if(e)try{n=await Promise.resolve(e(n,a))}catch{}return n}}var q=(s=>(s.Json="json",s.Blob="blob",s.ArrayBuffer="arrayBuffer",s.Text="text",s.FormData="formData",s.Raw="raw",s))(q||{});function Q(r){let e=r.toLowerCase().split(";")[0].trim();if(e.includes("application/json"))return"json";if(e.includes("text/"))return"text";if(e.includes("multipart/form-data"))return"formData";if(e.includes("application/octet-stream")||e.includes("application/pdf")||e.includes("image/")||e.includes("audio/")||e.includes("video/"))return"blob"}function H(r){let e=r.get("content-type")||"",t=r.get("content-disposition")||"",a=e.includes("application/octet-stream")||e.includes("application/pdf")||e.includes("image/")||e.includes("audio/")||e.includes("video/"),n=t.includes("attachment")||t.includes("filename=");return a||n}function L(r){let e=r.get("content-disposition");if(!e)return;let t=e.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);if(t&&t[1])return t[1].replace(/['"]/g,"").trim()}function B(r){let e=r.get("content-type")||"",t=r.get("content-disposition")||"",a=r.get("content-length");return H(r)?{filename:L(r),contentType:e,contentDisposition:t,size:a?parseInt(a,10):void 0}:void 0}async function J(r,e){let t=e,a=r.headers.get("content-type")||"";!t&&a&&(H(r.headers)?t="blob":t=Q(a)),t||(t="json");try{let n;switch((t==="blob"||t==="arrayBuffer")&&(n=B(r.headers)),t){case"json":try{let s=await r.json();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}catch{let d=await r.text();return r.ok?{data:d,fileMetadata:n}:{error:d,fileMetadata:n}}case"text":{let s=await r.text();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"blob":{let s=await r.blob();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"arrayBuffer":{let s=await r.arrayBuffer();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"formData":{let s=await r.formData();return r.ok?{data:s,fileMetadata:n}:{error:s,fileMetadata:n}}case"raw":return r.ok?{data:r,fileMetadata:n}:{error:r,fileMetadata:n};default:let i=await r.blob();return r.ok?{data:i,fileMetadata:n}:{error:i,fileMetadata:n}}}catch(n){return r.ok?{data:void 0}:{error:n}}}function M(r){let{baseUrl:e,timeout:t=3e4,fetchFn:a=fetch,credentials:n="same-origin"}=r;return async(i,s={},d)=>{let{path:h,method:C,body:o,query:u,responseFormat:f}=i,{signal:b,timeout:R=t,responseFormat:l}=s,A=l||f,m=new URL(h.startsWith("http")?h:`${e}${h}`);u&&Object.entries(u).forEach(([p,c])=>{c!=null&&(Array.isArray(c)?c.forEach(w=>m.searchParams.append(p,String(w))):m.searchParams.append(p,String(c)))});let E;if(o!==void 0)if(o instanceof FormData||o instanceof Blob)E=o;else if(typeof o=="object"&&o!==null)try{E=JSON.stringify(o),d.has("Content-Type")||d.set("Content-Type","application/json")}catch{E=String(o)}else E=String(o);let P,g=new Promise((p,c)=>{R&&(P=window.setTimeout(()=>{c(new Error(`\u041F\u0440\u0435\u0432\u044B\u0448\u0435\u043D\u043E \u0432\u0440\u0435\u043C\u044F \u043E\u0436\u0438\u0434\u0430\u043D\u0438\u044F \u0437\u0430\u043F\u0440\u043E\u0441\u0430 (${R}\u043C\u0441)`))},R))});try{let p=a(m.toString(),{method:C,headers:d,body:E,signal:b,credentials:n}),c=await Promise.race([p,g]),{data:w,error:F,fileMetadata:j}=await J(c,A);return{data:w,error:F,ok:c.ok,status:c.status,statusText:c.statusText,headers:c.headers,fileDownloadResult:j}}catch(p){let c=p;return{error:c,ok:!1,status:0,statusText:c.message,headers:new Headers}}finally{P&&window.clearTimeout(P)}}}function $(r,e=[]){let t={};return!r||e.length===0||e.forEach(a=>{r.has(a)&&(t[a]=r.get(a)||"")}),t}var v=class{constructor(e,t,a,n,i,s){this.name=e;this.queryStorage=t;this.configCurrentEndpoint=a;this.cacheableHeaderKeys=n;this.globalCacheConfig=i;this.baseQueryConfig=s;this.prepareHeaders=K(s.prepareHeaders,a.prepareHeaders),this.queryFunction=M({baseUrl:s.baseUrl,fetchFn:s.fetchFn,timeout:s.timeout,credentials:s.credentials}),this.cacheableHeaders=[...n||[],...a.includeCacheableHeaderKeys||[]].filter(d=>!a.excludeCacheableHeaderKeys?.includes(d)),this.meta.name=e,this.meta.tags=a.tags??this.meta.tags,this.meta.invalidatesTags=a.invalidatesTags??this.meta.invalidatesTags,this.meta.cache=this.queryStorage.createCacheConfig(this.configCurrentEndpoint)??this.meta.cache}endpointSubscribers=new Set;fetchCounts=0;meta={cache:!1,invalidatesTags:[],name:"",tags:[]};queryFunction;cacheableHeaders;prepareHeaders;request(e,t){this.fetchCounts++;let a=O(this.name),n=new AbortController,i=new Set,s={status:"idle",requestParams:e,headers:{},error:void 0,data:void 0,fromCache:!1},d=T({requestParams:e},t?.context||{}),h=o=>{Object.assign(s,o),i.forEach(u=>{u({...s})})},C=new Promise(async(o,u)=>{try{let f=await D(this.prepareHeaders,d),b=$(f,t?.cacheableHeaderKeys?t.cacheableHeaderKeys:this.cacheableHeaders),R=this.queryStorage.shouldCache(this.configCurrentEndpoint,t),[l,A]=this.queryStorage.createCacheKey(this.name,{...e,...b}),m;if(R&&(m=await this.queryStorage.getCachedResult(l)),m)h({fromCache:!0,status:"success",data:m.data,error:void 0,headers:m.headers,requestParams:e}),o({...m,fromCache:!0});else{h({fromCache:!1,status:"loading"});let E=this.configCurrentEndpoint.request(e,t?.context),P={...t,signal:n.signal},g=await this.queryFunction(E,P,f);if(g.ok){let{headers:p,...c}=g;if(R){let w=this.queryStorage.createCacheConfig(this.configCurrentEndpoint);await this.queryStorage.setCachedResult(l,{...c,headers:I(p)},w,A??{},this.configCurrentEndpoint.tags??[],this.configCurrentEndpoint.invalidatesTags??[])}h({fromCache:!1,status:"success",data:g.data,error:void 0,headers:g.headers,requestParams:e}),this.endpointSubscribers.forEach(w=>{let F={status:"success",fetchCounts:this.fetchCounts,meta:this.meta,cacheableHeaders:this.cacheableHeaders,error:void 0};w(F)}),o({...g,fromCache:!1})}else h({fromCache:!1,status:"error",data:void 0,error:g.error,headers:g.headers,requestParams:e}),this.endpointSubscribers.forEach(p=>{let c={status:"error",fetchCounts:this.fetchCounts,meta:this.meta,cacheableHeaders:this.cacheableHeaders,error:g.error};p(c)}),u(g.error)}}catch(f){h({fromCache:!1,status:"error",data:void 0,error:f,headers:void 0,requestParams:e}),u(f)}});return{id:a,subscribe(o,u={}){let{autoUnsubscribe:f=!0}=u;i.add(o),o(s);let b=()=>i.delete(o);return f&&C.finally(()=>{b()}),b},wait:()=>C,waitWithCallbacks(o={}){let{idle:u,loading:f,success:b,error:R}=o;return this.subscribe(l=>{switch(l.status){case"idle":u?.(l);break;case"loading":f?.(l);break;case"success":b?.(l.data,l);break;case"error":R?.(l.error,l);break}},{autoUnsubscribe:!0}),C},abort:()=>{n&&!n.signal.aborted&&n.abort()},then:(o,u)=>C.then(o,u),catch:o=>C.catch(o),finally:o=>C.finally(o)}}subscribe(e){this.endpointSubscribers.add(e);let t={status:"idle",fetchCounts:this.fetchCounts,meta:this.meta,cacheableHeaders:this.cacheableHeaders,error:void 0};return e(t),()=>this.endpointSubscribers.delete(e)}reset(){return this.fetchCounts=0,Promise.resolve()}destroy(){this.endpointSubscribers.clear()}};var x=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 y=class{static createMetadata(e=0,t=[]){let a=Date.now(),n=e>0?a+e:1/0;return{createdAt:a,updatedAt:a,expiresAt:n,tags:t,createdAtDateTime:this.formatDateTime(a),updatedAtDateTime:this.formatDateTime(a),expiresAtDateTime:n===1/0?"never":this.formatDateTime(n)}}static formatDateTime(e){return new Date(e).toISOString()}static isExpired(e){return Date.now()>e.expiresAt}static updateMetadata(e){return{...e,updatedAt:Date.now()}}static createKey(...e){return new x(e.join("_"))}static createApiKey(e,t){if(!t)return[new x(e,!0),t];let a=Object.entries(t).sort(([n],[i])=>n.localeCompare(i)).map(([n,i])=>`${n}=${i}`).join("&");return[new x(`${e}_${a}`,!0),t]}static hasAnyTag(e,t=[]){return!e.tags||!t.length?!1:t.some(a=>e.tags?.includes(a))}};var S=class{constructor(e,t){this.storageExternal=e;this.globalCacheConfig=t}storage=null;cleanupInterval=null;defaultCacheOptions={ttl:5*60*1e3,cleanup:{enabled:!0,interval:10*60*1e3},invalidateOnError:!0};async initialize(){return await this.createStorage(),this.startCleanupInterval(),this}async createStorage(){try{let e=this.storageExternal;await e.initialize(),this.storage=e}catch(e){throw e}}startCleanupInterval(){this.cleanupInterval&&(clearInterval(this.cleanupInterval),this.cleanupInterval=null);let e=typeof this.globalCacheConfig=="object"?this.globalCacheConfig.cleanup:this.defaultCacheOptions.cleanup;e?.enabled&&e.interval&&(this.cleanupInterval=setInterval(()=>{this.cleanup().catch(t=>{})},e.interval))}getStorage(){return this.storage}createCacheKey(e,t){return y.createApiKey(e,t)}async getCachedResult(e){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");let t=await this.storage.get(e);if(!t)return;if(y.isExpired(t.metadata)){await this.storage.delete(e);return}let a={...t,metadata:y.updateMetadata(t.metadata)};return await this.storage.set(e,a),t.data}async setCachedResult(e,t,a,n,i,s){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");s?.length&&await this.invalidateCacheByTags(s);let d=y.createMetadata(a.ttl,i),h={data:t,metadata:d,params:n};await this.storage.set(e,h)}shouldCache(e,t){return!(this.globalCacheConfig===!1||e?.cache===!1||typeof e?.cache=="object"&&e?.cache.ttl===0||t?.disableCache===!0||this.globalCacheConfig===void 0&&e?.cache===void 0)}createCacheConfig(e){let t=this.defaultCacheOptions;if(typeof this.globalCacheConfig=="object"&&(t=this.globalCacheConfig),typeof e?.cache=="object"){let a=e.cache;t={...t,...a}}return t}async invalidateCacheByTags(e){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");let t=await this.storage.keys();for(let a of t){let n=await this.storage.get(a);n&&y.hasAnyTag(n.metadata,e)&&await this.storage.delete(a)}}async invalidateCache(e){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");await this.storage.delete(e)}async cleanup(){if(!this.storage)throw new Error("\u0425\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0435 \u043D\u0435 \u0438\u043D\u0438\u0446\u0438\u0430\u043B\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E");let e=await this.storage.keys();for(let t of e){let a=await this.storage.get(t);a&&y.isExpired(a.metadata)&&await this.storage.delete(t)}}async destroy(){this.cleanupInterval&&(window.clearInterval(this.cleanupInterval),this.cleanupInterval=null),this.storage&&(await this.storage.destroy(),this.storage=null)}};var U=class{queryStorage;cacheableHeaderKeys;globalCacheConfig;baseQueryConfig;storageExternal;createEndpoints;endpoints={};constructor(e){this.cacheableHeaderKeys=e.cacheableHeaderKeys,this.globalCacheConfig=e.cache,this.baseQueryConfig=e.baseQuery,this.storageExternal=e.storage,this.createEndpoints=e.endpoints}async init(){return this.queryStorage=await new S(this.storageExternal,this.globalCacheConfig).initialize(),await this.initializeEndpoints(),this}async initializeEndpoints(){let e=a=>a,t=await this.createEndpoints(e)||{};for(let[a,n]of Object.entries(t)){let i=a;this.endpoints[i]=new v(a,this.queryStorage,n,this.cacheableHeaderKeys,this.globalCacheConfig,this.baseQueryConfig)}}getEndpoints(){return this.endpoints}async request(e,t,a){let i=this.getEndpoints()[e];if(!i)throw new Error(`\u042D\u043D\u0434\u043F\u043E\u0438\u043D\u0442 ${String(e)} \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D`);try{return await i.request(t,a).wait()}catch(s){throw k.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0437\u0430\u043F\u0440\u043E\u0441\u0430 \u043A ${String(e)}`,{error:s,params:t}),s}}async destroy(){await Promise.all(Object.values(this.endpoints).map(async e=>(e.destroy(),Promise.resolve()))),this.endpoints={},await this.queryStorage.destroy()}};export{U as ApiClient,q as ResponseFormat,k as apiLogger,O as createUniqueId,I as headersToObject};
|
package/dist/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var m=!1,x=new Map;function V(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 U(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),m&&console.log(`[${this.id}] \u0421\u043E\u0437\u0434\u0430\u043D new SelectorSubscription`)}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)){m&&console.log(`[${this.id}] \u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u0438\u0437\u043C\u0435\u043D\u0438\u043B\u043E\u0441\u044C, notify()`,{old:this.lastValue,new:e}),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)}else m&&console.log(`[${this.id}] \u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u043D\u0435 \u0438\u0437\u043C\u0435\u043D\u0438\u043B\u043E\u0441\u044C in notify(), \u043F\u0440\u043E\u043F\u0443\u0441\u043A \u0443\u0432\u0435\u0434\u043E\u043C\u043B\u0435\u043D\u0438\u044F`)}catch(e){throw this.logger?.error(`[${this.id}] \u041E\u0448\u0438\u0431\u043A\u0430 \u0432 notify()`,{error:e}),e}}subscribe(e){return m&&console.log(`[${this.id}] \u0414\u043E\u0431\u0430\u0432\u043B\u0435\u043D\u043E \u043D\u043E\u0432\u044B\u0439 \u043F\u043E\u0434\u043F\u0438\u0441\u0447\u0438\u043A, \u0432\u0441\u0435\u0433\u043E: ${this.subscribers.size+1}`),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})}),()=>{m&&console.log(`[${this.id}] \u041F\u043E\u0434\u043F\u0438\u0441\u0447\u0438\u043A \u0443\u0434\u0430\u043B\u0435\u043D, \u043E\u0441\u0442\u0430\u043B\u043E\u0441\u044C: ${this.subscribers.size-1}`),this.subscribers.delete(e)}}cleanup(){m&&console.log(`[${this.id}] \u041E\u0447\u0438\u0441\u0442\u043A\u0430 \u043F\u043E\u0434\u043F\u0438\u0441\u043A\u0438, \u0431\u044B\u043B\u043E ${this.subscribers.size} \u043F\u043E\u0434\u043F\u0438\u0441\u0447\u0438\u043A\u043E\u0432`),this.subscribers.clear(),this.lastValue=void 0}getId(){return this.id}},G=class{constructor(e,t){this.source=e;this.logger=t;this.storageName=e.name,m&&console.log(`\u0421\u043E\u0437\u0434\u0430\u043D SelectorModule \u0434\u043B\u044F \u0445\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0430: ${this.storageName}`),this.source.getState().then(r=>{this.cachedState=r,m&&console.log(`\u041A\u044D\u0448\u0438\u0440\u043E\u0432\u0430\u043D\u043D\u043E\u0435 \u043D\u0430\u0447\u0430\u043B\u044C\u043D\u043E\u0435 \u0441\u043E\u0441\u0442\u043E\u044F\u043D\u0438\u0435 \u0434\u043B\u044F ${this.storageName}`)})}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=V(a)}else{let a=t.map(n=>n.getId()).join("_"),o=r.toString();i=V(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 m&&console.log(`[${this.storageName}] Reusing cached selector: ${a}`),this.localSelectorCache.get(a).api;if(x.has(a)){let d=x.get(a);return d.refCount++,m&&console.log(`[${this.storageName}] \u041F\u043E\u0432\u0442\u043E\u0440\u043D\u043E\u0435 \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u043D\u0438\u0435 \u0433\u043B\u043E\u0431\u0430\u043B\u044C\u043D\u043E\u0433\u043E \u043A\u044D\u0448\u0438\u0440\u043E\u0432\u0430\u043D\u043D\u043E\u0433\u043E \u0441\u0435\u043B\u0435\u043A\u0442\u043E\u0440\u0430: ${a}, refCount: ${d.refCount}`),d.api}let o,n,l=[];if(s){let d=U(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}),m&&console.log(`[${this.storageName}] \u0421\u043E\u0437\u0434\u0430\u043D \u043D\u043E\u0432\u044B\u0439 \u0441\u0435\u043B\u0435\u043A\u0442\u043E\u0440: ${a}`),o}createSimpleSelector(e,t){m&&console.log(`[${this.storageName}] \u0421\u043E\u0437\u0434\u0430\u043D \u043F\u0440\u043E\u0441\u0442\u043E\u0439 \u0441\u0435\u043B\u0435\u043A\u0442\u043E\u0440: ${t.name}`);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"&&(m&&console.log(`[${i}] \u041F\u043E\u043B\u0443\u0447\u0435\u043D\u043E \u0441\u043E\u0431\u044B\u0442\u0438\u0435 \u043E\u0431\u043D\u043E\u0432\u043B\u0435\u043D\u0438\u044F \u0445\u0440\u0430\u043D\u0438\u043B\u0438\u0449\u0430`),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=U(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(){m&&console.log(`[${this.storageName}] \u041D\u0430\u0447\u0430\u043B\u043E\u0441\u044C \u0443\u043D\u0438\u0447\u0442\u043E\u0436\u0435\u043D\u0438\u0435 SelectorModule`),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),m&&console.log(`[${this.storageName}] \u0423\u0434\u0430\u043B\u0435\u043D \u0441\u0435\u043B\u0435\u043A\u0442\u043E\u0440 \u0438\u0437 \u0433\u043B\u043E\u0431\u0430\u043B\u044C\u043D\u043E\u0433\u043E \u043A\u044D\u0448\u0430: ${t}`)))}),m&&console.log(`[${this.storageName}] \u0423\u043D\u0438\u0447\u0442\u043E\u0436\u0435\u043D`)}};var z=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)),console.log("executeAfterGet",e,t,r);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 j=(s=>(s.IDLE="idle",s.LOADING="loading",s.READY="ready",s.ERROR="error",s))(j||{}),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&&console.log(`[SyncBroadcastChannel][${this.tabId}]`,...e)}error(...e){console.error(`[SyncBroadcastChannel][${this.tabId}]`,...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)){console.error("[Sync Response] Invalid updates structure:",a.value);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(o){console.error("[Sync Response] Error applying updates:",o)}})),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 f=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(y=>y.action),b=o(I);console.log(`\u{1F504} Batching ${u}: ${S.length} actions -> ${b.length} merged`,{original:I.map(y=>({type:y.type,key:y.key?.toString(),value:y.value})),merged:b.map(y=>({type:y.type,key:y.key?.toString(),value:y.value}))});for(let y of b)try{let O=await p(y);S.filter(T=>T.action.type===y.type&&T.action.key?.toString()===y.key?.toString()).forEach(T=>T.resolve(O))}catch(O){S.filter(T=>T.action.type===y.type&&T.action.key?.toString()===y.key?.toString()).forEach(T=>T.reject(O))}}catch(I){S.forEach(b=>b.reject(I))}}},g=async(u,h,p)=>new Promise((S,I)=>{let b=a(u),y=r.get(b);y||(y=[],r.set(b,y)),y.push({action:u,resolve:S,reject:I,timestamp:Date.now()}),n(b),y.length>=e?setImmediate(()=>d(b,h,p)):l(b,()=>d(b,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 console.log("ShallowCompare: \u0437\u043D\u0430\u0447\u0435\u043D\u0438\u044F \u0438\u0434\u0435\u043D\u0442\u0438\u0447\u043D\u044B, \u043F\u0440\u043E\u043F\u0443\u0441\u043A\u0430\u0435\u043C \u043E\u043F\u0435\u0440\u0430\u0446\u0438\u044E",{key:o,value:l}),{...n,__metadata:{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 console.error("Error in middleware chain:",t),t}}};function w(c){return c instanceof f&&c.isUnparseable()?[c.toString()]:c.toString().replace(/\[/g,".").replace(/\]/g,"").split(".").filter(Boolean)}function v(c,e){return w(e).reduce((r,s)=>r===void 0?void 0:r[s],c)}function E(c,e,t){if(e==="")return t;let r=w(e);if(e instanceof f&&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;_initStatus={status:"idle"};statusSubscribers=new Set;selectorPathCache=new WeakMap;middlewareModule;initializedMiddlewares=null;subscribers=new Map;get initStatus(){return{...this._initStatus}}async waitForReady(){if(this._initStatus.status==="ready")return this;if(this._initStatus.status==="error")throw this._initStatus.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._initStatus.status;this._initStatus={...this._initStatus,...e},t!==this._initStatus.status&&this.logger?.debug(`Storage "${this.name}" status changed: ${t} -> ${this._initStatus.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._initStatus.status==="ready")return this;if(this._initStatus.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._initStatus.status!=="ready")throw new Error(`Storage "${this.name}" is not ready. Current status: ${this._initStatus.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?.__metadata?.valueNotChanged===!0,o;if(a&&i?.__metadata?.originalValue!==void 0?o=i.__metadata.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=v(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}},B=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)}},K=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=()=>{console.warn(`\u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u043E\u043F\u0440\u0435\u0434\u0435\u043B\u0435\u043D\u0438\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0411\u0414 ${e}`,r.error),t(0)}}catch(r){console.warn(`\u0418\u0441\u043A\u043B\u044E\u0447\u0435\u043D\u0438\u0435 \u043F\u0440\u0438 \u043E\u043F\u0440\u0435\u0434\u0435\u043B\u0435\u043D\u0438\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0411\u0414 ${e}`,r),t(0)}})}static async createStorages(e,t,r){let i=await this.getCurrentDBVersion(e)||1,a=new B(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 f&&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=w(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=v(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 f&&e.isUnparseable()){await this.putValueInStore(r,e.valueOf(),t);return}let s=w(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 f&&s.isUnparseable()){r.push({key:s.valueOf(),value:i});continue}let a=w(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 f&&e.isUnparseable())return new Promise((i,a)=>{let o=t.delete(e.valueOf());o.onerror=()=>a(o.error),o.onsuccess=()=>i(!0)});let r=w(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=v(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 f&&e.isUnparseable()?r[e.valueOf()]:v(r,e)}async doSet(e,t){let r=localStorage.getItem(this.name),s=r?JSON.parse(r):{};if(e instanceof f&&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 f&&e.isUnparseable()){let n=e.valueOf();return n in r?(delete r[n],localStorage.setItem(this.name,JSON.stringify(r)),!0):!1}let s=w(e),i=s.slice(0,-1).join("."),a=s[s.length-1],o=i?v(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 f&&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 _=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 f&&e.isUnparseable()?t[e.valueOf()]:v(t,e)}async doSet(e,t){let r=this.storage.get(this.name)||{};if(e instanceof f&&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 f&&e.isUnparseable()){let o=e.valueOf();return o in t?(delete t[o],this.storage.set(this.name,t),!0):!1}let r=w(e),s=r.slice(0,-1).join("."),i=r[r.length-1],a=s?v(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 f&&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{K as IndexedDBStorage,$ as LocalStorage,_ as MemoryStorage,G as SelectorModule,C as StorageEvents,z as StoragePluginModule,j as StorageStatus,q as broadcastMiddleware};
|
|
1
|
+
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};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import { I as ISelectorModule } from './selector.interface-CA5y-kD_.js';
|
|
3
3
|
import { j as IStorage } from './storage.interface-2HKvqdAJ.js';
|
|
4
|
-
import {
|
|
4
|
+
import { l as ExternalStates, n as Effect } from './dispatcher.module-DjMloBXe.js';
|
|
5
5
|
|
|
6
6
|
type ExtractDispatchType<T> = T extends {
|
|
7
7
|
dispatch: infer D;
|
|
@@ -219,6 +219,7 @@ interface WatcherDefinition<T, R> {
|
|
|
219
219
|
selector: (state: T) => R;
|
|
220
220
|
meta?: Record<string, any>;
|
|
221
221
|
shouldTrigger?: (prev: R | undefined, current: R) => boolean;
|
|
222
|
+
notifyAfterSubscribe?: boolean;
|
|
222
223
|
}
|
|
223
224
|
/**
|
|
224
225
|
* Тип для функции watcher
|
|
@@ -253,10 +254,6 @@ interface DispatchFunction<TParams, TResult> {
|
|
|
253
254
|
* Тип для фабрики создателей действий
|
|
254
255
|
*/
|
|
255
256
|
type ActionCreatorFactory = <TParams, TResult>(config: ActionDefinition<TParams, TResult>, executionOptions?: ActionExecutionOptions) => DispatchFunction<TParams, TResult>;
|
|
256
|
-
/**
|
|
257
|
-
* Тип для функции настройки действий
|
|
258
|
-
*/
|
|
259
|
-
type ActionsSetup<T extends Record<string, unknown>> = (create: ActionCreatorFactory, storage: IStorage<T>) => Record<string, DispatchFunction<any, any>>;
|
|
260
257
|
/**
|
|
261
258
|
* Извлекает тип результата из функции диспетчера
|
|
262
259
|
*/
|
|
@@ -360,4 +357,4 @@ declare function createDispatcher<TState extends Record<string, any>, TActions e
|
|
|
360
357
|
};
|
|
361
358
|
type CreateDispatcherType = ReturnType<typeof createDispatcher>;
|
|
362
359
|
|
|
363
|
-
export { type Action as A,
|
|
360
|
+
export { type Action as A, type CreateDispatcherType as C, type DispatchFunction as D, type EnhancedMiddlewareAPI as E, type TypedAction as T, type ValidateConfig as V, type WatcherFunction as W, type EnhancedMiddleware as a, type ActionDefinition as b, type ActionsSetupWithUtils as c, type ExtractResultType as d, type ActionsResult as e, type DispatchActions as f, type WatcherActions as g, type DispatcherMiddlewareAPI as h, type DispatcherMiddleware as i, Dispatcher as j, createDispatcher as k, type ExternalStates as l, type EffectBase as m, type Effect as n, type DispatcherActions as o, type ValidateMapRequestUtils as p, ofType as q, ofTypes as r, ofTypesWaitAll as s, selectorMap as t, selectorObject as u, validateMap as v, EffectsModule as w, createEffectBase as x, createEffect as y, combineEffects as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ export { I as ISelectorModule, S as SelectorAPI } from './selector.interface-CA5
|
|
|
3
3
|
export { LocalStorage, MemoryStorage, SelectorModule, StoragePluginModule, broadcastMiddleware } from './core.js';
|
|
4
4
|
export { B as BatchingMiddlewareOptions, C as ConfigureMiddlewares, D as DefaultMiddlewares, G as GetDefaultMiddleware, m as IEventEmitter, n as ILogger, d as IPlugin, f as IPluginExecutor, g as IPluginManager, j as IStorage, e as IStoragePlugin, I as IndexedDBStorage, r as IndexedDBStorageConfig, L as LocalStorageConfig, q as MemoryStorageConfig, M as Middleware, a as MiddlewareAPI, N as NextFunction, P as PluginContext, S as ShallowCompareMiddlewareOptions, b as StorageAction, o as StorageConfig, l as StorageEvent, k as StorageEvents, i as StorageInitStatus, c as StorageKeyType, h as StorageStatus, p as StorageType } from './storage.interface-2HKvqdAJ.js';
|
|
5
5
|
export { createSynapseCtx, useSelector, useStorageSubscribe } from './react.js';
|
|
6
|
-
export { A as Action, b as ActionDefinition,
|
|
6
|
+
export { A as Action, b as ActionDefinition, e as ActionsResult, c as ActionsSetupWithUtils, C as CreateDispatcherType, f as DispatchActions, D as DispatchFunction, j as Dispatcher, o as DispatcherActions, i as DispatcherMiddleware, h as DispatcherMiddlewareAPI, n as Effect, m as EffectBase, w as EffectsModule, a as EnhancedMiddleware, E as EnhancedMiddlewareAPI, l as ExternalStates, d as ExtractResultType, T as TypedAction, V as ValidateConfig, p as ValidateMapRequestUtils, g as WatcherActions, W as WatcherFunction, z as combineEffects, k as createDispatcher, y as createEffect, x as createEffectBase, q as ofType, r as ofTypes, s as ofTypesWaitAll, t as selectorMap, u as selectorObject, v as validateMap } from './dispatcher.module-DjMloBXe.js';
|
|
7
7
|
export { loggerDispatcherMiddleware } from './reactive.js';
|
|
8
8
|
export { EventBusConfig, EventBusEvent, EventBusState, createEventBus } from './utils.js';
|
|
9
|
-
export { A as AnySynapseStore, S as SynapseStoreBasic, a as SynapseStoreWithDispatcher, b as SynapseStoreWithEffects, c as createSynapse } from './createSynapse
|
|
9
|
+
export { A as AnySynapseStore, S as SynapseStoreBasic, a as SynapseStoreWithDispatcher, b as SynapseStoreWithEffects, c as createSynapse } from './createSynapse-CzeX0ONj.js';
|
|
10
10
|
import 'react';
|
|
11
11
|
import 'rxjs';
|