coaction 0.1.5 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/{declarations/src/interface.d.ts → index.d.mts} +135 -61
- package/dist/index.d.ts +290 -0
- package/dist/index.js +841 -0
- package/dist/index.mjs +818 -0
- package/package.json +24 -22
- package/dist/coaction.cjs.d.mts +0 -2
- package/dist/coaction.cjs.d.ts +0 -2
- package/dist/coaction.cjs.js +0 -679
- package/dist/coaction.cjs.mjs +0 -5
- package/dist/coaction.esm.js +0 -673
- package/dist/coaction.umd.min.js +0 -2
- package/dist/coaction.umd.min.js.map +0 -1
- package/dist/declarations/src/binder.d.ts +0 -28
- package/dist/declarations/src/create.d.ts +0 -5
- package/dist/declarations/src/index.d.ts +0 -4
- package/dist/declarations/src/internal.d.ts +0 -48
- package/dist/declarations/src/wrapStore.d.ts +0 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Michael Lin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -25,6 +25,18 @@ const useStore = create((set) => ({
|
|
|
25
25
|
}));
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
+
### Store Shape Mode (`sliceMode`)
|
|
29
|
+
|
|
30
|
+
`create()` uses `sliceMode: 'auto'` by default. You can force behavior explicitly:
|
|
31
|
+
|
|
32
|
+
- `sliceMode: 'single'`: treat object input as a single store.
|
|
33
|
+
- `sliceMode: 'slices'`: require object-of-slice-functions input.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
create({ ping: () => 'pong' }, { sliceMode: 'single' });
|
|
37
|
+
create({ counter: (set) => ({ count: 0 }) }, { sliceMode: 'slices' });
|
|
38
|
+
```
|
|
39
|
+
|
|
28
40
|
## Documentation
|
|
29
41
|
|
|
30
42
|
You can find the documentation [here](https://github.com/unadlib/coaction).
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Transport } from 'data-transport';
|
|
2
|
+
import { Draft, Patches } from 'mutative';
|
|
3
|
+
|
|
4
|
+
type ISlices<T = any> = Record<string, T>;
|
|
5
|
+
type DeepPartial<T> = {
|
|
5
6
|
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
|
|
6
7
|
};
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
type Listener = () => void;
|
|
9
|
+
interface Store<T extends ISlices = ISlices> {
|
|
9
10
|
/**
|
|
10
11
|
* The name of the store.
|
|
11
12
|
*/
|
|
@@ -94,39 +95,11 @@ export interface Store<T extends ISlices = ISlices> {
|
|
|
94
95
|
result?: any;
|
|
95
96
|
}) => void;
|
|
96
97
|
}
|
|
97
|
-
export type InternalEvents = {
|
|
98
|
-
/**
|
|
99
|
-
* Update the state in the worker or shared worker.
|
|
100
|
-
*/
|
|
101
|
-
update(options: {
|
|
102
|
-
/**
|
|
103
|
-
* The patches is used to update the state.
|
|
104
|
-
*/
|
|
105
|
-
patches: Patches;
|
|
106
|
-
/**
|
|
107
|
-
* The sequence is used to ensure the sequence of the state.
|
|
108
|
-
*/
|
|
109
|
-
sequence: number;
|
|
110
|
-
}): Promise<void>;
|
|
111
|
-
};
|
|
112
|
-
export type ExternalEvents = {
|
|
113
|
-
/**
|
|
114
|
-
* Execute the function in the worker or shared worker.
|
|
115
|
-
*/
|
|
116
|
-
execute(keys: string[], args: any[]): Promise<any>;
|
|
117
|
-
/**
|
|
118
|
-
* Full sync the state with the worker or shared worker.
|
|
119
|
-
*/
|
|
120
|
-
fullSync(): Promise<{
|
|
121
|
-
state: string;
|
|
122
|
-
sequence: number;
|
|
123
|
-
}>;
|
|
124
|
-
};
|
|
125
98
|
interface Getter<T extends ISlices> {
|
|
126
99
|
<P extends any[], R>(getDeps: (store: T) => readonly [...P] | [...P], selector: (...args: P) => R): R;
|
|
127
100
|
(): T;
|
|
128
101
|
}
|
|
129
|
-
|
|
102
|
+
type Slice<T extends ISlices> = (
|
|
130
103
|
/**
|
|
131
104
|
* The setState is used to update the state.
|
|
132
105
|
*/
|
|
@@ -139,7 +112,7 @@ get: Getter<T>,
|
|
|
139
112
|
* The store is used to store the state.
|
|
140
113
|
*/
|
|
141
114
|
store: Store<T>) => T;
|
|
142
|
-
|
|
115
|
+
type Slices<T extends ISlices, K extends keyof T> = (
|
|
143
116
|
/**
|
|
144
117
|
* The setState is used to update the state.
|
|
145
118
|
*/
|
|
@@ -152,11 +125,11 @@ get: Getter<T>,
|
|
|
152
125
|
* The store is used to store the state.
|
|
153
126
|
*/
|
|
154
127
|
store: Store<T>) => T[K];
|
|
155
|
-
|
|
156
|
-
|
|
128
|
+
type Middleware<T extends CreateState> = (store: Store<T>) => Store<T>;
|
|
129
|
+
type SliceState<T extends Record<string, Slice<any>>> = {
|
|
157
130
|
[K in keyof T]: ReturnType<T[K]>;
|
|
158
131
|
};
|
|
159
|
-
|
|
132
|
+
type StoreOptions<T extends CreateState> = {
|
|
160
133
|
/**
|
|
161
134
|
* The name of the store.
|
|
162
135
|
*/
|
|
@@ -168,49 +141,150 @@ export type StoreOptions<T extends CreateState> = {
|
|
|
168
141
|
* enable patches
|
|
169
142
|
*/
|
|
170
143
|
enablePatches?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* control how createState should be interpreted.
|
|
146
|
+
* - auto: infer from createState shape.
|
|
147
|
+
* - slices: force slices mode.
|
|
148
|
+
* - single: force single-store mode.
|
|
149
|
+
*/
|
|
150
|
+
sliceMode?: 'auto' | 'slices' | 'single';
|
|
171
151
|
};
|
|
172
|
-
|
|
152
|
+
type ClientStoreOptions<T extends CreateState> = {
|
|
173
153
|
/**
|
|
174
154
|
* The name of the store.
|
|
175
155
|
*/
|
|
176
156
|
name?: string;
|
|
177
157
|
middlewares?: Middleware<T>[];
|
|
158
|
+
/**
|
|
159
|
+
* control how createState should be interpreted.
|
|
160
|
+
* - auto: infer from createState shape.
|
|
161
|
+
* - slices: force slices mode.
|
|
162
|
+
* - single: force single-store mode.
|
|
163
|
+
*/
|
|
164
|
+
sliceMode?: 'auto' | 'slices' | 'single';
|
|
178
165
|
} & ClientTransportOptions;
|
|
179
|
-
|
|
166
|
+
interface ClientTransportOptions {
|
|
180
167
|
workerType?: 'WebWorkerClient' | 'SharedWorkerClient';
|
|
181
168
|
clientTransport?: Transport<any>;
|
|
182
169
|
worker?: SharedWorker | Worker;
|
|
183
170
|
}
|
|
184
|
-
|
|
171
|
+
type Asyncify<T extends object, D extends true | false> = {
|
|
185
172
|
[K in keyof T]: T[K] extends (...args: any[]) => any ? (...args: Parameters<T[K]>) => Promise<ReturnType<T[K]>> : D extends false ? T[K] : {
|
|
186
173
|
[P in keyof T[K]]: T[K][P] extends (...args: any[]) => any ? (...args: Parameters<T[K][P]>) => Promise<ReturnType<T[K][P]>> : T[K][P];
|
|
187
174
|
};
|
|
188
175
|
};
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
176
|
+
type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & (() => Asyncify<T, D>);
|
|
177
|
+
type StoreReturn<T extends object> = Store<T> & ((...args: any[]) => T);
|
|
178
|
+
type CreateState = ISlices | Record<string, Slice<any>>;
|
|
179
|
+
type Creator = {
|
|
193
180
|
<T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
194
181
|
<T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
|
|
195
182
|
<T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
|
|
196
183
|
<T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
197
184
|
};
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Create a simple store or a shared store. The shared store can be used in a worker or another thread.
|
|
188
|
+
*/
|
|
189
|
+
declare const create: Creator;
|
|
190
|
+
|
|
191
|
+
interface Internal<T extends CreateState = CreateState> {
|
|
192
|
+
/**
|
|
193
|
+
* The store module.
|
|
194
|
+
*/
|
|
195
|
+
module: T;
|
|
196
|
+
/**
|
|
197
|
+
* The root state.
|
|
198
|
+
*/
|
|
199
|
+
rootState: T | Draft<T>;
|
|
200
|
+
/**
|
|
201
|
+
* The backup state.
|
|
202
|
+
*/
|
|
203
|
+
backupState: T | Draft<T>;
|
|
204
|
+
/**
|
|
205
|
+
* Finalize the draft.
|
|
206
|
+
*/
|
|
207
|
+
finalizeDraft: () => [T, Patches, Patches];
|
|
208
|
+
/**
|
|
209
|
+
* The mutable instance.
|
|
210
|
+
*/
|
|
211
|
+
mutableInstance: any;
|
|
212
|
+
/**
|
|
213
|
+
* The sequence number.
|
|
214
|
+
*/
|
|
215
|
+
sequence: number;
|
|
202
216
|
/**
|
|
203
|
-
*
|
|
217
|
+
* Whether the batch is running.
|
|
204
218
|
*/
|
|
205
|
-
|
|
206
|
-
}) | undefined;
|
|
207
|
-
export type StoreTransport = (Transport<{
|
|
208
|
-
listen: ExternalEvents;
|
|
209
|
-
emit: InternalEvents;
|
|
210
|
-
}> & {
|
|
219
|
+
isBatching: boolean;
|
|
211
220
|
/**
|
|
212
|
-
*
|
|
221
|
+
* The listeners.
|
|
213
222
|
*/
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
223
|
+
listeners: Set<Listener>;
|
|
224
|
+
/**
|
|
225
|
+
* The act is used to run the function in the action for mutable state.
|
|
226
|
+
*/
|
|
227
|
+
actMutable?: <T extends () => any>(fn: T) => ReturnType<T>;
|
|
228
|
+
/**
|
|
229
|
+
* Get the mutable raw instance via the initial state.
|
|
230
|
+
*/
|
|
231
|
+
toMutableRaw?: (key: any) => any;
|
|
232
|
+
/**
|
|
233
|
+
* The update immutable function.
|
|
234
|
+
*/
|
|
235
|
+
updateImmutable?: (state: T) => void;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* createBinder is a function to create a binder for the 3rd party store.
|
|
240
|
+
*/
|
|
241
|
+
declare function createBinder<F = (...args: any[]) => any>({ handleState, handleStore }: {
|
|
242
|
+
/**
|
|
243
|
+
* handleState is a function to handle the state object.
|
|
244
|
+
*/
|
|
245
|
+
handleState: <T extends object = object>(state: T) => {
|
|
246
|
+
/**
|
|
247
|
+
* copyState is a copy of the state object.
|
|
248
|
+
*/
|
|
249
|
+
copyState: T;
|
|
250
|
+
/**
|
|
251
|
+
* key is the key of the state object.
|
|
252
|
+
*/
|
|
253
|
+
key?: keyof T;
|
|
254
|
+
/**
|
|
255
|
+
* bind is a function to bind the state object.
|
|
256
|
+
*/
|
|
257
|
+
bind: (state: T) => T;
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* handleStore is a function to handle the store object.
|
|
261
|
+
*/
|
|
262
|
+
handleStore: (
|
|
263
|
+
/**
|
|
264
|
+
* Coaction store
|
|
265
|
+
*/
|
|
266
|
+
store: Store<object>,
|
|
267
|
+
/**
|
|
268
|
+
* The raw state object from 3rd party library.
|
|
269
|
+
*/
|
|
270
|
+
rawState: object,
|
|
271
|
+
/**
|
|
272
|
+
* 3rd party library state object to Coaction state object.
|
|
273
|
+
*/
|
|
274
|
+
state: object,
|
|
275
|
+
/**
|
|
276
|
+
* internal Coaction API.
|
|
277
|
+
*/
|
|
278
|
+
internal: Internal<object>,
|
|
279
|
+
/**
|
|
280
|
+
* the key of the slice state object.
|
|
281
|
+
*/
|
|
282
|
+
key?: string) => void;
|
|
283
|
+
}): F;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* wrapStore is a function to wrap the store and return function to get the state with store.
|
|
287
|
+
*/
|
|
288
|
+
declare const wrapStore: <T extends object>(store: Store<T>, getState?: (...args: unknown[]) => T) => StoreReturn<T>;
|
|
289
|
+
|
|
290
|
+
export { type StoreWithAsyncFunction as AsyncStore, type Asyncify, type ClientStoreOptions, type ISlices, type Middleware, type Slice, type SliceState, type Slices, type Store, type StoreOptions, create, createBinder, wrapStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { Transport } from 'data-transport';
|
|
2
|
+
import { Draft, Patches } from 'mutative';
|
|
3
|
+
|
|
4
|
+
type ISlices<T = any> = Record<string, T>;
|
|
5
|
+
type DeepPartial<T> = {
|
|
6
|
+
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
|
|
7
|
+
};
|
|
8
|
+
type Listener = () => void;
|
|
9
|
+
interface Store<T extends ISlices = ISlices> {
|
|
10
|
+
/**
|
|
11
|
+
* The name of the store.
|
|
12
|
+
*/
|
|
13
|
+
name: string;
|
|
14
|
+
/**
|
|
15
|
+
* Set the next state.
|
|
16
|
+
*/
|
|
17
|
+
setState: (
|
|
18
|
+
/**
|
|
19
|
+
* The next state.
|
|
20
|
+
*/
|
|
21
|
+
next: DeepPartial<T> | ((draft: Draft<T>) => any) | null,
|
|
22
|
+
/**
|
|
23
|
+
* The updater is used to update the state.
|
|
24
|
+
*/
|
|
25
|
+
updater?: (next: DeepPartial<T> | ((draft: Draft<T>) => any) | null) => [] | [T, Patches, Patches]) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Get the current state.
|
|
28
|
+
*/
|
|
29
|
+
getState: () => T;
|
|
30
|
+
/**
|
|
31
|
+
* Subscribe to the state changes, and return the unsubscribe function.
|
|
32
|
+
*/
|
|
33
|
+
subscribe: (listener: Listener) => () => void;
|
|
34
|
+
/**
|
|
35
|
+
* Unsubscribe all listeners and dispose the transport.
|
|
36
|
+
*/
|
|
37
|
+
destroy: () => void;
|
|
38
|
+
/**
|
|
39
|
+
* The store is shared in the web worker, shared worker, or other process.
|
|
40
|
+
*/
|
|
41
|
+
share?: 'main' | 'client' | false;
|
|
42
|
+
/**
|
|
43
|
+
* The transport is used to communicate between the main thread and the worker or shared worker.
|
|
44
|
+
*/
|
|
45
|
+
transport?: Transport;
|
|
46
|
+
/**
|
|
47
|
+
* The store is a slices.
|
|
48
|
+
*/
|
|
49
|
+
isSliceStore: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* apply the patches to the state.
|
|
52
|
+
*/
|
|
53
|
+
apply: (state?: T, patches?: Patches) => void;
|
|
54
|
+
/**
|
|
55
|
+
* the pure state is used to get the state without the methods and getters.
|
|
56
|
+
*/
|
|
57
|
+
getPureState: () => T;
|
|
58
|
+
/**
|
|
59
|
+
* Get the initial state.
|
|
60
|
+
*/
|
|
61
|
+
getInitialState: () => T;
|
|
62
|
+
/**
|
|
63
|
+
* The patch is used to update the state.
|
|
64
|
+
*/
|
|
65
|
+
patch?: (option: {
|
|
66
|
+
patches: Patches;
|
|
67
|
+
inversePatches: Patches;
|
|
68
|
+
}) => {
|
|
69
|
+
patches: Patches;
|
|
70
|
+
inversePatches: Patches;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* The trace is used to trace the action
|
|
74
|
+
*/
|
|
75
|
+
trace?: (options: {
|
|
76
|
+
/**
|
|
77
|
+
* The id of the method.
|
|
78
|
+
*/
|
|
79
|
+
id: string;
|
|
80
|
+
/**
|
|
81
|
+
* The method name.
|
|
82
|
+
*/
|
|
83
|
+
method: string;
|
|
84
|
+
/**
|
|
85
|
+
* The slice key.
|
|
86
|
+
*/
|
|
87
|
+
sliceKey?: string;
|
|
88
|
+
/**
|
|
89
|
+
* The parameters of the method.
|
|
90
|
+
*/
|
|
91
|
+
parameters?: any[];
|
|
92
|
+
/**
|
|
93
|
+
* The result of the method.
|
|
94
|
+
*/
|
|
95
|
+
result?: any;
|
|
96
|
+
}) => void;
|
|
97
|
+
}
|
|
98
|
+
interface Getter<T extends ISlices> {
|
|
99
|
+
<P extends any[], R>(getDeps: (store: T) => readonly [...P] | [...P], selector: (...args: P) => R): R;
|
|
100
|
+
(): T;
|
|
101
|
+
}
|
|
102
|
+
type Slice<T extends ISlices> = (
|
|
103
|
+
/**
|
|
104
|
+
* The setState is used to update the state.
|
|
105
|
+
*/
|
|
106
|
+
set: Store<T>['setState'],
|
|
107
|
+
/**
|
|
108
|
+
* The getState is used to get the state.
|
|
109
|
+
*/
|
|
110
|
+
get: Getter<T>,
|
|
111
|
+
/**
|
|
112
|
+
* The store is used to store the state.
|
|
113
|
+
*/
|
|
114
|
+
store: Store<T>) => T;
|
|
115
|
+
type Slices<T extends ISlices, K extends keyof T> = (
|
|
116
|
+
/**
|
|
117
|
+
* The setState is used to update the state.
|
|
118
|
+
*/
|
|
119
|
+
set: Store<T>['setState'],
|
|
120
|
+
/**
|
|
121
|
+
* The getState is used to get the state.
|
|
122
|
+
*/
|
|
123
|
+
get: Getter<T>,
|
|
124
|
+
/**
|
|
125
|
+
* The store is used to store the state.
|
|
126
|
+
*/
|
|
127
|
+
store: Store<T>) => T[K];
|
|
128
|
+
type Middleware<T extends CreateState> = (store: Store<T>) => Store<T>;
|
|
129
|
+
type SliceState<T extends Record<string, Slice<any>>> = {
|
|
130
|
+
[K in keyof T]: ReturnType<T[K]>;
|
|
131
|
+
};
|
|
132
|
+
type StoreOptions<T extends CreateState> = {
|
|
133
|
+
/**
|
|
134
|
+
* The name of the store.
|
|
135
|
+
*/
|
|
136
|
+
name?: string;
|
|
137
|
+
transport?: Transport;
|
|
138
|
+
workerType?: 'SharedWorkerInternal' | 'WebWorkerInternal';
|
|
139
|
+
middlewares?: Middleware<T>[];
|
|
140
|
+
/**
|
|
141
|
+
* enable patches
|
|
142
|
+
*/
|
|
143
|
+
enablePatches?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* control how createState should be interpreted.
|
|
146
|
+
* - auto: infer from createState shape.
|
|
147
|
+
* - slices: force slices mode.
|
|
148
|
+
* - single: force single-store mode.
|
|
149
|
+
*/
|
|
150
|
+
sliceMode?: 'auto' | 'slices' | 'single';
|
|
151
|
+
};
|
|
152
|
+
type ClientStoreOptions<T extends CreateState> = {
|
|
153
|
+
/**
|
|
154
|
+
* The name of the store.
|
|
155
|
+
*/
|
|
156
|
+
name?: string;
|
|
157
|
+
middlewares?: Middleware<T>[];
|
|
158
|
+
/**
|
|
159
|
+
* control how createState should be interpreted.
|
|
160
|
+
* - auto: infer from createState shape.
|
|
161
|
+
* - slices: force slices mode.
|
|
162
|
+
* - single: force single-store mode.
|
|
163
|
+
*/
|
|
164
|
+
sliceMode?: 'auto' | 'slices' | 'single';
|
|
165
|
+
} & ClientTransportOptions;
|
|
166
|
+
interface ClientTransportOptions {
|
|
167
|
+
workerType?: 'WebWorkerClient' | 'SharedWorkerClient';
|
|
168
|
+
clientTransport?: Transport<any>;
|
|
169
|
+
worker?: SharedWorker | Worker;
|
|
170
|
+
}
|
|
171
|
+
type Asyncify<T extends object, D extends true | false> = {
|
|
172
|
+
[K in keyof T]: T[K] extends (...args: any[]) => any ? (...args: Parameters<T[K]>) => Promise<ReturnType<T[K]>> : D extends false ? T[K] : {
|
|
173
|
+
[P in keyof T[K]]: T[K][P] extends (...args: any[]) => any ? (...args: Parameters<T[K][P]>) => Promise<ReturnType<T[K][P]>> : T[K][P];
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & (() => Asyncify<T, D>);
|
|
177
|
+
type StoreReturn<T extends object> = Store<T> & ((...args: any[]) => T);
|
|
178
|
+
type CreateState = ISlices | Record<string, Slice<any>>;
|
|
179
|
+
type Creator = {
|
|
180
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
181
|
+
<T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
|
|
182
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
|
|
183
|
+
<T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Create a simple store or a shared store. The shared store can be used in a worker or another thread.
|
|
188
|
+
*/
|
|
189
|
+
declare const create: Creator;
|
|
190
|
+
|
|
191
|
+
interface Internal<T extends CreateState = CreateState> {
|
|
192
|
+
/**
|
|
193
|
+
* The store module.
|
|
194
|
+
*/
|
|
195
|
+
module: T;
|
|
196
|
+
/**
|
|
197
|
+
* The root state.
|
|
198
|
+
*/
|
|
199
|
+
rootState: T | Draft<T>;
|
|
200
|
+
/**
|
|
201
|
+
* The backup state.
|
|
202
|
+
*/
|
|
203
|
+
backupState: T | Draft<T>;
|
|
204
|
+
/**
|
|
205
|
+
* Finalize the draft.
|
|
206
|
+
*/
|
|
207
|
+
finalizeDraft: () => [T, Patches, Patches];
|
|
208
|
+
/**
|
|
209
|
+
* The mutable instance.
|
|
210
|
+
*/
|
|
211
|
+
mutableInstance: any;
|
|
212
|
+
/**
|
|
213
|
+
* The sequence number.
|
|
214
|
+
*/
|
|
215
|
+
sequence: number;
|
|
216
|
+
/**
|
|
217
|
+
* Whether the batch is running.
|
|
218
|
+
*/
|
|
219
|
+
isBatching: boolean;
|
|
220
|
+
/**
|
|
221
|
+
* The listeners.
|
|
222
|
+
*/
|
|
223
|
+
listeners: Set<Listener>;
|
|
224
|
+
/**
|
|
225
|
+
* The act is used to run the function in the action for mutable state.
|
|
226
|
+
*/
|
|
227
|
+
actMutable?: <T extends () => any>(fn: T) => ReturnType<T>;
|
|
228
|
+
/**
|
|
229
|
+
* Get the mutable raw instance via the initial state.
|
|
230
|
+
*/
|
|
231
|
+
toMutableRaw?: (key: any) => any;
|
|
232
|
+
/**
|
|
233
|
+
* The update immutable function.
|
|
234
|
+
*/
|
|
235
|
+
updateImmutable?: (state: T) => void;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* createBinder is a function to create a binder for the 3rd party store.
|
|
240
|
+
*/
|
|
241
|
+
declare function createBinder<F = (...args: any[]) => any>({ handleState, handleStore }: {
|
|
242
|
+
/**
|
|
243
|
+
* handleState is a function to handle the state object.
|
|
244
|
+
*/
|
|
245
|
+
handleState: <T extends object = object>(state: T) => {
|
|
246
|
+
/**
|
|
247
|
+
* copyState is a copy of the state object.
|
|
248
|
+
*/
|
|
249
|
+
copyState: T;
|
|
250
|
+
/**
|
|
251
|
+
* key is the key of the state object.
|
|
252
|
+
*/
|
|
253
|
+
key?: keyof T;
|
|
254
|
+
/**
|
|
255
|
+
* bind is a function to bind the state object.
|
|
256
|
+
*/
|
|
257
|
+
bind: (state: T) => T;
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* handleStore is a function to handle the store object.
|
|
261
|
+
*/
|
|
262
|
+
handleStore: (
|
|
263
|
+
/**
|
|
264
|
+
* Coaction store
|
|
265
|
+
*/
|
|
266
|
+
store: Store<object>,
|
|
267
|
+
/**
|
|
268
|
+
* The raw state object from 3rd party library.
|
|
269
|
+
*/
|
|
270
|
+
rawState: object,
|
|
271
|
+
/**
|
|
272
|
+
* 3rd party library state object to Coaction state object.
|
|
273
|
+
*/
|
|
274
|
+
state: object,
|
|
275
|
+
/**
|
|
276
|
+
* internal Coaction API.
|
|
277
|
+
*/
|
|
278
|
+
internal: Internal<object>,
|
|
279
|
+
/**
|
|
280
|
+
* the key of the slice state object.
|
|
281
|
+
*/
|
|
282
|
+
key?: string) => void;
|
|
283
|
+
}): F;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* wrapStore is a function to wrap the store and return function to get the state with store.
|
|
287
|
+
*/
|
|
288
|
+
declare const wrapStore: <T extends object>(store: Store<T>, getState?: (...args: unknown[]) => T) => StoreReturn<T>;
|
|
289
|
+
|
|
290
|
+
export { type StoreWithAsyncFunction as AsyncStore, type Asyncify, type ClientStoreOptions, type ISlices, type Middleware, type Slice, type SliceState, type Slices, type Store, type StoreOptions, create, createBinder, wrapStore };
|