-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReducerRegistry.ts
82 lines (72 loc) · 2.46 KB
/
ReducerRegistry.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module State
*/
import { UiError } from "@itwin/appui-abstract";
import { UiFramework } from "../UiFramework";
/** NameToReducerMap used by Reducer Registry
* @public
*/
export interface NameToReducerMap {
[name: string]: (state: any, action: any) => any;
}
/** Redux Reducer Registry.
* Follows the example at http://nicolasgallagher.com/redux-modules-and-code-splitting/
* Allows for small modules to provide their own reducers so that the they can manage their own state
* @beta
*/
export class ReducerRegistry {
private _onReducerListChanged:
| ((reducers: NameToReducerMap) => void)
| undefined;
private _reducers: NameToReducerMap;
/** ReducerRegistry constructor that initializes an empty reducer map to be populated by called to registerReducer. */
constructor() {
this._reducers = {};
}
/** Call to register a reducer and its name. */
public registerReducer(
name: string,
reducer: (state: any, action: any) => any
) {
if (this._reducers[name]) {
// eslint-disable-next-line deprecation/deprecation
throw new UiError(
UiFramework.loggerCategory(this),
`Redux Reducer with matching name of '${name}' is already registered`
);
}
this._reducers = { ...this._reducers, [name]: reducer };
// istanbul ignore else
if (this._onReducerListChanged) {
this._onReducerListChanged(this._reducers);
}
}
/** Supports a single listener which should be the Redux store, see [[StoreManager]].
* @internal
*/
public setChangeListener(listener: (reducers: NameToReducerMap) => void) {
this._onReducerListChanged = listener;
}
/** Returns map of registered Reducers. */
public getReducers() {
return this._reducers;
}
/** Clear reducers only required for unit testing
* @internal
*/
public clearReducers() {
this._reducers = {};
// istanbul ignore else
if (this._onReducerListChanged) {
this._onReducerListChanged(this._reducers);
}
}
}
/** ReducerRegistryInstance singleton instance of Reducer Registry
* @beta
*/
export const ReducerRegistryInstance = new ReducerRegistry();