effector-storage 6.1.1 → 7.1.0

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.
Files changed (94) hide show
  1. package/README.md +7 -40
  2. package/async-storage/index.cjs.map +1 -1
  3. package/async-storage/index.d.cts +24 -0
  4. package/async-storage/index.d.ts +14 -18
  5. package/async-storage/index.js.flow +6 -5
  6. package/async-storage/index.js.map +1 -1
  7. package/async-storage/package.json +15 -2
  8. package/broadcast/index.cjs.map +1 -1
  9. package/broadcast/index.d.cts +94 -0
  10. package/broadcast/index.d.ts +63 -85
  11. package/broadcast/index.js.flow +23 -25
  12. package/broadcast/index.js.map +1 -1
  13. package/broadcast/package.json +15 -2
  14. package/core/index.cjs +1 -1
  15. package/core/index.cjs.map +1 -1
  16. package/core/index.d.cts +77 -0
  17. package/core/index.d.ts +53 -66
  18. package/core/index.js +1 -1
  19. package/core/index.js.flow +14 -14
  20. package/core/index.js.map +1 -1
  21. package/core/package.json +15 -2
  22. package/index.d.cts +97 -0
  23. package/index.d.ts +68 -99
  24. package/index.js.flow +27 -28
  25. package/local/index.d.cts +99 -0
  26. package/local/index.d.ts +67 -90
  27. package/local/index.js.flow +25 -27
  28. package/local/package.json +15 -2
  29. package/log/index.cjs.map +1 -1
  30. package/log/index.d.cts +19 -0
  31. package/log/index.d.ts +11 -11
  32. package/log/index.js.flow +6 -5
  33. package/log/index.js.map +1 -1
  34. package/log/package.json +15 -2
  35. package/memory/index.d.cts +95 -0
  36. package/memory/index.d.ts +63 -82
  37. package/memory/index.js.flow +23 -25
  38. package/memory/package.json +15 -2
  39. package/nil/index.cjs.map +1 -1
  40. package/nil/index.d.cts +18 -0
  41. package/nil/index.d.ts +10 -10
  42. package/nil/index.js.flow +6 -5
  43. package/nil/index.js.map +1 -1
  44. package/nil/package.json +15 -2
  45. package/package.json +101 -57
  46. package/query/index.cjs.map +1 -1
  47. package/query/index.d.cts +108 -0
  48. package/query/index.d.ts +75 -104
  49. package/query/index.js.flow +30 -28
  50. package/query/index.js.map +1 -1
  51. package/query/package.json +15 -2
  52. package/session/index.d.cts +99 -0
  53. package/session/index.d.ts +67 -90
  54. package/session/index.js.flow +25 -27
  55. package/session/package.json +15 -2
  56. package/storage/index.cjs.map +1 -1
  57. package/storage/index.d.cts +23 -0
  58. package/storage/index.d.ts +15 -22
  59. package/storage/index.js.flow +7 -6
  60. package/storage/index.js.map +1 -1
  61. package/storage/package.json +15 -2
  62. package/tools/index.cjs.map +1 -1
  63. package/tools/{index.cjs.d.ts → index.d.cts} +34 -58
  64. package/tools/index.d.ts +34 -58
  65. package/tools/index.js.flow +19 -16
  66. package/tools/index.js.map +1 -1
  67. package/tools/package.json +15 -2
  68. package/async-storage/index.cjs.d.ts +0 -28
  69. package/broadcast/index.cjs.d.ts +0 -116
  70. package/core/index.cjs.d.ts +0 -90
  71. package/index.cjs.d.ts +0 -128
  72. package/local/index.cjs.d.ts +0 -122
  73. package/log/index.cjs.d.ts +0 -19
  74. package/memory/index.cjs.d.ts +0 -114
  75. package/nil/index.cjs.d.ts +0 -18
  76. package/query/index.cjs.d.ts +0 -137
  77. package/rn/async/index.cjs +0 -2
  78. package/rn/async/index.cjs.d.ts +0 -120
  79. package/rn/async/index.cjs.map +0 -1
  80. package/rn/async/index.d.ts +0 -120
  81. package/rn/async/index.js +0 -2
  82. package/rn/async/index.js.flow +0 -135
  83. package/rn/async/index.js.map +0 -1
  84. package/rn/async/package.json +0 -8
  85. package/rn/encrypted/index.cjs +0 -2
  86. package/rn/encrypted/index.cjs.d.ts +0 -120
  87. package/rn/encrypted/index.cjs.map +0 -1
  88. package/rn/encrypted/index.d.ts +0 -120
  89. package/rn/encrypted/index.js +0 -2
  90. package/rn/encrypted/index.js.flow +0 -137
  91. package/rn/encrypted/index.js.map +0 -1
  92. package/rn/encrypted/package.json +0 -8
  93. package/session/index.cjs.d.ts +0 -122
  94. package/storage/index.cjs.d.ts +0 -30
package/README.md CHANGED
@@ -18,8 +18,6 @@ Small module for [Effector](https://github.com/effector/effector) ☄️ to sync
18
18
  - [with `sessionStorage`](#with-sessionstorage)
19
19
  - [with query string](#with-query-string)
20
20
  - [with `BroadcastChannel`](#with-broadcastchannel)
21
- - [with React Native AsyncStorage](#with-react-native-asyncstorage)
22
- - [with React Native EncryptedStorage](#with-react-native-encryptedstorage)
23
21
  - [extra adapters](#extra-adapters)
24
22
  - [Usage with domains](#usage-with-domains)
25
23
  - [Formulae](#formulae)
@@ -79,6 +77,8 @@ persist({ store: $counter })
79
77
 
80
78
  Stores, persisted in `localStorage`, are automatically synced between two (or more) windows/tabs. Also, they are synced between instances, so if you will persist two stores with the same key — each store will receive updates from another one.
81
79
 
80
+ ℹ️ If you need just basic bare minimum functionality, you can take a look at [`effector-localstorage`](https://github.com/ilyalesik/effector-localstorage) library. It has similar API, it much simpler and tinier.
81
+
82
82
  ### with `sessionStorage`
83
83
 
84
84
  Docs: [effector-storage/session](https://github.com/yumauri/effector-storage/tree/main/src/session/README.md)
@@ -116,42 +116,6 @@ You can sync stores across different browsing contexts (tabs, windows, workers),
116
116
  import { persist } from 'effector-storage/broadcast'
117
117
  ```
118
118
 
119
- ### with React Native AsyncStorage
120
-
121
- ❗️ Deprecated and will be removed in next major release, in favor of [`@effector-storage/react-native-async-storage`](https://github.com/yumauri/effector-storage-extras/tree/main/packages/react-native-async-storage).
122
-
123
- Docs: [effector-storage/rn/async](https://github.com/yumauri/effector-storage/tree/main/src/rn/async/README.md)
124
-
125
- ```javascript
126
- import { persist } from 'effector-storage/rn/async'
127
-
128
- // persist store `$counter` with key 'counter'
129
- persist({ store: $counter, key: 'counter' })
130
-
131
- // if your storage has a name, you can omit `key` field
132
- persist({ store: $counter })
133
- ```
134
-
135
- ⚠️ Note, that [AsyncStorage] is asynchronous, hence the name.
136
-
137
- ### with React Native EncryptedStorage
138
-
139
- ❗️ Deprecated and will be removed in next major release, in favor of [`@effector-storage/react-native-encrypted-storage`](https://github.com/yumauri/effector-storage-extras/tree/main/packages/react-native-encrypted-storage).
140
-
141
- Docs: [effector-storage/rn/encrypted](https://github.com/yumauri/effector-storage/tree/main/src/rn/encrypted/README.md)
142
-
143
- ```javascript
144
- import { persist } from 'effector-storage/rn/encrypted'
145
-
146
- // persist store `$counter` with key 'counter'
147
- persist({ store: $counter, key: 'counter' })
148
-
149
- // if your storage has a name, you can omit `key` field
150
- persist({ store: $counter })
151
- ```
152
-
153
- ⚠️ Note, that [EncryptedStorage] is asynchronous (it is based on [AsyncStorage] actually).
154
-
155
119
  ### extra adapters
156
120
 
157
121
  You can find a collection of useful adapters in [effector-storage-extras](https://github.com/yumauri/effector-storage-extras). That side repository was created in order to not bloat `effector-storage` with dependencies and adapters, which depends on other libraries.
@@ -332,7 +296,10 @@ Adapter is a function, which is called by the core `persist` function, and has f
332
296
 
333
297
  ```typescript
334
298
  interface StorageAdapter {
335
- <State>(key: string, update: (raw?: any) => any): {
299
+ <State>(
300
+ key: string,
301
+ update: (raw?: any) => void
302
+ ): {
336
303
  get(raw?: any, ctx?: any): State | Promise<State | undefined> | undefined
337
304
  set(value: State, ctx?: any): void
338
305
  }
@@ -380,7 +347,7 @@ persist({ store, adapter }) // <- use adapter
380
347
 
381
348
  ### Asynchronous storage adapter example
382
349
 
383
- Using asynchronous storage is just as simple. Once again, this is just a bare simple idea, without serialization and edge cases checks.
350
+ Using asynchronous storage is just as simple. Once again, this is just a bare simple idea, without serialization and edge cases checks. If you need to use React Native Async Storage, try [@effector-storage/react-native-async-storage](https://github.com/yumauri/effector-storage-extras/tree/main/packages/react-native-async-storage)) adapter instead.
384
351
 
385
352
  ```javascript
386
353
  import AsyncStorage from '@react-native-async-storage/async-storage'
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/async-storage/index.ts"],"sourcesContent":["import type { StorageAdapter } from '../types'\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<void>\n}\n\nexport interface AsyncStorageConfig {\n storage: () => AsyncStorage\n serialize?: (value: any) => string\n deserialize?: (value: string) => any\n}\n\n/**\n * Creates generic `AsyncStorage` adapter\n */\nasyncStorage.factory = true as const\nexport function asyncStorage({\n storage,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n}: AsyncStorageConfig): StorageAdapter {\n const adapter: StorageAdapter = <State>(key: string) => ({\n async get() {\n const item = await storage().getItem(key)\n return item === null ? undefined : deserialize(item)\n },\n\n async set(value: State) {\n await storage().setItem(key, serialize(value))\n },\n })\n\n try {\n adapter.keyArea = storage()\n } catch (error) {\n // do nothing\n }\n\n return adapter\n}\n"],"names":["asyncStorage","storage","serialize","JSON","stringify","deserialize","parse","adapter","key","async","item","getItem","undefined","value","setItem","keyArea","error","factory"],"mappings":"aAiBO,SAASA,GAAaC,QAC3BA,EAAOC,UACPA,EAAYC,KAAKC,UAASC,YAC1BA,EAAcF,KAAKG,QAEnB,IAAMC,EAAkCC,IAAiB,CACvDC,YACE,IAAMC,QAAaT,IAAUU,QAAQH,GACrC,OAAgB,OAATE,OAAgBE,EAAYP,EAAYK,EAChD,EAEDD,UAAUI,SACFZ,IAAUa,QAAQN,EAAKN,EAAUW,GACzC,IAGF,IACEN,EAAQQ,QAAUd,GACnB,CAAC,MAAOe,GACP,CAGF,OAAOT,CACT,CAxBAP,EAAaiB,SAAU"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/async-storage/index.ts"],"sourcesContent":["import type { StorageAdapter } from '../types'\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<void>\n}\n\nexport interface AsyncStorageConfig {\n storage: () => AsyncStorage\n serialize?: (value: any) => string\n deserialize?: (value: string) => any\n}\n\n/**\n * Creates generic `AsyncStorage` adapter\n */\nasyncStorage.factory = true as const\nexport function asyncStorage({\n storage,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n}: AsyncStorageConfig): StorageAdapter {\n const adapter: StorageAdapter = <State>(key: string) => ({\n async get() {\n const item = await storage().getItem(key)\n return item === null ? undefined : deserialize(item)\n },\n\n async set(value: State) {\n await storage().setItem(key, serialize(value))\n },\n })\n\n try {\n adapter.keyArea = storage()\n } catch (error) {\n // do nothing\n }\n\n return adapter\n}\n"],"names":["asyncStorage","storage","serialize","JSON","stringify","deserialize","parse","adapter","key","get","item","getItem","undefined","set","value","setItem","keyArea","error","factory"],"mappings":"aAiBO,SAASA,GAAaC,QAC3BA,EAAOC,UACPA,EAAYC,KAAKC,UAASC,YAC1BA,EAAcF,KAAKG,QAEnB,IAAMC,EAAkCC,IAAiB,CACvD,SAAMC,GACJ,IAAMC,QAAaT,IAAUU,QAAQH,GACrC,OAAgB,OAATE,OAAgBE,EAAYP,EAAYK,EAChD,EAED,SAAMG,CAAIC,SACFb,IAAUc,QAAQP,EAAKN,EAAUY,GACzC,IAGF,IACEP,EAAQS,QAAUf,GACnB,CAAC,MAAOgB,GACP,CAGF,OAAOV,CACT,CAxBAP,EAAakB,SAAU"}
@@ -0,0 +1,24 @@
1
+ interface StorageAdapter {
2
+ <State>(key: string, update: (raw?: any) => void): {
3
+ get(raw?: any, ctx?: any): State | Promise<State | undefined> | undefined;
4
+ set(value: State, ctx?: any): void;
5
+ };
6
+ keyArea?: any;
7
+ noop?: boolean;
8
+ }
9
+
10
+ interface AsyncStorage {
11
+ getItem: (key: string) => Promise<string | null>;
12
+ setItem: (key: string, value: string) => Promise<void>;
13
+ }
14
+ interface AsyncStorageConfig {
15
+ storage: () => AsyncStorage;
16
+ serialize?: (value: any) => string;
17
+ deserialize?: (value: string) => any;
18
+ }
19
+ declare function asyncStorage({ storage, serialize, deserialize, }: AsyncStorageConfig): StorageAdapter;
20
+ declare namespace asyncStorage {
21
+ var factory: true;
22
+ }
23
+
24
+ export { type AsyncStorage, type AsyncStorageConfig, asyncStorage };
@@ -1,28 +1,24 @@
1
1
  interface StorageAdapter {
2
- <State>(key: string, update: (raw?: any) => any): {
3
- get(raw?: any, ctx?: any): State | Promise<State | undefined> | undefined
4
- set(value: State, ctx?: any): void
5
- }
6
- keyArea?: any
7
- noop?: boolean
2
+ <State>(key: string, update: (raw?: any) => void): {
3
+ get(raw?: any, ctx?: any): State | Promise<State | undefined> | undefined;
4
+ set(value: State, ctx?: any): void;
5
+ };
6
+ keyArea?: any;
7
+ noop?: boolean;
8
8
  }
9
9
 
10
10
  interface AsyncStorage {
11
- getItem: (key: string) => Promise<string | null>
12
- setItem: (key: string, value: string) => Promise<void>
11
+ getItem: (key: string) => Promise<string | null>;
12
+ setItem: (key: string, value: string) => Promise<void>;
13
13
  }
14
14
  interface AsyncStorageConfig {
15
- storage: () => AsyncStorage
16
- serialize?: (value: any) => string
17
- deserialize?: (value: string) => any
15
+ storage: () => AsyncStorage;
16
+ serialize?: (value: any) => string;
17
+ deserialize?: (value: string) => any;
18
18
  }
19
- declare function asyncStorage({
20
- storage,
21
- serialize,
22
- deserialize,
23
- }: AsyncStorageConfig): StorageAdapter
19
+ declare function asyncStorage({ storage, serialize, deserialize, }: AsyncStorageConfig): StorageAdapter;
24
20
  declare namespace asyncStorage {
25
- var factory: true
21
+ var factory: true;
26
22
  }
27
23
 
28
- export { AsyncStorage, AsyncStorageConfig, asyncStorage }
24
+ export { type AsyncStorage, type AsyncStorageConfig, asyncStorage };
@@ -8,7 +8,7 @@
8
8
  declare interface StorageAdapter {
9
9
  <State>(
10
10
  key: string,
11
- update: (raw?: any) => any
11
+ update: (raw?: any) => void
12
12
  ): {
13
13
  get(raw?: any, ctx?: any): State | Promise<State | void> | void,
14
14
  set(value: State, ctx?: any): void,
@@ -26,11 +26,12 @@ declare interface AsyncStorageConfig {
26
26
  serialize?: (value: any) => string;
27
27
  deserialize?: (value: string) => any;
28
28
  }
29
- declare var asyncStorage: typeof npm$namespace$asyncStorage
29
+ declare var asyncStorage: typeof npm$namespace$asyncStorage;
30
30
 
31
31
  declare var npm$namespace$asyncStorage: {|
32
32
  (x: AsyncStorageConfig): StorageAdapter,
33
33
  factory: typeof asyncStorage$factory,
34
- |}
35
- declare var asyncStorage$factory: true
36
- declare export { AsyncStorage, AsyncStorageConfig, asyncStorage }
34
+ |};
35
+ declare var asyncStorage$factory: true;
36
+ export type { AsyncStorage, AsyncStorageConfig };
37
+ declare export { asyncStorage };
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/async-storage/index.ts"],"sourcesContent":["import type { StorageAdapter } from '../types'\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<void>\n}\n\nexport interface AsyncStorageConfig {\n storage: () => AsyncStorage\n serialize?: (value: any) => string\n deserialize?: (value: string) => any\n}\n\n/**\n * Creates generic `AsyncStorage` adapter\n */\nasyncStorage.factory = true as const\nexport function asyncStorage({\n storage,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n}: AsyncStorageConfig): StorageAdapter {\n const adapter: StorageAdapter = <State>(key: string) => ({\n async get() {\n const item = await storage().getItem(key)\n return item === null ? undefined : deserialize(item)\n },\n\n async set(value: State) {\n await storage().setItem(key, serialize(value))\n },\n })\n\n try {\n adapter.keyArea = storage()\n } catch (error) {\n // do nothing\n }\n\n return adapter\n}\n"],"names":["asyncStorage","storage","serialize","JSON","stringify","deserialize","parse","adapter","key","async","item","getItem","undefined","value","setItem","keyArea","error","factory"],"mappings":"AAiBO,SAASA,GAAaC,QAC3BA,EAAOC,UACPA,EAAYC,KAAKC,UAASC,YAC1BA,EAAcF,KAAKG,QAEnB,IAAMC,EAAkCC,IAAiB,CACvDC,YACE,IAAMC,QAAaT,IAAUU,QAAQH,GACrC,OAAgB,OAATE,OAAgBE,EAAYP,EAAYK,EAChD,EAEDD,UAAUI,SACFZ,IAAUa,QAAQN,EAAKN,EAAUW,GACzC,IAGF,IACEN,EAAQQ,QAAUd,GACnB,CAAC,MAAOe,GACP,CAGF,OAAOT,CACT,CAxBAP,EAAaiB,SAAU"}
1
+ {"version":3,"file":"index.js","sources":["../../src/async-storage/index.ts"],"sourcesContent":["import type { StorageAdapter } from '../types'\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<void>\n}\n\nexport interface AsyncStorageConfig {\n storage: () => AsyncStorage\n serialize?: (value: any) => string\n deserialize?: (value: string) => any\n}\n\n/**\n * Creates generic `AsyncStorage` adapter\n */\nasyncStorage.factory = true as const\nexport function asyncStorage({\n storage,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n}: AsyncStorageConfig): StorageAdapter {\n const adapter: StorageAdapter = <State>(key: string) => ({\n async get() {\n const item = await storage().getItem(key)\n return item === null ? undefined : deserialize(item)\n },\n\n async set(value: State) {\n await storage().setItem(key, serialize(value))\n },\n })\n\n try {\n adapter.keyArea = storage()\n } catch (error) {\n // do nothing\n }\n\n return adapter\n}\n"],"names":["asyncStorage","storage","serialize","JSON","stringify","deserialize","parse","adapter","key","get","item","getItem","undefined","set","value","setItem","keyArea","error","factory"],"mappings":"AAiBO,SAASA,GAAaC,QAC3BA,EAAOC,UACPA,EAAYC,KAAKC,UAASC,YAC1BA,EAAcF,KAAKG,QAEnB,IAAMC,EAAkCC,IAAiB,CACvD,SAAMC,GACJ,IAAMC,QAAaT,IAAUU,QAAQH,GACrC,OAAgB,OAATE,OAAgBE,EAAYP,EAAYK,EAChD,EAED,SAAMG,CAAIC,SACFb,IAAUc,QAAQP,EAAKN,EAAUY,GACzC,IAGF,IACEP,EAAQS,QAAUf,GACnB,CAAC,MAAOgB,GACP,CAGF,OAAOV,CACT,CAxBAP,EAAakB,SAAU"}
@@ -1,8 +1,21 @@
1
1
  {
2
2
  "type": "module",
3
3
  "sideEffects": false,
4
- "main": "index.cjs",
4
+ "types": "index.d.ts",
5
5
  "module": "index.js",
6
+ "main": "index.cjs",
6
7
  "react-native": "index.js",
7
- "types": "index.d.ts"
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "import": {
12
+ "types": "./index.d.ts",
13
+ "default": "./index.js"
14
+ },
15
+ "require": {
16
+ "types": "./index.d.cts",
17
+ "default": "./index.cjs"
18
+ }
19
+ }
20
+ }
8
21
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/broadcast/adapter.ts","../../src/broadcast/index.ts"],"sourcesContent":["import type { StorageAdapter } from '../types'\n\nexport interface BroadcastConfig {\n channel?: string\n}\n\n/**\n * BroadcastChannel instances cache\n */\nconst channels = new Map<string, BroadcastChannel>()\n\n/**\n * BroadcastChannel adapter factory\n */\nexport function adapter({\n channel = 'effector-storage',\n}: BroadcastConfig): StorageAdapter {\n let created: BroadcastChannel | undefined\n const bus = channels.get(channel) ?? (created = new BroadcastChannel(channel))\n if (created) channels.set(channel, created)\n\n const adapter: StorageAdapter = <State>(\n key: string,\n update: (raw?: any) => any\n ) => {\n bus.addEventListener('message', ({ data }) => {\n // according to e2e tests, chromium can call `message`\n // instead of `messageerror`, with `null` as message's data\n if (data == null) {\n update(() => {\n throw new Error('Unable to deserialize message')\n })\n } else if (data.key === key) {\n update(() => {\n return data.value\n })\n }\n })\n\n // I know only one case when this event can be fired:\n // if message was sent from page to shared worker, and it contains `SharedArrayBuffer`\n // https://bugs.webkit.org/show_bug.cgi?id=171216\n bus.addEventListener('messageerror', () => {\n update(() => {\n throw new Error('Unable to deserialize message')\n })\n })\n\n return {\n get(box?: () => State | undefined) {\n if (box) return box()\n },\n\n set(value: State) {\n bus.postMessage({ key, value })\n },\n }\n }\n\n adapter.keyArea = bus\n return adapter\n}\n","import type { Subscription } from 'effector'\nimport type {\n ConfigPersist as BaseConfigPersist,\n ConfigStore as BaseConfigStore,\n ConfigSourceTarget as BaseConfigSourceTarget,\n StorageAdapter,\n} from '../types'\nimport type { BroadcastConfig } from './adapter'\nimport { persist as base } from '../core'\nimport { nil } from '../nil'\nimport { adapter } from './adapter'\n\nexport type {\n Contract,\n Done,\n Fail,\n Finally,\n StorageAdapter,\n StorageAdapterFactory,\n} from '../types'\nexport type { BroadcastConfig } from './adapter'\n\nexport interface ConfigPersist extends BaseConfigPersist {}\n\nexport interface ConfigStore<State, Err = Error>\n extends BroadcastConfig,\n BaseConfigStore<State, Err> {}\n\nexport interface ConfigSourceTarget<State, Err = Error>\n extends BroadcastConfig,\n BaseConfigSourceTarget<State, Err> {}\n\nexport interface Persist {\n <State, Err = Error>(config: ConfigSourceTarget<State, Err>): Subscription\n <State, Err = Error>(config: ConfigStore<State, Err>): Subscription\n}\n\n/**\n * Function, checking if `BroadcastChannel` exists and accessible\n */\nfunction supports() {\n return typeof BroadcastChannel !== 'undefined'\n}\n\n/**\n * Creates BroadcastChannel string adapter\n */\nbroadcast.factory = true as const\nexport function broadcast(config?: BroadcastConfig): StorageAdapter {\n return supports()\n ? adapter({\n ...config,\n })\n : nil({ keyArea: 'broadcast' })\n}\n\n/**\n * Creates custom partially applied `persist`\n * with predefined BroadcastChannel adapter\n */\nexport function createPersist(defaults?: ConfigPersist): Persist {\n return (config) =>\n base({\n adapter: broadcast,\n ...defaults,\n ...config,\n })\n}\n\n/**\n * Default partially applied `persist`\n */\nexport const persist = createPersist()\n"],"names":["channels","Map","broadcast","config","BroadcastChannel","channel","created","bus","get","set","adapter","key","update","addEventListener","data","Error","value","box","postMessage","keyArea","nil","createPersist","defaults","base","factory","persist"],"mappings":"8EASMA,EAAW,IAAIC,ICuCd,SAASC,EAAUC,GACxB,MARmC,oBAArBC,iBD3BT,UAAiBC,QACtBA,EAAU,qBAEV,IAAIC,EACEC,EAAMP,EAASQ,IAAIH,KAAaC,EAAU,IAAIF,iBAAiBC,IACjEC,GAASN,EAASS,IAAIJ,EAASC,GAEnC,IAAMI,EAA0BA,CAC9BC,EACAC,KAEAL,EAAIM,iBAAiB,WAAW,EAAGC,WAGrB,MAARA,EACFF,GAAO,KACL,MAAM,IAAIG,MAAM,gCAAgC,IAEzCD,EAAKH,MAAQA,GACtBC,GAAO,IACEE,EAAKE,OAEhB,IAMFT,EAAIM,iBAAiB,gBAAgB,KACnCD,GAAO,KACL,MAAM,IAAIG,MAAM,gCAAgC,GAChD,IAGG,CACLP,IAAIS,GACF,GAAIA,EAAK,OAAOA,GACjB,EAEDR,IAAIO,GACFT,EAAIW,YAAY,CAAEP,MAAKK,SACzB,IAKJ,OADAN,EAAQS,QAAUZ,EACXG,CACT,CCXMA,CAAQ,IACHP,IAELiB,EAAAA,IAAI,CAAED,QAAS,aACrB,CAMO,SAASE,EAAcC,GAC5B,OAAQnB,GACNoB,EAAAA,QAAK,CACHb,QAASR,KACNoB,KACAnB,GAET,CApBAD,EAAUsB,SAAU,EAyBPC,IAAAA,EAAUJ"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/broadcast/adapter.ts","../../src/broadcast/index.ts"],"sourcesContent":["import type { StorageAdapter } from '../types'\n\nexport interface BroadcastConfig {\n channel?: string\n}\n\n/**\n * BroadcastChannel instances cache\n */\nconst channels = new Map<string, BroadcastChannel>()\n\n/**\n * BroadcastChannel adapter factory\n */\nexport function adapter({\n channel = 'effector-storage',\n}: BroadcastConfig): StorageAdapter {\n let created: BroadcastChannel | undefined\n const bus = channels.get(channel) ?? (created = new BroadcastChannel(channel))\n if (created) channels.set(channel, created)\n\n const adapter: StorageAdapter = <State>(\n key: string,\n update: (raw?: any) => void\n ) => {\n bus.addEventListener('message', ({ data }) => {\n // according to e2e tests, chromium can call `message`\n // instead of `messageerror`, with `null` as message's data\n if (data == null) {\n update(() => {\n throw new Error('Unable to deserialize message')\n })\n } else if (data.key === key) {\n update(() => {\n return data.value\n })\n }\n })\n\n // I know only one case when this event can be fired:\n // if message was sent from page to shared worker, and it contains `SharedArrayBuffer`\n // https://bugs.webkit.org/show_bug.cgi?id=171216\n bus.addEventListener('messageerror', () => {\n update(() => {\n throw new Error('Unable to deserialize message')\n })\n })\n\n return {\n get(box?: () => State | undefined) {\n if (box) return box()\n },\n\n set(value: State) {\n bus.postMessage({ key, value })\n },\n }\n }\n\n adapter.keyArea = bus\n return adapter\n}\n","import type { Subscription } from 'effector'\nimport type {\n ConfigPersist as BaseConfigPersist,\n ConfigStore as BaseConfigStore,\n ConfigSourceTarget as BaseConfigSourceTarget,\n StorageAdapter,\n} from '../types'\nimport type { BroadcastConfig } from './adapter'\nimport { persist as base } from '../core'\nimport { nil } from '../nil'\nimport { adapter } from './adapter'\n\nexport type {\n Contract,\n Done,\n Fail,\n Finally,\n StorageAdapter,\n StorageAdapterFactory,\n} from '../types'\nexport type { BroadcastConfig } from './adapter'\n\nexport interface ConfigPersist extends BaseConfigPersist {}\n\nexport interface ConfigStore<State, Err = Error>\n extends BroadcastConfig,\n BaseConfigStore<State, Err> {}\n\nexport interface ConfigSourceTarget<State, Err = Error>\n extends BroadcastConfig,\n BaseConfigSourceTarget<State, Err> {}\n\nexport interface Persist {\n <State, Err = Error>(config: ConfigSourceTarget<State, Err>): Subscription\n <State, Err = Error>(config: ConfigStore<State, Err>): Subscription\n}\n\n/**\n * Function, checking if `BroadcastChannel` exists and accessible\n */\nfunction supports() {\n return typeof BroadcastChannel !== 'undefined'\n}\n\n/**\n * Creates BroadcastChannel string adapter\n */\nbroadcast.factory = true as const\nexport function broadcast(config?: BroadcastConfig): StorageAdapter {\n return supports()\n ? adapter({\n ...config,\n })\n : nil({ keyArea: 'broadcast' })\n}\n\n/**\n * Creates custom partially applied `persist`\n * with predefined BroadcastChannel adapter\n */\nexport function createPersist(defaults?: ConfigPersist): Persist {\n return (config) =>\n base({\n adapter: broadcast,\n ...defaults,\n ...config,\n })\n}\n\n/**\n * Default partially applied `persist`\n */\nexport const persist = createPersist()\n"],"names":["channels","Map","broadcast","config","BroadcastChannel","channel","created","bus","get","set","adapter","key","update","addEventListener","data","Error","value","box","postMessage","keyArea","nil","createPersist","defaults","base","factory","persist"],"mappings":"8EASMA,EAAW,IAAIC,ICuCd,SAASC,EAAUC,GACxB,MARmC,oBAArBC,iBD3BT,UAAiBC,QACtBA,EAAU,qBAEV,IAAIC,EACEC,EAAMP,EAASQ,IAAIH,KAAaC,EAAU,IAAIF,iBAAiBC,IACjEC,GAASN,EAASS,IAAIJ,EAASC,GAEnC,IAAMI,EAA0BA,CAC9BC,EACAC,KAEAL,EAAIM,iBAAiB,WAAW,EAAGC,WAGrB,MAARA,EACFF,GAAO,KACL,MAAM,IAAIG,MAAM,gCAAgC,IAEzCD,EAAKH,MAAQA,GACtBC,GAAO,IACEE,EAAKE,OAEhB,IAMFT,EAAIM,iBAAiB,gBAAgB,KACnCD,GAAO,KACL,MAAM,IAAIG,MAAM,gCAAgC,GAChD,IAGG,CACLP,GAAAA,CAAIS,GACF,GAAIA,EAAK,OAAOA,GACjB,EAEDR,GAAAA,CAAIO,GACFT,EAAIW,YAAY,CAAEP,MAAKK,SACzB,IAKJ,OADAN,EAAQS,QAAUZ,EACXG,CACT,CCXMA,CAAQ,IACHP,IAELiB,EAAAA,IAAI,CAAED,QAAS,aACrB,CAMO,SAASE,EAAcC,GAC5B,OAAQnB,GACNoB,EAAAA,QAAK,CACHb,QAASR,KACNoB,KACAnB,GAET,CApBAD,EAAUsB,SAAU,EAyBPC,IAAAA,EAAUJ"}
@@ -0,0 +1,94 @@
1
+ import { Unit, Store, Event, Effect, Subscription } from 'effector';
2
+
3
+ interface StorageAdapter {
4
+ <State>(key: string, update: (raw?: any) => void): {
5
+ get(raw?: any, ctx?: any): State | Promise<State | undefined> | undefined;
6
+ set(value: State, ctx?: any): void;
7
+ };
8
+ keyArea?: any;
9
+ noop?: boolean;
10
+ }
11
+ interface StorageAdapterFactory<AdapterConfig> {
12
+ (config?: AdapterConfig): StorageAdapter;
13
+ factory: true;
14
+ }
15
+ type Contract<Data> = ((raw: unknown) => raw is Data) | {
16
+ isData: (raw: unknown) => raw is Data;
17
+ getErrorMessages: (raw: unknown) => string[];
18
+ };
19
+ type Done<State> = {
20
+ key: string;
21
+ keyPrefix: string;
22
+ operation: 'set' | 'get';
23
+ value: State;
24
+ };
25
+ type Fail<Err> = {
26
+ key: string;
27
+ keyPrefix: string;
28
+ operation: 'set' | 'get';
29
+ error: Err;
30
+ value?: any;
31
+ };
32
+ type Finally<State, Err> = (Done<State> & {
33
+ status: 'done';
34
+ }) | (Fail<Err> & {
35
+ status: 'fail';
36
+ });
37
+ interface ConfigPersist$1 {
38
+ pickup?: Unit<any>;
39
+ context?: Unit<any>;
40
+ keyPrefix?: string;
41
+ contract?: Contract<any>;
42
+ }
43
+ interface ConfigCommon<State, Err = Error> {
44
+ clock?: Unit<any>;
45
+ done?: Unit<Done<State>>;
46
+ fail?: Unit<Fail<Err>>;
47
+ finally?: Unit<Finally<State, Err>>;
48
+ pickup?: Unit<any>;
49
+ context?: Unit<any>;
50
+ key?: string;
51
+ keyPrefix?: string;
52
+ contract?: Contract<State | undefined>;
53
+ }
54
+ interface ConfigJustStore<State> {
55
+ store: Store<State>;
56
+ }
57
+ interface ConfigJustSourceTarget<State> {
58
+ source: Store<State> | Event<State> | Effect<State, any, any>;
59
+ target: Store<State> | Event<State> | Effect<State, any, any>;
60
+ }
61
+ interface ConfigStore$1<State, Err = Error> extends ConfigCommon<State, Err>, ConfigJustStore<State> {
62
+ }
63
+ interface ConfigSourceTarget$1<State, Err = Error> extends ConfigCommon<State, Err>, ConfigJustSourceTarget<State> {
64
+ }
65
+
66
+ interface BroadcastConfig {
67
+ channel?: string;
68
+ }
69
+
70
+ interface ConfigPersist extends ConfigPersist$1 {
71
+ }
72
+ interface ConfigStore<State, Err = Error> extends BroadcastConfig, ConfigStore$1<State, Err> {
73
+ }
74
+ interface ConfigSourceTarget<State, Err = Error> extends BroadcastConfig, ConfigSourceTarget$1<State, Err> {
75
+ }
76
+ interface Persist {
77
+ <State, Err = Error>(config: ConfigSourceTarget<State, Err>): Subscription;
78
+ <State, Err = Error>(config: ConfigStore<State, Err>): Subscription;
79
+ }
80
+ declare function broadcast(config?: BroadcastConfig): StorageAdapter;
81
+ declare namespace broadcast {
82
+ var factory: true;
83
+ }
84
+ /**
85
+ * Creates custom partially applied `persist`
86
+ * with predefined BroadcastChannel adapter
87
+ */
88
+ declare function createPersist(defaults?: ConfigPersist): Persist;
89
+ /**
90
+ * Default partially applied `persist`
91
+ */
92
+ declare const persist: Persist;
93
+
94
+ export { type BroadcastConfig, type ConfigPersist, type ConfigSourceTarget, type ConfigStore, type Contract, type Done, type Fail, type Finally, type Persist, type StorageAdapter, type StorageAdapterFactory, broadcast, createPersist, persist };
@@ -1,116 +1,94 @@
1
- import { Unit, Store, Event, Effect, Subscription } from 'effector'
1
+ import { Unit, Store, Event, Effect, Subscription } from 'effector';
2
2
 
3
3
  interface StorageAdapter {
4
- <State>(key: string, update: (raw?: any) => any): {
5
- get(raw?: any, ctx?: any): State | Promise<State | undefined> | undefined
6
- set(value: State, ctx?: any): void
7
- }
8
- keyArea?: any
9
- noop?: boolean
4
+ <State>(key: string, update: (raw?: any) => void): {
5
+ get(raw?: any, ctx?: any): State | Promise<State | undefined> | undefined;
6
+ set(value: State, ctx?: any): void;
7
+ };
8
+ keyArea?: any;
9
+ noop?: boolean;
10
10
  }
11
11
  interface StorageAdapterFactory<AdapterConfig> {
12
- (config?: AdapterConfig): StorageAdapter
13
- factory: true
12
+ (config?: AdapterConfig): StorageAdapter;
13
+ factory: true;
14
14
  }
15
- type Contract<Data> =
16
- | ((raw: unknown) => raw is Data)
17
- | {
18
- isData: (raw: unknown) => raw is Data
19
- getErrorMessages: (raw: unknown) => string[]
20
- }
15
+ type Contract<Data> = ((raw: unknown) => raw is Data) | {
16
+ isData: (raw: unknown) => raw is Data;
17
+ getErrorMessages: (raw: unknown) => string[];
18
+ };
21
19
  type Done<State> = {
22
- key: string
23
- keyPrefix: string
24
- operation: 'set' | 'get'
25
- value: State
26
- }
20
+ key: string;
21
+ keyPrefix: string;
22
+ operation: 'set' | 'get';
23
+ value: State;
24
+ };
27
25
  type Fail<Err> = {
28
- key: string
29
- keyPrefix: string
30
- operation: 'set' | 'get'
31
- error: Err
32
- value?: any
33
- }
34
- type Finally<State, Err> =
35
- | (Done<State> & {
36
- status: 'done'
37
- })
38
- | (Fail<Err> & {
39
- status: 'fail'
40
- })
26
+ key: string;
27
+ keyPrefix: string;
28
+ operation: 'set' | 'get';
29
+ error: Err;
30
+ value?: any;
31
+ };
32
+ type Finally<State, Err> = (Done<State> & {
33
+ status: 'done';
34
+ }) | (Fail<Err> & {
35
+ status: 'fail';
36
+ });
41
37
  interface ConfigPersist$1 {
42
- pickup?: Unit<any>
43
- context?: Unit<any>
44
- keyPrefix?: string
45
- contract?: Contract<any>
38
+ pickup?: Unit<any>;
39
+ context?: Unit<any>;
40
+ keyPrefix?: string;
41
+ contract?: Contract<any>;
46
42
  }
47
43
  interface ConfigCommon<State, Err = Error> {
48
- clock?: Unit<any>
49
- done?: Unit<Done<State>>
50
- fail?: Unit<Fail<Err>>
51
- finally?: Unit<Finally<State, Err>>
52
- pickup?: Unit<any>
53
- context?: Unit<any>
54
- key?: string
55
- keyPrefix?: string
56
- contract?: Contract<State | undefined>
44
+ clock?: Unit<any>;
45
+ done?: Unit<Done<State>>;
46
+ fail?: Unit<Fail<Err>>;
47
+ finally?: Unit<Finally<State, Err>>;
48
+ pickup?: Unit<any>;
49
+ context?: Unit<any>;
50
+ key?: string;
51
+ keyPrefix?: string;
52
+ contract?: Contract<State | undefined>;
57
53
  }
58
54
  interface ConfigJustStore<State> {
59
- store: Store<State>
55
+ store: Store<State>;
60
56
  }
61
57
  interface ConfigJustSourceTarget<State> {
62
- source: Store<State> | Event<State> | Effect<State, any, any>
63
- target: Store<State> | Event<State> | Effect<State, any, any>
58
+ source: Store<State> | Event<State> | Effect<State, any, any>;
59
+ target: Store<State> | Event<State> | Effect<State, any, any>;
60
+ }
61
+ interface ConfigStore$1<State, Err = Error> extends ConfigCommon<State, Err>, ConfigJustStore<State> {
62
+ }
63
+ interface ConfigSourceTarget$1<State, Err = Error> extends ConfigCommon<State, Err>, ConfigJustSourceTarget<State> {
64
64
  }
65
- interface ConfigStore$1<State, Err = Error>
66
- extends ConfigCommon<State, Err>,
67
- ConfigJustStore<State> {}
68
- interface ConfigSourceTarget$1<State, Err = Error>
69
- extends ConfigCommon<State, Err>,
70
- ConfigJustSourceTarget<State> {}
71
65
 
72
66
  interface BroadcastConfig {
73
- channel?: string
67
+ channel?: string;
74
68
  }
75
69
 
76
- interface ConfigPersist extends ConfigPersist$1 {}
77
- interface ConfigStore<State, Err = Error>
78
- extends BroadcastConfig,
79
- ConfigStore$1<State, Err> {}
80
- interface ConfigSourceTarget<State, Err = Error>
81
- extends BroadcastConfig,
82
- ConfigSourceTarget$1<State, Err> {}
70
+ interface ConfigPersist extends ConfigPersist$1 {
71
+ }
72
+ interface ConfigStore<State, Err = Error> extends BroadcastConfig, ConfigStore$1<State, Err> {
73
+ }
74
+ interface ConfigSourceTarget<State, Err = Error> extends BroadcastConfig, ConfigSourceTarget$1<State, Err> {
75
+ }
83
76
  interface Persist {
84
- <State, Err = Error>(config: ConfigSourceTarget<State, Err>): Subscription
85
- <State, Err = Error>(config: ConfigStore<State, Err>): Subscription
77
+ <State, Err = Error>(config: ConfigSourceTarget<State, Err>): Subscription;
78
+ <State, Err = Error>(config: ConfigStore<State, Err>): Subscription;
86
79
  }
87
- declare function broadcast(config?: BroadcastConfig): StorageAdapter
80
+ declare function broadcast(config?: BroadcastConfig): StorageAdapter;
88
81
  declare namespace broadcast {
89
- var factory: true
82
+ var factory: true;
90
83
  }
91
84
  /**
92
85
  * Creates custom partially applied `persist`
93
86
  * with predefined BroadcastChannel adapter
94
87
  */
95
- declare function createPersist(defaults?: ConfigPersist): Persist
88
+ declare function createPersist(defaults?: ConfigPersist): Persist;
96
89
  /**
97
90
  * Default partially applied `persist`
98
91
  */
99
- declare const persist: Persist
92
+ declare const persist: Persist;
100
93
 
101
- export {
102
- BroadcastConfig,
103
- ConfigPersist,
104
- ConfigSourceTarget,
105
- ConfigStore,
106
- Contract,
107
- Done,
108
- Fail,
109
- Finally,
110
- Persist,
111
- StorageAdapter,
112
- StorageAdapterFactory,
113
- broadcast,
114
- createPersist,
115
- persist,
116
- }
94
+ export { type BroadcastConfig, type ConfigPersist, type ConfigSourceTarget, type ConfigStore, type Contract, type Done, type Fail, type Finally, type Persist, type StorageAdapter, type StorageAdapterFactory, broadcast, createPersist, persist };
@@ -5,11 +5,11 @@
5
5
  * @flow
6
6
  */
7
7
 
8
- import { Unit, Store, Event, Effect, Subscription } from 'effector'
8
+ import { Unit, Store, Event, Effect, Subscription } from "effector";
9
9
  declare interface StorageAdapter {
10
10
  <State>(
11
11
  key: string,
12
- update: (raw?: any) => any
12
+ update: (raw?: any) => void
13
13
  ): {
14
14
  get(raw?: any, ctx?: any): State | Promise<State | void> | void,
15
15
  set(value: State, ctx?: any): void,
@@ -28,37 +28,37 @@ declare type Contract<Data> =
28
28
  isData: (raw: mixed) => boolean,
29
29
  getErrorMessages: (raw: mixed) => string[],
30
30
  ...
31
- }
31
+ };
32
32
  declare type Done<State> = {
33
33
  key: string,
34
34
  keyPrefix: string,
35
- operation: 'set' | 'get',
35
+ operation: "set" | "get",
36
36
  value: State,
37
37
  ...
38
- }
38
+ };
39
39
  declare type Fail<Err> = {
40
40
  key: string,
41
41
  keyPrefix: string,
42
- operation: 'set' | 'get',
42
+ operation: "set" | "get",
43
43
  error: Err,
44
44
  value?: any,
45
45
  ...
46
- }
46
+ };
47
47
  declare type Finally<State, Err> =
48
48
  | {
49
49
  ...Done<State>,
50
50
  ...{
51
- status: 'done',
51
+ status: "done",
52
52
  ...
53
53
  },
54
54
  }
55
55
  | {
56
56
  ...Fail<Err>,
57
57
  ...{
58
- status: 'fail',
58
+ status: "fail",
59
59
  ...
60
60
  },
61
- }
61
+ };
62
62
  declare interface ConfigPersist$1 {
63
63
  pickup?: Unit<any>;
64
64
  context?: Unit<any>;
@@ -87,36 +87,36 @@ declare type ConfigStore$1<State, Err = Error> = { ... } & ConfigCommon<
87
87
  State,
88
88
  Err
89
89
  > &
90
- ConfigJustStore<State>
90
+ ConfigJustStore<State>;
91
91
  declare type ConfigSourceTarget$1<State, Err = Error> = { ... } & ConfigCommon<
92
92
  State,
93
93
  Err
94
94
  > &
95
- ConfigJustSourceTarget<State>
95
+ ConfigJustSourceTarget<State>;
96
96
  declare interface BroadcastConfig {
97
97
  channel?: string;
98
98
  }
99
- declare type ConfigPersist = { ... } & ConfigPersist$1
99
+ declare type ConfigPersist = { ... } & ConfigPersist$1;
100
100
  declare type ConfigStore<State, Err = Error> = { ... } & BroadcastConfig &
101
- ConfigStore$1<State, Err>
101
+ ConfigStore$1<State, Err>;
102
102
  declare type ConfigSourceTarget<State, Err = Error> = {
103
103
  ...
104
104
  } & BroadcastConfig &
105
- ConfigSourceTarget$1<State, Err>
105
+ ConfigSourceTarget$1<State, Err>;
106
106
  declare interface Persist {
107
107
  <State, Err>(config: ConfigSourceTarget<State, Err>): Subscription;
108
108
  <State, Err>(config: ConfigStore<State, Err>): Subscription;
109
109
  }
110
- declare var broadcast: typeof npm$namespace$broadcast
110
+ declare var broadcast: typeof npm$namespace$broadcast;
111
111
 
112
112
  declare var npm$namespace$broadcast: {|
113
113
  (config?: BroadcastConfig): StorageAdapter,
114
114
  factory: typeof broadcast$factory,
115
- |}
116
- declare var broadcast$factory: true
117
- declare function createPersist(defaults?: ConfigPersist): Persist
118
- declare var persist: Persist
119
- declare export {
115
+ |};
116
+ declare var broadcast$factory: true;
117
+ declare function createPersist(defaults?: ConfigPersist): Persist;
118
+ declare var persist: Persist;
119
+ export type {
120
120
  BroadcastConfig,
121
121
  ConfigPersist,
122
122
  ConfigSourceTarget,
@@ -128,7 +128,5 @@ declare export {
128
128
  Persist,
129
129
  StorageAdapter,
130
130
  StorageAdapterFactory,
131
- broadcast,
132
- createPersist,
133
- persist,
134
- }
131
+ };
132
+ declare export { broadcast, createPersist, persist };