-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStonexStore.ts
More file actions
248 lines (221 loc) · 7.44 KB
/
StonexStore.ts
File metadata and controls
248 lines (221 loc) · 7.44 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import {
ModuleConfiguration, ModuleCreator, ModuleCreatorsMap,
StateSnapshot,
StonexModules
} from '.'
import { copy, isType, noop, types } from './helpers/base'
import { convertToStandardModule, isPureModule } from './helpers/module'
import { ActionModifier, Modifier, ModifiersWorker, ModuleModifier } from './ModifiersWorker'
import { StateWorker } from './StateWorker'
import { StonexModule } from './StonexModule'
import { createStoreBinder } from './StoreBinder'
/**
* @typedef {Object} MP - Map of Stonex Modules class references
*/
export declare interface Store<MP> {
modules: StonexModules<MP>
getState: <State>(moduleName: string) => State
setState: <State>(
moduleName: string,
changes: ((() => Partial<State>) | Partial<State>), callback?: (state: State) => any
) => any
resetState: (moduleName: string, callback?: (state: any) => any) => void
connectModule: <State> (
moduleName: string,
data: ModuleCreator<State, any>
) => StonexModule<State>
createStateSnapshot: () => StateSnapshot<MP>,
storeId: number
}
export declare interface StoreConfiguration<MP> {
stateWorker?: new (...args: any[]) => StateWorker,
modifiers?: Array<Modifier<MP>>
}
/**
* Creates a new stonex store.
* Combining all your stonex modules together and allows to use them in your application.
*
* @class StonexStore
* @implements {Store<MP>}
* @template MP
*
* @example
* import SomeModule from './SomeModule'
*
* const store = new StonexStore({
* someModule: SomeModule
* })
*
* store.modules.someModule.doSomething()
*
* store.createStateSnapshot()
*/
class StonexStore<MP> implements Store<MP> {
/**
* Creates snapshot of state
*
* @param {MP} modules - Map with keys where key it is name of module and value it is class reference or object
*
* @static
* @memberof StonexStore
*
* @returns {StateSnapshot<MP>}
*/
public static createStateSnapshot = <MP>(modules: MP): StateSnapshot<MP> =>
Object.keys(modules).reduce((state, name) => {
state[name] = copy(modules[name].state)
return state
}, {}) as StateSnapshot<MP>
/**
* Unique identificator of store.
* Usings inside Stonex Store. Don't change it!
*
* @type {number}
* @memberof StonexStore
*/
public storeId: number = Math.round(Math.random() * Number.MAX_SAFE_INTEGER - Date.now())
/**
* Map of the Stonex modules
*
* @type {StonexModules<MP>}
* @memberof StonexStore
*/
public modules: StonexModules<MP> = {} as StonexModules<MP>
/**
* Set of methods which needed to work with module's state (updating, initializing, etc)
*
* Its can be overriden via StonexStore constructor:
* @example
* new StonexStore(modules, { stateWorker: OwnStateWorker })
*
* @private
* @type {StateWorker}
* @memberof StonexStore
*/
private stateWorker: StateWorker
/**
* Creates an instance of StonexStore.
*
* @param {Partial<ModuleCreatorsMap<MP>>} modulesMap
* @param {StoreConfiguration<MP>} storeConfiguration - have keys 'stateWorker', 'modifiers'
* @memberof StonexStore
*/
constructor (
modulesMap: Partial<ModuleCreatorsMap<MP>>,
{ stateWorker = StateWorker, modifiers }: StoreConfiguration<MP> = {}
) {
this.stateWorker = new stateWorker()
const moduleModifiers = ModifiersWorker.getModuleModifiers(modifiers || [], this)
for (const moduleName of Object.keys(modulesMap)) {
this.connectModule(
moduleName,
modulesMap[moduleName],
moduleModifiers
)
}
}
/**
* Create snapshot of the current store state
*
* @memberof StonexStore
* @returns {StateSnapshot<MP>}
*/
public createStateSnapshot = (): StateSnapshot<MP> => StonexStore.createStateSnapshot(this.modules)
// tslint:disable:max-line-length
/**
* Allows to attach stonex module to the store
*
* @template State
* @param {string} moduleName - name of stonex module. This name will usings inside stonex store
* @param {(ModuleCreator<State, any> | ModuleConfiguration<State>)} data - It can be: stonex module class reference, pure stonex module or ModuleConfiguration
* @param {ModuleModifier[]} moduleModifiers - list of module modifiers (specific middleware)
* @returns {StonexModule<State>}
* @memberof StonexStore
*
*
* @example
* yourStore.connectModule('moduleName', ModuleClass)
*
* yourStore.modules.moduleName.methodFromModuleClass()
*/
// tslint:enable:max-line-length
public connectModule<State> (
moduleName: string,
data: ModuleCreator<State, any> | ModuleConfiguration<State>,
moduleModifiers: ModuleModifier[] = []
): StonexModule<State> {
const createDefaultStoreBinder = () => createStoreBinder<MP, State>(moduleName, this)
const { module: Module, storeBinder = createDefaultStoreBinder() } =
(
isType(data, types.function)
|| (isType(data, types.object) && typeof (data as ModuleConfiguration<State>).module === 'undefined')
) ? {
module: data as ModuleConfiguration<State>['module'],
storeBinder: createDefaultStoreBinder(),
} : data as ModuleConfiguration<State>
const moduleInstance = new (isPureModule(Module) ? convertToStandardModule(Module) : Module)(storeBinder)
const actionModifiers: ActionModifier[] = ModifiersWorker.getActionModifiers(moduleModifiers, moduleInstance)
if (!moduleInstance.__STONEXMODULE__) {
console.error(`${name} is not a Stonex Module` + '\r\n' +
`To solve this you should extend your class ${name} from StonexModule class`)
}
moduleInstance.__initialState = copy(moduleInstance.state)
this.stateWorker.initializeState(moduleInstance)
ModifiersWorker.attachActionModifiersToModule(actionModifiers, moduleInstance)
this.modules[moduleName] = moduleInstance
return moduleInstance
}
/**
* Update Stonex module state
*
* @param {string} moduleName - name of module
* @param {*} changes - changes which need to apply to state of module
* @param {function} callback - function which will been called when state has been changed
*
* @memberof StonexStore
* @returns {void}
*/
public setState = <State>(
moduleName: string,
changes: Partial<State> | ((state: State) => Partial<State>),
callback: (state: State) => any = noop
): void =>
this.stateWorker.setState(this.getModuleByName(moduleName), changes, callback)
/**
* Returns Stonex module state
*
* @param {string} moduleName
*
* @memberof StonexStore
* @returns {*}
*/
public getState = (moduleName: string): any =>
this.stateWorker.getState(moduleName)
/**
* Reset state of the Stonex module to initial value (first value of module state)
*
* @param {string} moduleName - name of module
* @param {function} callback - function which will been called when state has been cleared
*
* @memberof StonexStore
* @returns {void}
*/
public resetState = (moduleName: string, callback: (state: any) => any = noop): void =>
this.stateWorker.resetState(this.getModuleByName(moduleName), callback)
/**
* Find module in Stonex store by name
*
* @private
* @memberof StonexStore
*
* @returns {(StonexModule | never)}
*/
private getModuleByName = (moduleName: string): StonexModule<any> | never => {
const module = this.modules[moduleName]
if (!module) {
throw new Error(`Module with name ${moduleName} is not exist in your stonex store`)
}
return module
}
}
export default StonexStore