-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathissue802.ts
More file actions
80 lines (70 loc) · 1.94 KB
/
issue802.ts
File metadata and controls
80 lines (70 loc) · 1.94 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
75
76
77
78
79
80
import { EffectOn, effectOn, Action, action } from 'easy-peasy';
type AppEffectOn<TModel extends object> = EffectOn<
TModel,
StoreModel,
Injections
>;
interface Injections {
doSomething: () => Promise<void>;
}
interface TodosModel {
items: { id: number; text: string }[];
foo: string;
setFoo: Action<TodosModel, string>;
onStateChanged: AppEffectOn<TodosModel>;
}
interface HelloModel {
name: string;
sayHelloTo: Action<HelloModel, string>;
onStateChanged: AppEffectOn<HelloModel>;
}
interface StoreModel {
hello: HelloModel;
todos: TodosModel;
rootData: number;
}
const model: StoreModel = {
rootData: 42,
hello: {
name: 'Arthur',
sayHelloTo: action((state, payload) => {
state.name = payload;
}),
onStateChanged: effectOn([(state) => state.name], (_, __, helpers) => {
console.log(`Hello, ${helpers.getState().name}`);
}),
},
todos: {
items: [],
foo: 'bar',
setFoo: action((state, payload) => {
state.foo = payload;
}),
onStateChanged: effectOn(
[
(state) => state.items,
(state) => state.foo,
(state, storeState) => storeState.rootData,
],
(actions, change, helpers) => {
actions.setFoo('bar');
const [prevItems, prevFoo, prevRootData] = change.prev;
prevItems[0].text;
const baz = `${prevFoo}bar`;
prevRootData + 2;
const [currentItems, currentFoo, currentRootData] = change.current;
const qux = `${currentFoo}bar`;
currentItems[0].text;
currentRootData + 2;
helpers.injections.doSomething().then(() => {});
helpers.dispatch.todos.setFoo('plop');
helpers.getState().items[0].id + 2;
helpers.getStoreActions().todos.setFoo('plop');
helpers.getStoreState().rootData + 2;
helpers.meta.parent[0].toLowerCase();
helpers.meta.path[0].toLowerCase();
return () => console.log('dispose');
},
),
},
};