forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
299 lines (272 loc) · 6.44 KB
/
index.js
File metadata and controls
299 lines (272 loc) · 6.44 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict'
/**
* adonis-framework
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const EventEmitter2 = require('eventemitter2').EventEmitter2
const Ioc = require('adonis-fold').Ioc
const Resolver = require('adonis-binding-resolver')
const resolver = new Resolver(Ioc)
const _ = require('lodash')
const util = require('../../lib/util')
const co = require('co')
const CE = require('../Exceptions')
class Event {
constructor (Config, Helpers) {
const options = Config.get('event')
this.listenersPath = 'Listeners'
this.helpers = Helpers
this.namedListeners = {}
this.listenerLimit = null
this.emitter = new EventEmitter2(options)
}
/**
* here we resolve the handler from the IoC container
* or the actual callback if a closure was passed.
*
* @param {String|Function} handler
* @return {Object|Function}
*
* @private
*/
_resolveHandler (handler) {
const formattedHandler = typeof (handler) === 'string' ? this.helpers.makeNameSpace(this.listenersPath, handler) : handler
return resolver.resolveBinding(formattedHandler)
}
/**
* here we bind the instance to the method, only
* if it exists.
*
* @param {Object} instance
* @param {Function} method
* @return {Function}
*
* @private
*/
_bindInstance (instance, method) {
return function () {
instance.emitter = this
instance.emitter.eventName = instance.emitter.event instanceof Array
? instance.emitter.event.join(instance.emitter.delimiter)
: instance.emitter.event
method.apply(instance, arguments)
}
}
/**
* here we wrap the generator method using co.wrap
* and make sure to pass the instance to the
* method.
*
* @param {Object} instance
* @param {Function} method
* @return {Function}
*
* @private
*/
_wrapGenerator (instance, method) {
return co.wrap(function * () {
instance.emitter = this
yield method.apply(instance, arguments)
})
}
/**
* here we make the handler method with correct context and
* execution type. It makes it possible to use generator
* methods and other methods as event handler.
*
* @param {Object|Function} handler
* @return {Function}
*
* @private
*/
_makeHandler (handler) {
let parentContext = {}
/**
* if handler is resolved out of IoC container, it will
* be an object with the parent context and the method.
*/
if (typeof (handler) !== 'function' && handler.instance) {
parentContext = handler.instance
handler = handler.instance[handler.method]
}
/**
* if handler to the event is a generator, then we need to
* wrap it inside co with correct context
*/
if (util.isGenerator(handler)) {
return this._wrapGenerator(parentContext, handler)
}
/**
* otherwise we bind the parentContext to the method
*/
return this._bindInstance(parentContext, handler)
}
/**
* returns an array of listeners for a specific event
*
* @param {String} event
* @return {Array}
*
* @public
*/
getListeners (event) {
return this.emitter.listeners(event)
}
/**
* it should tell whether there are any listeners
* for a given event or not.
*
* @param {String} event
* @return {Boolean}
*
* @public
*/
hasListeners (event) {
return !!this.getListeners(event).length
}
/**
* returns the status for wildcard
*
*
* @return {Boolean}
*
* @public
*/
wildcard () {
return this.emitter.wildcard
}
/**
* removes a named handler for a given event from the
* emitter
*
* @param {String} event
* @param {String} name
*
* @public
*/
removeListener (event, name) {
const handler = this.namedListeners[name]
if (!handler) {
throw CE.InvalidArgumentException.missingEvent(event, name)
}
this.emitter.removeListener(event, handler)
}
/**
* removes all listeners for a given or all events
*
* @param {String} [event]
*
* @public
*/
removeListeners (event) {
event ? this.emitter.removeAllListeners(event) : this.emitter.removeAllListeners()
}
/**
* emits a given event and passes all the data
* to the handler
*
* @param {...Spread} data
*
* @public
*/
emit () {
const args = _.toArray(arguments)
this.emitter.emit.apply(this.emitter, args)
}
/**
* @alias emit
*/
fire () {
this.emit.apply(this, arguments)
}
/**
* binds an handler to any event
*
* @param {Function|Sring} handler
*
* @public
*/
any (handler) {
resolver.validateBinding(handler)
handler = this._resolveHandler(handler)
handler = this._makeHandler(handler)
this.emitter.onAny(handler)
}
/**
* defines a limit for a listener to be executed
*
* @param {Number} limit
* @return {Object}
*
* @public
*/
times (limit) {
this.listenerLimit = limit
return this
}
/**
* adds a new event listen for a specific event
*
* @param {String} event
* @param {Function|String} handler
*
* @public
*/
on (event, name, handler) {
if (!handler) {
handler = name
name = null
}
resolver.validateBinding(handler)
handler = this._resolveHandler(handler)
handler = this._makeHandler(handler)
if (name) {
this.namedListeners[name] = handler
}
/**
* if there is a limit define, go with the many
* method on the emitter
*/
if (this.listenerLimit) {
this.emitter.many(event, this.listenerLimit, handler)
this.listenerLimit = null
return
}
/**
* otherwise register normally
*/
this.emitter.on(event, handler)
}
/**
* @alias on
*/
when () {
this.on.apply(this, arguments)
}
/**
* @alias on
*/
listen () {
this.on.apply(this, arguments)
}
/**
* adds a new event listen for a specific event
* to be ran only for one time.
*
* @param {String} event
* @param {Function|String} handler
*
* @public
*/
once (event, handler) {
resolver.validateBinding(handler)
handler = this._resolveHandler(handler)
handler = this._makeHandler(handler)
this.emitter.once(event, handler)
}
}
module.exports = Event