Created
February 4, 2020 10:27
-
-
Save joona/cc583c14cf1d3c2749867499ec912881 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function dispatch(el, eventName, payload, options) { | |
options || (options = {}); | |
el || (el = document); | |
el = el.el || el; | |
const event = document.createEvent('Event'); | |
event.initEvent(eventName, options.bubbles !== false, true); | |
event.detail = payload || {}; | |
el.dispatchEvent(event); | |
} | |
export function listen(el, eventName, callback) { | |
el || (el = document); | |
el = el.el || el; | |
el.addEventListener(eventName, callback); | |
return callback; | |
} | |
export function unlisten(el, eventName, func) { | |
el || (el = document); | |
el = el.el || el; | |
el.removeEventListener(eventName, func); | |
} | |
export function dispatchAction(actionName, payload, el) { | |
el || (el = document); | |
const action = { | |
action: actionName, | |
payload: payload || {} | |
}; | |
requestAnimationFrame(() => { | |
dispatch(el, 'action', action); | |
}); | |
} | |
export function dispatcher(handler) { | |
listen(document, 'action', e => { | |
const action = e.detail.action; | |
const payload = e.detail.payload || {}; | |
handler.call(this, action, Object.assign({}, payload)); | |
e.stopPropagation(); | |
return false; | |
}); | |
} | |
export function storeFactory(obj) { | |
const eventChannel = document.createElement('div'); | |
eventChannel.setAttribute('data-store', true); | |
const store = obj || {}; | |
store._eventChannel = eventChannel; | |
store.listen = store.on = (eventName, eventHandler) => { | |
eventChannel.addEventListener(eventName, e => { | |
eventHandler(e, e.detail); | |
}); | |
return eventHandler; | |
}; | |
store.unlisten = (eventName, eventHandler) => { | |
eventChannel.removeEventListener(eventName, eventHandler); | |
}; | |
store.dispatch = (eventName, details) => { | |
dispatch(eventChannel, eventName, details); | |
}; | |
if(store.initialize && typeof store.initialize == 'function') { | |
store.initialize.call(store); | |
store._initialized = true; | |
} | |
return store; | |
} | |
class HandlerRegistry { | |
constructor() { | |
this._handlers = {}; | |
} | |
register(handlerMap) { | |
const actions = Object.keys(handlerMap); | |
for (var i = 0, len = actions.length; i < len; i++) { | |
if(this._handlers[actions[i]]) { | |
console.warn('DUPLICATE HANDLER:', actions[i]); | |
} | |
this._handlers[actions[i]] = handlerMap[actions[i]]; | |
} | |
return this; | |
} | |
add(action, callback) { | |
this._handlers[action] = callback; | |
return this; | |
} | |
handleAsync(action, payload, context) { | |
const handler = this._handlers[action]; | |
if(handler) { | |
return handler.call(this, payload, context); | |
} | |
return Promise.resolve(); | |
} | |
handle(action, payload, context) { | |
const handler = this._handlers[action]; | |
if(handler) { | |
handler.call(this, payload, context); | |
} | |
return !!handler; | |
} | |
} | |
export const handlers = new HandlerRegistry(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment