'use strict'
function createEmitter() {
let evtHandlersMap = {}
// @evt [String] event name
// @handler [Function] event handlers
// @options [Object] options passed to event handlers
function emit (evt, options) {
const handlers = evtHandlersMap[evt]
if (handlers) {
handlers.forEach(handler => handler(options))
}
}
function on (evt, handler) {
!evtHandlersMap[evt] && evtHandlersMap[evt] = []
evtHandlersMap[evt].push(handler.bind(this))
}
return {
emit,
on
}
}
export default createEmitter()
import {emit} from './emitter.js'
export default function (target, opts = {}) {
if (!opts || typeof opts !== 'object') {
return
}
const keys = Object.keys(opts)
for (const key of keys) {
if (key reserved) {
target.addEventListener(EVENT_TYPE, (e) => {
emit(EVENT_TYPE)
opts[key]()
})
} else {
// customized ...
}
}
}