-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
thunk.ts
92 lines (85 loc) · 2.54 KB
/
thunk.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { createStore, Thunk, thunk } from 'easy-peasy';
interface Injections {
fetch: () => Promise<void>;
}
interface AuditModel {
logs: string[];
log: Thunk<AuditModel, string, Injections, StoreModel, Promise<number>>;
empty: Thunk<AuditModel>;
syncThunk: Thunk<AuditModel, undefined, undefined, StoreModel, string>;
optionalPayloadThunk: Thunk<AuditModel, { foo: string } | void>;
optionalPayloadThunkTwo: Thunk<AuditModel, { foo: string } | undefined>;
optionalPayloadThunkThree: Thunk<AuditModel, { foo: string } | null>;
}
interface StoreModel {
audit: AuditModel;
}
const model: StoreModel = {
audit: {
logs: [],
log: thunk(
async (
actions,
payload,
{
injections,
getState,
getStoreActions,
getStoreState,
dispatch,
meta,
},
) => {
actions.log(payload);
getState().logs.length;
getStoreState().audit.logs.length;
injections.fetch().then(() => 'done');
dispatch({ type: 'FOO' });
getStoreActions().audit.log('foo');
meta.parent.concat(['foo']);
meta.path.concat(['foo']);
return 1;
},
),
syncThunk: thunk((actions, payload) => {
return 'Woot!';
}),
empty: thunk(() => {}),
optionalPayloadThunk: thunk((actions, payload) => {
const foo = payload?.foo.substr(0, 3);
if (payload == null) {
return;
}
payload.foo.substr(0, 3);
}),
optionalPayloadThunkTwo: thunk((actions, payload) => {
const foo = payload?.foo.substr(0, 3);
}),
optionalPayloadThunkThree: thunk((actions, payload) => {
const foo = payload?.foo.substr(0, 3);
}),
},
};
const store = createStore(model);
store.getActions().audit.optionalPayloadThunk();
store.getActions().audit.optionalPayloadThunk({ foo: 'bar' });
store.getActions().audit.optionalPayloadThunkTwo();
store.getActions().audit.optionalPayloadThunkTwo({ foo: 'bar' });
// @ts-expect-error
store.getActions().audit.optionalPayloadThunkThree();
store.getActions().audit.optionalPayloadThunkThree(null);
store.getActions().audit.optionalPayloadThunkThree({ foo: 'bar' });
// @ts-expect-error
store.getActions().audit.optionalPayloadThunk(1);
store.getActions().audit.syncThunk().toUpperCase();
store
.getActions()
.audit.log('foo')
.then((result) => result + 1);
// @ts-expect-error
store.getActions().audit.log(1);
// @ts-expect-error
store.getActions().audit.log();
store.getActions().audit.empty();
// @ts-expect-error
store.getActions().audit.empty('foo');