-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathslices.ts
More file actions
74 lines (65 loc) · 2.06 KB
/
Copy pathslices.ts
File metadata and controls
74 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { produceWithPatches, type WritableDraft } from 'immer';
import { emitStorePatches, ImmerActions } from './immer';
type InferState<Slices> = Slices extends [
SliceConfig<infer Name, infer State, infer Actions>,
...infer Rest,
]
? { [K in Name]: State } & {
[K in keyof Actions]: (...args: Parameters<Actions[K]>) => void;
} & InferState<Rest>
: unknown;
export function withSlices<
Slices extends SliceConfig<
string,
Record<string, any>,
Record<string, Action<any>>
>[],
>(...slices: [...Slices]) {
return (
set: (
fn: (prevState: InferState<Slices>) => Partial<InferState<Slices>>,
) => void,
get: () => InferState<Slices>,
) => {
const state: Record<string, any> = {};
for (const slice of slices) {
state[slice.name] = slice.value;
for (const [name, action] of Object.entries(slice.actions)) {
state[name] = (...args: any[]) => {
set(prevState => {
const [nextState, patches] = produceWithPatches(prevState, draft =>
action(...args)(draft[slice.name], get),
);
if (patches.length > 0) emitStorePatches(patches);
return nextState;
});
};
(state[name] as any)[ImmerActions] = (state: any, ...args: any[]) => {
action(...args)(state[slice.name], get);
};
}
}
return state as InferState<Slices>;
};
}
export type SliceConfig<
Name extends string,
Value extends Record<string, any>,
Actions extends Record<string, Action<Value>>,
> = {
name: Name;
value: Value;
actions: Actions;
};
type SliceImmerArgs<Value> = [state: WritableDraft<Value>, get: () => Value];
type SliceVanillaArgs<Value> = [state: Value, get: () => Value];
export type Action<Value extends Record<string, any>> = (
...args: any[]
) => (state: WritableDraft<Value>, get: () => Value) => void;
export function createSlice<
Name extends string,
Value extends Record<string, any>,
Actions extends Record<string, Action<Value>>,
>(config: SliceConfig<Name, Value, Actions>) {
return config;
}