coaction 0.0.1 → 0.1.1
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 +30 -0
- package/dist/coaction.cjs.d.mts +2 -0
- package/dist/coaction.cjs.d.ts +2 -0
- package/dist/coaction.cjs.js +661 -0
- package/dist/coaction.cjs.mjs +5 -0
- package/dist/coaction.esm.js +655 -0
- package/dist/coaction.umd.min.js +2 -0
- package/dist/coaction.umd.min.js.map +1 -0
- package/dist/declarations/src/binder.d.ts +27 -0
- package/dist/declarations/src/create.d.ts +5 -0
- package/dist/declarations/src/index.d.ts +4 -0
- package/dist/declarations/src/interface.d.ts +224 -0
- package/dist/declarations/src/wrapStore.d.ts +5 -0
- package/package.json +48 -8
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Store } from "./interface.js";
|
|
2
|
+
/**
|
|
3
|
+
* createBinder is a function to create a binder for the 3rd party store.
|
|
4
|
+
*/
|
|
5
|
+
export declare function createBinder<F = (...args: any[]) => any>({ handleState, handleStore }: {
|
|
6
|
+
/**
|
|
7
|
+
* handleState is a function to handle the state object.
|
|
8
|
+
*/
|
|
9
|
+
handleState: <T extends object = object>(state: T) => {
|
|
10
|
+
/**
|
|
11
|
+
* copyState is a copy of the state object.
|
|
12
|
+
*/
|
|
13
|
+
copyState: T;
|
|
14
|
+
/**
|
|
15
|
+
* key is the key of the state object.
|
|
16
|
+
*/
|
|
17
|
+
key?: keyof T;
|
|
18
|
+
/**
|
|
19
|
+
* bind is a function to bind the state object.
|
|
20
|
+
*/
|
|
21
|
+
bind: (state: T) => T;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* handleStore is a function to handle the store object.
|
|
25
|
+
*/
|
|
26
|
+
handleStore: (store: Store<object>, rawState: object, state: object) => void;
|
|
27
|
+
}): F;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { create } from "./create.js";
|
|
2
|
+
export { createBinder } from "./binder.js";
|
|
3
|
+
export { wrapStore } from "./wrapStore.js";
|
|
4
|
+
export type { Store, StoreOptions, ISlices, Slice, Slices, Middleware, ClientStoreOptions, SliceState, Asyncify, StoreWithAsyncFunction as AsyncStore } from "./interface.js";
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import type { Transport } from 'data-transport';
|
|
2
|
+
import type { Draft, Patches } from 'mutative';
|
|
3
|
+
export type ISlices<T = any> = Record<string, T>;
|
|
4
|
+
export type DeepPartial<T> = {
|
|
5
|
+
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
|
|
6
|
+
};
|
|
7
|
+
export type Listener = () => void;
|
|
8
|
+
export interface Store<T extends ISlices = ISlices> {
|
|
9
|
+
/**
|
|
10
|
+
* The name of the store.
|
|
11
|
+
*/
|
|
12
|
+
name: string;
|
|
13
|
+
/**
|
|
14
|
+
* Set the next state.
|
|
15
|
+
*/
|
|
16
|
+
setState: (
|
|
17
|
+
/**
|
|
18
|
+
* The next state.
|
|
19
|
+
*/
|
|
20
|
+
next: DeepPartial<T> | ((draft: Draft<T>) => any) | null,
|
|
21
|
+
/**
|
|
22
|
+
* The updater is used to update the state.
|
|
23
|
+
*/
|
|
24
|
+
updater?: (next: DeepPartial<T> | ((draft: Draft<T>) => any) | null) => [] | [T, Patches, Patches]) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Get the current state.
|
|
27
|
+
*/
|
|
28
|
+
getState: () => T;
|
|
29
|
+
/**
|
|
30
|
+
* Subscribe to the state changes.
|
|
31
|
+
*/
|
|
32
|
+
subscribe: (listener: Listener) => () => void;
|
|
33
|
+
/**
|
|
34
|
+
* Unsubscribe all listeners and dispose the transport.
|
|
35
|
+
*/
|
|
36
|
+
destroy: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* The store is shared in the web worker, shared worker, or other process.
|
|
39
|
+
*/
|
|
40
|
+
share?: 'main' | 'client' | false;
|
|
41
|
+
/**
|
|
42
|
+
* The transport is used to communicate between the main thread and the worker or shared worker.
|
|
43
|
+
*/
|
|
44
|
+
transport?: Transport;
|
|
45
|
+
/**
|
|
46
|
+
* The store is a slices.
|
|
47
|
+
*/
|
|
48
|
+
isSliceStore: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* apply the patches to the state.
|
|
51
|
+
*/
|
|
52
|
+
apply: (state?: T, patches?: Patches) => void;
|
|
53
|
+
/**
|
|
54
|
+
* the pure state is used to get the state without the methods and getters.
|
|
55
|
+
*/
|
|
56
|
+
getPureState: () => T;
|
|
57
|
+
/**
|
|
58
|
+
* Get the initial state.
|
|
59
|
+
*/
|
|
60
|
+
getInitialState: () => T;
|
|
61
|
+
/**
|
|
62
|
+
* Get the raw instance via the initial state.
|
|
63
|
+
*/
|
|
64
|
+
toRaw?: (key: any) => any;
|
|
65
|
+
/**
|
|
66
|
+
* The patch is used to update the state.
|
|
67
|
+
*/
|
|
68
|
+
patch?: (option: {
|
|
69
|
+
patches: Patches;
|
|
70
|
+
inversePatches: Patches;
|
|
71
|
+
}) => {
|
|
72
|
+
patches: Patches;
|
|
73
|
+
inversePatches: Patches;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* The act is used to run the function in the action.
|
|
77
|
+
*/
|
|
78
|
+
act?: <T extends () => any>(fn: T) => ReturnType<T>;
|
|
79
|
+
/**
|
|
80
|
+
* The trace is used to trace the action
|
|
81
|
+
*/
|
|
82
|
+
trace?: (options: {
|
|
83
|
+
/**
|
|
84
|
+
* The id of the method.
|
|
85
|
+
*/
|
|
86
|
+
id: string;
|
|
87
|
+
/**
|
|
88
|
+
* The method name.
|
|
89
|
+
*/
|
|
90
|
+
method: string;
|
|
91
|
+
/**
|
|
92
|
+
* The slice key.
|
|
93
|
+
*/
|
|
94
|
+
sliceKey?: string;
|
|
95
|
+
/**
|
|
96
|
+
* The parameters of the method.
|
|
97
|
+
*/
|
|
98
|
+
parameters?: any[];
|
|
99
|
+
/**
|
|
100
|
+
* The result of the method.
|
|
101
|
+
*/
|
|
102
|
+
result?: any;
|
|
103
|
+
}) => void;
|
|
104
|
+
}
|
|
105
|
+
export type InternalEvents = {
|
|
106
|
+
/**
|
|
107
|
+
* Update the state in the worker or shared worker.
|
|
108
|
+
*/
|
|
109
|
+
update(options: {
|
|
110
|
+
/**
|
|
111
|
+
* The patches is used to update the state.
|
|
112
|
+
*/
|
|
113
|
+
patches: Patches;
|
|
114
|
+
/**
|
|
115
|
+
* The sequence is used to ensure the sequence of the state.
|
|
116
|
+
*/
|
|
117
|
+
sequence: number;
|
|
118
|
+
}): Promise<void>;
|
|
119
|
+
};
|
|
120
|
+
export type ExternalEvents = {
|
|
121
|
+
/**
|
|
122
|
+
* Execute the function in the worker or shared worker.
|
|
123
|
+
*/
|
|
124
|
+
execute(keys: string[], args: any[]): Promise<any>;
|
|
125
|
+
/**
|
|
126
|
+
* Full sync the state with the worker or shared worker.
|
|
127
|
+
*/
|
|
128
|
+
fullSync(): Promise<{
|
|
129
|
+
state: string;
|
|
130
|
+
sequence: number;
|
|
131
|
+
}>;
|
|
132
|
+
};
|
|
133
|
+
interface Getter<T extends ISlices> {
|
|
134
|
+
<P extends any[], R>(getDeps: (store: T) => readonly [...P] | [...P], selector: (...args: P) => R): R;
|
|
135
|
+
(): T;
|
|
136
|
+
}
|
|
137
|
+
export type Slice<T extends ISlices> = (
|
|
138
|
+
/**
|
|
139
|
+
* The setState is used to update the state.
|
|
140
|
+
*/
|
|
141
|
+
set: Store<T>['setState'],
|
|
142
|
+
/**
|
|
143
|
+
* The getState is used to get the state.
|
|
144
|
+
*/
|
|
145
|
+
get: Getter<T>,
|
|
146
|
+
/**
|
|
147
|
+
* The store is used to store the state.
|
|
148
|
+
*/
|
|
149
|
+
store: Store<T>) => T;
|
|
150
|
+
export type Slices<T extends ISlices, K extends keyof T> = (
|
|
151
|
+
/**
|
|
152
|
+
* The setState is used to update the state.
|
|
153
|
+
*/
|
|
154
|
+
set: Store<T>['setState'],
|
|
155
|
+
/**
|
|
156
|
+
* The getState is used to get the state.
|
|
157
|
+
*/
|
|
158
|
+
get: Getter<T>,
|
|
159
|
+
/**
|
|
160
|
+
* The store is used to store the state.
|
|
161
|
+
*/
|
|
162
|
+
store: Store<T>) => T[K];
|
|
163
|
+
export type Middleware<T extends CreateState> = (store: Store<T>) => Store<T>;
|
|
164
|
+
export type SliceState<T extends Record<string, Slice<any>>> = {
|
|
165
|
+
[K in keyof T]: ReturnType<T[K]>;
|
|
166
|
+
};
|
|
167
|
+
export type StoreOptions<T extends CreateState> = {
|
|
168
|
+
/**
|
|
169
|
+
* The name of the store.
|
|
170
|
+
*/
|
|
171
|
+
name?: string;
|
|
172
|
+
transport?: Transport;
|
|
173
|
+
workerType?: 'SharedWorkerInternal' | 'WebWorkerInternal';
|
|
174
|
+
middlewares?: Middleware<T>[];
|
|
175
|
+
/**
|
|
176
|
+
* enable patches
|
|
177
|
+
*/
|
|
178
|
+
enablePatches?: boolean;
|
|
179
|
+
};
|
|
180
|
+
export type ClientStoreOptions<T extends CreateState> = {
|
|
181
|
+
/**
|
|
182
|
+
* The name of the store.
|
|
183
|
+
*/
|
|
184
|
+
name?: string;
|
|
185
|
+
middlewares?: Middleware<T>[];
|
|
186
|
+
} & ClientTransportOptions;
|
|
187
|
+
export interface ClientTransportOptions {
|
|
188
|
+
workerType?: 'WebWorkerClient' | 'SharedWorkerClient';
|
|
189
|
+
clientTransport?: Transport<any>;
|
|
190
|
+
worker?: SharedWorker | Worker;
|
|
191
|
+
}
|
|
192
|
+
export type Asyncify<T extends object, D extends true | false> = {
|
|
193
|
+
[K in keyof T]: T[K] extends (...args: any[]) => any ? (...args: Parameters<T[K]>) => Promise<ReturnType<T[K]>> : D extends false ? T[K] : {
|
|
194
|
+
[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];
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
export type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & (() => Asyncify<T, D>);
|
|
198
|
+
export type StoreReturn<T extends object> = Store<T> & ((...args: any[]) => T);
|
|
199
|
+
export type CreateState = ISlices | Record<string, Slice<any>>;
|
|
200
|
+
export type Creator = {
|
|
201
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
202
|
+
<T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
|
|
203
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
|
|
204
|
+
<T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
205
|
+
};
|
|
206
|
+
export type ClientTransport = (Transport<{
|
|
207
|
+
listen: InternalEvents;
|
|
208
|
+
emit: ExternalEvents;
|
|
209
|
+
}> & {
|
|
210
|
+
/**
|
|
211
|
+
* onConnect is called when the transport is connected.
|
|
212
|
+
*/
|
|
213
|
+
onConnect?: (fn: () => void) => void;
|
|
214
|
+
}) | undefined;
|
|
215
|
+
export type StoreTransport = (Transport<{
|
|
216
|
+
listen: ExternalEvents;
|
|
217
|
+
emit: InternalEvents;
|
|
218
|
+
}> & {
|
|
219
|
+
/**
|
|
220
|
+
* onConnect is called when the transport is connected.
|
|
221
|
+
*/
|
|
222
|
+
onConnect?: (fn: () => void) => void;
|
|
223
|
+
}) | undefined;
|
|
224
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Store, StoreReturn } from "./interface.js";
|
|
2
|
+
/**
|
|
3
|
+
* wrapStore is a function to wrap the store and return function to get the state with store.
|
|
4
|
+
*/
|
|
5
|
+
export declare const wrapStore: (store: Store<any>, getState?: (...args: any[]) => any) => StoreReturn<any>;
|
package/package.json
CHANGED
|
@@ -1,11 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "coaction",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A sleek JavaScript library designed for high-performance and multithreading web apps.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"coaction"
|
|
7
|
+
],
|
|
8
|
+
"authors": [
|
|
9
|
+
"Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/unadlib/coaction/tree/main/packages/core#readme",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"main": "dist/coaction.cjs.js",
|
|
14
|
+
"module": "dist/coaction.esm.js",
|
|
15
|
+
"umd:main": "dist/coaction.umd.min.js",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": {
|
|
19
|
+
"import": "./dist/coaction.cjs.mjs",
|
|
20
|
+
"default": "./dist/coaction.cjs.js"
|
|
21
|
+
},
|
|
22
|
+
"module": "./dist/coaction.esm.js",
|
|
23
|
+
"import": "./dist/coaction.cjs.mjs",
|
|
24
|
+
"default": "./dist/coaction.cjs.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+ssh://git@github.com/unadlib/coaction.git"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/unadlib/coaction/issues"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {},
|
|
41
|
+
"preconstruct": {
|
|
42
|
+
"umdName": "Coaction",
|
|
43
|
+
"entrypoints": [
|
|
44
|
+
"./index.ts"
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"data-transport": "^4.5.0",
|
|
49
|
+
"mutative": "^1.1.0"
|
|
50
|
+
}
|
|
11
51
|
}
|