-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-multiple-context.ts
84 lines (80 loc) · 1.86 KB
/
01-multiple-context.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
import { UseCase, Context, Dispatcher, Store, StoreGroup } from "almin";
/**
* ## Multiple Context
*
* We want to have multiple separated storegroup.
* It is useful for large application which has many pages.
*
* For example, Page A use
*
* - Store A
* - Store B
*
* Page B use
*
* - Store C
* - Store D
*
* In the Page A, We not want to Store C and Store D.
*
* ### Should we have multiple context?
*
* In the Page A, use ContextA.
* In the Page B, use ContextB.
*
*/
class MockStore extends Store {
}
const aStore = new MockStore();
const bStore = new MockStore();
const cStore = new MockStore();
const dStore = new MockStore();
// Pattern 1: big nest StoreGroup
const storeGroup = new StoreGroup({
pageX: new StoreGroup({
a: aStore,
b: bStore
}),
pageY: new StoreGroup({
c: cStore,
d: dStore
})
});
const context = new Context({
dispatcher: new Dispatcher,
store: storeGroup
});
context.getState(); // { pageX: {}, pageY: {} }
// Patter 2: multiple context
const storeGroupPageX = new StoreGroup({
a: aStore,
b: bStore
});
const storeGroupPageY = new StoreGroup({
c: cStore,
d: dStore
});
const contextX = new Context({
dispatcher: new Dispatcher,
store: storeGroupPageX,
});
const contextY = new Context({
dispatcher: new Dispatcher,
store: storeGroupPageY
});
// Pattern 3: multiple StoreGroup and single context
const context = new Context({
dispatcher: new Dispatcher,
stores: [storeGroupPageX, storeGroupPageY]
});
context.getState() ; // [{ },{ }]
// # Pattern 4: multiple context and shared dispatcher
const sharedDispatcher = new Dispatcher();
const contextXsharedDispatcher = new Context({
dispatcher: sharedDispatcher,
store: storeGroupPageX,
});
const contextYsharedDispatcher = new Context({
dispatcher: sharedDispatcher,
store: storeGroupPageY
});