-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventilator.js
93 lines (80 loc) · 2.61 KB
/
eventilator.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
(root => {
const curry = (fn, arity = fn.length, ...args) => arity <= args.length
? fn(...args) : curry.bind(null, fn, arity, ...args)
const listen = (once, target, type, fn, options = false) => {
if (typeof target === 'string') {
target = root.document.querySelectorAll(target)
target = target.length === 1 ? target[0] : Array.from(target)
}
if (Array.isArray(target) ? !target.length : !target.addEventListener) {
throw new Error('nil/empty event target(s)')
}
let typeobj = type != null && type.constructor === Object
if (type == null || !(typeobj || typeof type === 'string')) {
throw new TypeError('cannot listen to nil or invalid event type')
}
if (Array.isArray(target)) {
for (let i = 0; i < target.length; i++) {
target[i] = listen(
once, target[i], typeobj ? Object.assign({}, type) : type, fn, options
)
}
return target
}
if (typeobj) {
for (const name in type) {
type[name] = listen(once, target, name, type[name], options)
}
target.off = () => {
for (const h of target) h()
return target
}
target.on = mode => {
for (const h of target) h.on(mode)
return target
}
return type
}
function wrapper () {
fn.call(this, ...arguments, target)
if (off.once) off()
}
const on = mode => {
if (mode != null && mode !== off.once) off.once = !!mode
target.addEventListener(type, wrapper, options)
off.ison = true
return off
}
const off = Object.assign(() => {
target.removeEventListener(type, wrapper)
off.ison = false
return off
}, {
target,
on,
once,
emit: eventilator.emit.bind(target, target)
})
off.off = off
return on()
}
const eventilator = curry(listen, 3)
eventilator.curry = curry
eventilator.once = listen.bind(null, true)
eventilator.on = listen.bind(null, false)
eventilator.emit = (target, type, detail) => {
target.dispatchEvent(new root.CustomEvent(type, {detail}))
}
if (root.Proxy) {
const PC = { // Proxy Configuration
get: (fn, type) => (tgt, hndl, opts) => fn(tgt, type, hndl, opts)
}
eventilator.once = new root.Proxy(eventilator.once, PC)
eventilator.on = new root.Proxy(eventilator.on, PC)
}
typeof module !== 'undefined'
? module.exports = eventilator
: root.define instanceof Function && root.define.amd
? root.define(['eventilator'], () => eventilator)
: root.eventilator = eventilator
})(typeof global !== 'undefined' ? global : window)