-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
49 lines (46 loc) · 1.41 KB
/
index.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
export class ZeroFux {
constructor(element) {
if (element) {
this.dispatcher = element;
} else {
this.dispatcher = document.querySelector('body');
}
}
// The dipatch method takes an action argument
// of the previously defined action type.
dispatch(action) {
this.dispatcher.dispatchEvent(
new CustomEvent(action.type, {
detail: action,
// In case you set a custom dispatcher element
// and want them to bubble.
bubbles: true,
// In case your custom dispatcher is in the
// Shadow DOM and you want them to bubble between
// the borders or Shadow DOM and regular DOM.
compose: true,
})
)
}
// This method takes an array of action types
// that can influence a component's state,
// an object with reducers with the same names
// as the action types and a reference
// to the component on which we want to set
// the state propery.
setReducers(actionTypes, reducers, component) {
actionTypes.forEach(actionType => {
if (reducers[actionType]) {
this.dispatcher.addEventListener(actionType, e => {
const action = e.detail;
component.state = reducers[actionType](component.state, action);
});
} else {
throw new Error(
`Please add a reducer for the "${actionType}" action.`
);
}
});
}
}
export const zeroFux = new ZeroFux();