-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
Copy pathstore.ts
153 lines (126 loc) · 2.89 KB
/
store.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
Store,
createStore,
Reducer,
Action,
StoreEnhancer,
Unsubscribe,
Observer
} from 'redux'
import 'symbol-observable'
type BrandedString = string & { _brand: 'type' }
const brandedString = 'a string' as BrandedString
type State = {
a: 'a'
b: {
c: 'c'
d: 'd'
}
e: BrandedString
}
interface DerivedAction extends Action {
type: 'a'
b: 'b'
}
const reducer: Reducer<State> = (
state: State | undefined = {
a: 'a',
b: {
c: 'c',
d: 'd'
},
e: brandedString
},
action: Action
): State => {
return state
}
const reducerWithAction: Reducer<State, DerivedAction> = (
state: State | undefined = {
a: 'a',
b: {
c: 'c',
d: 'd'
},
e: brandedString
},
action: DerivedAction
): State => {
return state
}
const funcWithStore = (store: Store<State, DerivedAction>) => {}
/* createStore */
const store: Store<State> = createStore(reducer)
// ensure that an array-based state works
const arrayReducer = (state: any[] = []) => state || []
const storeWithArrayState: Store<any[]> = createStore(arrayReducer)
const storeWithPreloadedState: Store<State> = createStore(reducer, {
a: 'a',
b: { c: 'c', d: 'd' },
e: brandedString
})
// @ts-expect-error
const storeWithBadPreloadedState: Store<State> = createStore(reducer, {
b: { c: 'c' },
e: brandedString
})
const storeWithActionReducer = createStore(reducerWithAction)
const storeWithActionReducerAndPreloadedState = createStore(reducerWithAction, {
a: 'a',
b: { c: 'c', d: 'd' },
e: brandedString
})
funcWithStore(storeWithActionReducer)
funcWithStore(storeWithActionReducerAndPreloadedState)
// @ts-expect-error
const storeWithActionReducerAndBadPreloadedState = createStore(
reducerWithAction,
{
b: { c: 'c' },
e: brandedString
}
)
const enhancer: StoreEnhancer = next => next
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, enhancer)
const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(
reducer,
{
a: 'a',
b: { c: 'c', d: 'd' },
e: brandedString
},
enhancer
)
const storeWithBadPreloadedStateAndEnhancer: Store<State> = createStore(
reducer,
{
// @ts-expect-error
b: { c: 'c' }
},
enhancer
)
/* dispatch */
store.dispatch({
type: 'ADD_TODO',
text: 'test'
})
/* getState */
const state: State = store.getState()
/* subscribe / unsubscribe */
const unsubscribe: Unsubscribe = store.subscribe(() => {
console.log('Current state:', store.getState())
})
unsubscribe()
/* replaceReducer */
const newReducer: Reducer<State> = reducer
store.replaceReducer(newReducer)
/* observable */
let observable = store[Symbol.observable]()
observable = observable[Symbol.observable]()
const observer: Observer<State> = {
next(state: State) {
console.log('current state:', state)
}
}
const unsubscribeFromObservable = observable.subscribe(observer).unsubscribe
unsubscribeFromObservable()