-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (158 loc) · 4.41 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
!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
, proceed = {}
, array = []
, slice = array.slice
, owns = proceed.hasOwnProperty
, events = '_events'
, origin = '_origin'
, emit = 'emit'
, mode = 'mode'
, init = 'init'
, listeners = 'listeners'
, ifArray = function(o) {
return o instanceof Array && o
}
emitter[mode] = {'max': 0, 'all':false}
function defaults(ops, defs) {
for (var k in defs) if (void 0 === ops[k]) ops[k] = defs[k]
return ops
}
/**
* @constructor
*/
function Energy(ops) {
this[mode] = ops instanceof Energy ? defaults({}, ops[mode]) : defaults(ops || {}, emitter[mode])
this[events] = {}
}
/**
* @return {Energy} emitter instance
*/
function energy(o) {
return o instanceof Energy ? o[init]() : new Energy(o)
}
/**
* @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 {{length:number}} fns
* @param {*} scope
* @param {Array|Arguments} args
* @param {*=} stop breaker
* @return {number} fired
*/
function applies(fns, scope, args, stop) {
var l = fns && fns.length, i = 0
stop = void 0 === stop ? proceed : stop
while (i < l) if (stop === fns[i++].apply(scope, args)) break
return i
}
/**
* @param {Object} o
* @param {string} k
* @param {*} def
*/
function ensure(o, k, def) {
o[k] = owns.call(this, k) && this[k] || def
}
/**
* @param {Array} a array to mutate
* @param {*=} v value to remove
* @param {number=} quota occurrences to remove
*/
function pull(a, v, quota) {
quota >>= 0
for (var i = a.length; i--;) {
// Loop down so that splices don't interfere with subsequent iterations.
if ((v === a[i] || a[i] && v === a[i][origin]) && a.splice(i, 1) && !--quota) break
}
}
/**
* @this {Energy|Object}
*/
emitter[init] = function() {
ensure(this, mode, {})
ensure(this, events, {})
return this
}
/**
* @param {string|number} id
* @return {number} fired
*/
emitter[emit] = function(id) {
var rest = slice.call(arguments, 1), fired = applies(this[listeners](id), this, rest)
this[mode]['all'] && applies(this[listeners](this[mode]['all']), this, rest)
return fired
}
/**
* @param {string|number} id
* @return {Array}
*/
emitter[listeners] = function(id) {
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')
var n, max = this[mode]['max']
n = this[listeners](id).push(fn)
if (0 < max && n > max) throw new RangeError('@max')
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)
}
energy['applies'] = applies
return energy
});