-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (90 loc) · 3.43 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
const { EventEmitter } = require('events')
const eventPromiseCache = require('./lib/eventPromiseCache')
const shareEvent = require('./lib/shareEvent')
const verifyEventEmitter = require('./lib/verifyEventEmitter')
/**
* Class representing better EventEmitter.
* @class
* @extends EventEmitter
*/
class BetterEvents extends EventEmitter {
/**
* Return a promise that gets resolved when the event is emitted by the source.
* @param {BetterEvents|EventEmitter} source - The source of the event.
* @param {string} eventName - The name of the event.
* @param {boolean} [arrayMode] - Convert all arguments of the event into an array.
* @returns {Promise.<*>}
*/
static once(source, eventName, arrayMode) {
if (!(source instanceof EventEmitter)) {
return Promise.reject(new TypeError('source must be an instance of EventEmitter'))
}
if (arrayMode && eventName !== 'error') {
const cacheProp = '_arrayEventPromises'
const fn = resolve => {
source.once(eventName, (...args) => resolve(args))
}
return eventPromiseCache(source, cacheProp, eventName, fn)
}
const cacheProp = '_eventPromises'
const fn = (resolve, reject) => {
if (eventName === 'error') {
return source.once('error', reject)
}
source.once(eventName, resolve)
}
return eventPromiseCache(source, cacheProp, eventName, fn)
}
/**
* Listen for an event once.
* @param {string} eventName - The name of the event.
* @param {true|function} [listener] - Function that listens for the event. If set to true, the array-mode will be used and a promise will be returned.
* @returns {BetterEvents|Promise.<*>} - If no callback is provided, a promise gets returned.
*/
once(eventName, listener) {
if (typeof listener === 'function') {
return super.once(eventName, listener)
}
return BetterEvents.once(this, eventName, listener)
}
/**
* Collect an event from the source.
* @param {string} eventName - The name of the event.
* @param {BetterEvents|EventEmitter} source - The source of the Event.
* @returns {callback} - The callback that has been applied to the source.
*/
collect(eventName, source) {
return shareEvent(eventName, source, this)
}
/**
* Collect an event from the source once.
* @param {string} eventName - The name of the event.
* @param {BetterEvents|EventEmitter} source - The source of the Event.
* @returns {callback} - The callback that has been applied to the source.
*/
collectOnce(eventName, source) {
return shareEvent(eventName, source, this, true)
}
/**
* Share an event an event with the target.
* @param {string} eventName - The name of the event.
* @param {BetterEvents|EventEmitter} target - The target for the Event.
* @returns {callback} - The callback that has been applied to the target.
*/
share(eventName, target) {
return shareEvent(eventName, this, target)
}
/**
* Share an event an event with the target once.
* @param {string} eventName - The name of the event.
* @param {BetterEvents|EventEmitter} target - The target for the Event.
* @returns {callback} - The callback that has been applied to the target.
*/
shareOnce(eventName, target) {
return shareEvent(eventName, this, target, true)
}
}
BetterEvents.BetterEvents = BetterEvents
BetterEvents.verifyEventEmitter = verifyEventEmitter
BetterEvents.shareEvent = shareEvent
module.exports = BetterEvents