MCPcopy Create free account
hub / github.com/DavidWells/analytics / createReducer

Function createReducer

packages/analytics-core/src/modules/plugins.js:4–78  ·  view source on GitHub ↗
(getPlugins)

Source from the content-addressed store, hash-verified

2import EVENTS from '../events'
3
4export default function createReducer(getPlugins) {
5 return function plugins(state = {}, action) {
6 let newState = {}
7 if (action.type === 'initialize:aborted') {
8 return state
9 }
10 if (/^registerPlugin:([^:]*)$/.test(action.type)) {
11 const name = getNameFromEventType(action.type, 'registerPlugin')
12 const plugin = getPlugins()[name]
13 if (!plugin || !name) {
14 return state
15 }
16 const isEnabled = action.enabled
17 const config = plugin.config
18 newState[name] = {
19 enabled: isEnabled,
20 /* if no initialization method. Set initialized true */
21 initialized: (isEnabled) ? Boolean(!plugin.initialize) : false,
22 /* If plugin enabled === false, set loaded to false, else check plugin.loaded function */
23 loaded: (isEnabled) ? Boolean(plugin.loaded({ config })) : false,
24 config
25 }
26 return { ...state, ...newState }
27 }
28 if (/^initialize:([^:]*)$/.test(action.type)) {
29 const name = getNameFromEventType(action.type, EVENTS.initialize)
30 const plugin = getPlugins()[name]
31 if (!plugin || !name) {
32 return state
33 }
34 const config = plugin.config
35 newState[name] = {
36 ...state[name],
37 ...{
38 initialized: true,
39 /* check plugin.loaded function */
40 loaded: Boolean(plugin.loaded({ config }))
41 }
42 }
43 return { ...state, ...newState }
44 }
45 if (/^ready:([^:]*)$/.test(action.type)) {
46 // const name = getNameFromEventType(action.type, 'ready')
47 newState[action.name] = {
48 ...state[action.name],
49 ...{ loaded: true }
50 }
51 return { ...state, ...newState }
52 }
53 switch (action.type) {
54 /* case EVENTS.pluginFailed:
55 // console.log('PLUGIN_FAILED', action.name)
56 newState[action.name] = {
57 ...state[action.name],
58 ...{ loaded: false }
59 }
60 return { ...state, ...newState }
61 */

Callers

nothing calls this directly

Calls 3

getNameFromEventTypeFunction · 0.85
getPluginsFunction · 0.85
togglePluginStatusFunction · 0.85

Tested by

no test coverage detected