-
Notifications
You must be signed in to change notification settings - Fork 0
/
energy.js
166 lines (149 loc) · 3.91 KB
/
energy.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/** @preserve npm.im/energy */
!function(root, name, make) {
if (typeof module != 'undefined' && module.exports) module.exports = make()
else root[name] = make()
}(this, 'energy', function() {
var emitter = energy.prototype = Energy.prototype
, array = []
, slice = array.slice
, owns = {}.hasOwnProperty
, events = '_events'
, origin = '_origin'
, emit = 'emit'
, init = 'init'
, listeners = 'listeners'
, ifArray = function(o) {
return o instanceof Array && o
}
function defaults(ops, defs) {
for (var k in defs) if (void 0 === ops[k]) ops[k] = defs[k]
return ops
}
/**
* @constructor
*/
function Energy() {
this[events] = {}
}
/**
* @return {Energy} emitter instance
*/
function energy(o) {
return o instanceof Energy ? o[init]() : new Energy
}
/**
* @this {Function} wrapper that constructs the source instance
* @param {Object|Function} target to convert into emitter
* @return {Object|Function} target converted into emitter
*/
energy['to'] = function(target) {
return defaults(target, this.call())
}
/**
* @this {Object|Energy|Function} source emitter or emitter-like
* @param {Object|Energy|Function} target to convert into emitter
* @return {Object|Energy|Function} source for chaining
*/
emitter['to'] = function(target) {
defaults(target, this)
return this
}
/**
* @param {*} listener
* @param {*} fn
* @return {boolean} true if they're the same listener
*/
function is(listener, fn) {
return listener === fn || !!listener && listener[origin] === fn
}
/**
* @param {Array} a array to mutate
* @param {*=} v value to remove
* @param {number=} quota occurrences to remove
*/
function pull(a, v, quota) {
quota >>= 0
// Loop down so that splices don't interfere with subsequent iterations
for (var i = a.length; i--;) if (is(a[i], v) && a.splice(i, 1) && !--quota) break
}
/**
* @param {Object} o
* @param {string} k
*/
function ensure(o, k) {
o[k] = owns.call(o, k) && o[k] || {}
}
/**
* @this {Energy|Object}
*/
emitter[init] = function() {
ensure(this, events)
return this
}
/**
* @param {string|number} id
* @return {number} invoked listeners
*/
emitter[emit] = function(id) {
var fns = this[listeners](id)
var l = fns && fns.length
if (!l) return 0
var rest = slice.call(arguments, 1)
var i = 0
while (i < l)
fns[i++].apply(this, rest)
return i
}
/**
* @param {string|number} id
* @return {Array}
*/
emitter[listeners] = function(id) {
if (null == id) throw new TypeError('@listeners')
return this[events][id] = ifArray(this[events][id]) || []
}
/**
* @return {Energy}
*/
emitter['clone'] = function() {
var clone = new Energy(this)
defaults(clone[events], this[events])
return clone
}
/**
* @param {string|number} id
* @param {Function} fn listener to add
* @return {Energy|Object}
*/
emitter['on'] = function on(id, fn) {
if (null == id || null == fn) throw new TypeError('@on')
this[listeners](id).push(fn)
return this
}
/**
* @param {(string|number)=} id
* @param {Function=} fn listener to remove
* @param {number=} quota occurrences to remove (the default 0 removes all)
* @return {Energy|Object}
*/
emitter['off'] = function(id, fn, quota) {
if (null != fn) pull(this[listeners](id), fn, quota)
else if (null != id) this[events][id] = void 0
else if (!arguments.length) this[events] = {}
return this
}
/**
* @param {string|number} id
* @param {Function} fn one-time listener to add
* @return {Energy|Object}
*/
emitter['once'] = function(id, fn) {
var that = this, handler = function() {
that['off'](id, handler)
return fn.apply(this, arguments)
}
handler[origin] = fn
return this['on'](id, handler)
}
return energy
});