This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhover-engine.js
125 lines (105 loc) · 4.01 KB
/
hover-engine.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
114
115
116
117
118
119
120
121
122
123
124
125
const values = (object) => Object.keys(object).map(key => object[key])
const flatMap = (allItems, items) => allItems.concat(items)
class HoverEngine {
constructor() {
this.engine = {}
this.store = {}
this.actionQueue = []
this.listeners = []
this.actions = {}
}
/**
* addActions
* - adds action groups to the engine
* @param {Object} actionGroups
* - keys in the action group object can be used to access stores
* - values in the action group object are functions to mutate those stores
*/
addActions(actionGroups) {
const addActionToEngine = (actions, action) => {
const newActions = (action.name in actions) ?
actions[action.name].concat(action) :
[action]
return Object.assign({}, actions, {[action.name]: newActions})
}
const addActionGroupToEngine = (actions, group) => {
return values(group).reduce(addActionToEngine, actions)
}
const getAddKeyToActionFunc = (actionGroups) => {
return (actionKey) => {
const addStoreKeyToAction = (action) => {
action._storeKey = actionKey
return action
}
return values(actionGroups[actionKey]).map(addStoreKeyToAction)
}
}
this.engine = Object.keys(actionGroups)
.map(getAddKeyToActionFunc(actionGroups))
.reduce(addActionGroupToEngine, this.engine)
const addInitObjectToStore = (store, initObject) => {
return Object.assign({}, store, {[initObject.key]: initObject.init()})
}
const actionGroupToInitObject = (actionGroupKey) => ({
key: actionGroupKey,
init: actionGroups[actionGroupKey].init
})
this.store = Object.keys(actionGroups)
.map(actionGroupToInitObject)
.reduce(addInitObjectToStore, this.store)
const callActions = (name, args) => {
const updateStoreByNextAction = (nextAction) => (store, action) => {
return Object.assign({}, store,
{[action._storeKey]: action(store[action._storeKey], nextAction.args, this.actions)}
)
}
const shouldRunQueue = this.actionQueue.length === 0
this.actionQueue.push({actions: this.engine[name], args: args})
// eslint-disable-next-line no-unmodified-loop-condition
while (shouldRunQueue && this.actionQueue.length > 0) {
const nextAction = this.actionQueue[0]
const updateStoreByAction = updateStoreByNextAction(nextAction)
this.store = nextAction.actions.reduce(updateStoreByAction, this.store)
this.actionQueue.shift()
this.notifyListeners(name, args)
}
}
const addActionNameToActions = (actionsObject, actionName) => {
return Object.assign({}, actionsObject, {[actionName]: (args, badArgs) => {
if (badArgs) {
console.warn(`Hover-Engine: action '${actionName}' was called with more than one argument,` +
' but actions only take one argument. An action only needs to pass an (arguement) and will' +
' resolve with (store, arguement, actions)')
}
return callActions(actionName, args)
}})
}
this.actions = values(actionGroups)
.map(group => Object.keys(group))
.reduce(flatMap, [])
.reduce(addActionNameToActions, this.actions)
return this
}
/**
* addListener
* - function that takes in a function to call when an action occurs
* @param {Function} listener function that can take in a store, actions to
* call, actionName of what was called, and any arguments passed in
*/
addListener(listener) {
this.listeners.push(listener)
return this
}
/**
* notifyListeners
* - used internally to call each listener that has been added with addListners
* - doesn't actually trigger the action
* @param {*} actionName - name of action triggered
* @param {*} actionArguments - arguements passed into action when called
*/
notifyListeners(actionName, actionArguments) {
this.listeners.forEach(listener => listener(this.store, this.actions, actionName, actionArguments))
return this
}
}
module.exports = HoverEngine