This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp-registry.js
113 lines (103 loc) · 3.06 KB
/
app-registry.js
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
/**
* The prefix.
*/
const PREFIX = 'mongodb-redux-common/app-registry';
/**
* Local app registry activated.
*/
const LOCAL_APP_REGISTRY_ACTIVATED = `${PREFIX}/LOCAL_APP_REGISTRY_ACTIVATED`;
/**
* Global app registry activated.
*/
const GLOBAL_APP_REGISTRY_ACTIVATED = `${PREFIX}/GLOBAL_APP_REGISTRY_ACTIVATED`;
/**
* The initial state.
*/
const INITIAL_STATE = {
localAppRegistry: null,
globalAppRegistry: null
};
/**
* Reducer function for handle state changes to the app registry.
*
* @param {String} state - The app registry state.
* @param {Object} action - The action.
*
* @returns {String} The new state.
*/
const reducer = (state = INITIAL_STATE, action) => {
if (action.type === LOCAL_APP_REGISTRY_ACTIVATED) {
return { ...state, localAppRegistry: action.appRegistry };
} else if (action.type === GLOBAL_APP_REGISTRY_ACTIVATED) {
return { ...state, globalAppRegistry: action.appRegistry };
}
return state;
};
/**
* Action creator for local app registry activated events.
*
* @param {AppRegistry} appRegistry - The app registry.
*
* @returns {Object} The app registry activated event.
*/
const localAppRegistryActivated = (appRegistry) => ({
type: LOCAL_APP_REGISTRY_ACTIVATED,
appRegistry: appRegistry
});
/**
* Action creator for global app registry activated events.
*
* @param {AppRegistry} appRegistry - The app registry.
*
* @returns {Object} The app registry activated event.
*/
const globalAppRegistryActivated = (appRegistry) => ({
type: GLOBAL_APP_REGISTRY_ACTIVATED,
appRegistry: appRegistry
});
/**
* Emit an event on the provided app registry.
*
* @param {AppRegistry} appRegistry - The app registry.
* @param {String} name - The event name.
* @param {Object} metadata - The event metadata.
*/
const appRegistryEmit = (appRegistry, name, ...metadata) => {
if (appRegistry) {
appRegistry.emit(name, ...metadata);
}
};
/**
* Emit an event to the local app registry.
*
* @param {String} name - The event name.
* @param {Object} metadata - The metadata.
*
* @returns {Function} The thunk function.
*/
const localAppRegistryEmit = (name, ...metadata) => {
return (dispatch, getState) => {
appRegistryEmit(getState().appRegistry.localAppRegistry, name, ...metadata);
};
};
/**
* Emit an event to the app registry.
*
* @param {String} name - The event name.
* @param {Object} metadata - The metadata.
*
* @returns {Function} The thunk function.
*/
const globalAppRegistryEmit = (name, ...metadata) => {
return (dispatch, getState) => {
appRegistryEmit(getState().appRegistry.globalAppRegistry, name, ...metadata);
};
};
module.exports = reducer;
module.exports.LOCAL_APP_REGISTRY_ACTIVATED = LOCAL_APP_REGISTRY_ACTIVATED;
module.exports.GLOBAL_APP_REGISTRY_ACTIVATED = GLOBAL_APP_REGISTRY_ACTIVATED;
module.exports.INITIAL_STATE = INITIAL_STATE;
module.exports.localAppRegistryActivated = localAppRegistryActivated;
module.exports.globalAppRegistryActivated = globalAppRegistryActivated;
module.exports.localAppRegistryEmit = localAppRegistryEmit;
module.exports.globalAppRegistryEmit = globalAppRegistryEmit;