-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemitter.js
More file actions
203 lines (192 loc) · 6.2 KB
/
Copy pathemitter.js
File metadata and controls
203 lines (192 loc) · 6.2 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
/**
* @license
* MOST Web Framework 2.0 Codename Blueshift
* Copyright (c) 2017, THEMOST LP All rights reserved
*
* Use of this source code is governed by an BSD-3-Clause license that can be
* found in the LICENSE file at https://themost.io/license
*/
///
var EventEmitter = require('events').EventEmitter;
var LangUtils = require('./utils').LangUtils;
var applyEachSeries = require('async').applyEachSeries;
require('es6-promise/auto');
/**
* Wraps an async listener and returns a callback-like function
* @param {function(...*):Promise<void>} asyncListener
*/
function wrapAsyncListener(asyncListener) {
/**
* @this SequentialEventEmitter
*/
var result = function() {
// get arguments without callback
var args = [].concat(Array.prototype.slice.call(arguments, 0, arguments.length -1));
// get callback
var callback = arguments[arguments.length - 1];
return asyncListener.apply(this, args).then(function() {
return callback();
}).catch(function(err) {
return callback(err);
});
}
// set async listener property in order to have an option to unsubscribe
Object.defineProperty(result, '_listener', {
configurable: true,
enumerable: true,
value: asyncListener
});
return result;
}
/**
* Wraps an async listener and returns a callback-like function
* @param {string} event
* @param {function(...*):Promise<void>} asyncListener
*/
function wrapOnceAsyncListener(event, asyncListener) {
/**
* @this SequentialEventEmitter
*/
var result = function() {
var callee = arguments.callee;
// get arguments without callback
var args = [].concat(Array.prototype.slice.call(arguments, 0, arguments.length -1));
// get callback
var callback = arguments[arguments.length - 1];
var self = this;
return asyncListener.apply(self, args).then(function() {
// manually remove async listener
self.removeListener(event, callee);
return callback();
}).catch(function(err) {
// manually remove async listener
self.removeListener(event, callee);
return callback(err);
});
}
// set async listener property in order to have an option to unsubscribe
Object.defineProperty(result, '_listener', {
configurable: true,
enumerable: true,
value: asyncListener
});
return result;
}
// noinspection JSClosureCompilerSyntax,JSClosureCompilerSyntax,JSClosureCompilerSyntax,JSClosureCompilerSyntax
/**
* SequentialEventEmitter class is an extension of node.js EventEmitter class where listeners are executing in series.
* @class
* @constructor
* @augments EventEmitter
*/
function SequentialEventEmitter() {
//
}
LangUtils.inherits(SequentialEventEmitter, EventEmitter);
// noinspection JSUnusedGlobalSymbols
/**
* Executes event listeners in series.
* @param {String} event - The event that is going to be executed.
* @param {...*} args - An object that contains the event arguments.
*/
// eslint-disable-next-line no-unused-vars
SequentialEventEmitter.prototype.emit = function(event, args)
{
//ensure callback
callback = callback || function() {};
//get listeners
if (typeof this.listeners !== 'function') {
throw new Error('undefined listeners');
}
var listeners = this.listeners(event);
var argsAndCallback = [].concat(Array.prototype.slice.call(arguments, 1));
if (argsAndCallback.length > 0) {
//check the last argument (expected callback function)
if (typeof argsAndCallback[argsAndCallback.length - 1] !== "function") {
throw new TypeError("Expected event callback");
}
}
//get callback function (the last argument of arguments list)
var callback = argsAndCallback[argsAndCallback.length - 1];
//validate listeners
if (listeners.length===0) {
//exit emitter
return callback();
}
//apply each series
return applyEachSeries.apply(this, [listeners].concat(argsAndCallback));
};
// noinspection JSUnusedGlobalSymbols
/**
*
* @param {string} event
* @param {function(...*):Promise<void>} asyncListener
* @returns this
*/
SequentialEventEmitter.prototype.subscribe = function(event, asyncListener) {
return this.on(event, wrapAsyncListener(asyncListener));
}
// noinspection JSUnusedGlobalSymbols
/**
*
* @param {string} event
* @param {function(...*):Promise<void>} asyncListener
* @returns this
*/
SequentialEventEmitter.prototype.unsubscribe = function(event, asyncListener) {
// get event listeners
var listeners = this.listeners(event);
// enumerate
for (var i = 0; i < listeners.length; i++) {
var item = listeners[i];
// if listener has an underlying listener
if (typeof item._listener === 'function') {
// and it's the same with the listener specified
if (item._listener === asyncListener) {
// remove listener and break
this.removeListener(event, item);
break;
}
}
}
return this;
}
// noinspection JSUnusedGlobalSymbols
/**
*
* @param {string} event
* @param {function(...*):Promise<void>} asyncListener
*/
SequentialEventEmitter.prototype.subscribeOnce = function(event, asyncListener) {
return this.once(event, wrapOnceAsyncListener(event, asyncListener));
}
// noinspection JSUnusedGlobalSymbols
/**
*
* @param {string} event
* @param {...args} args
*/
// eslint-disable-next-line no-unused-vars
SequentialEventEmitter.prototype.next = function(event, args) {
var self = this;
/**
* get arguments as array
* @type {*[]}
*/
var argsAndCallback = [event].concat(Array.prototype.slice.call(arguments, 1));
// eslint-disable-next-line no-undef
return new Promise(function(resolve, reject) {
// set callback
argsAndCallback.push(function(err) {
if (err) {
return reject(err);
}
return resolve();
});
// emit event
self.emit.apply(self, argsAndCallback);
});
}
if (typeof exports !== 'undefined') {
module.exports.SequentialEventEmitter = SequentialEventEmitter;
}