-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncListenerList.js
87 lines (87 loc) · 3.88 KB
/
AsyncListenerList.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
"use strict";
var ListenerList = require("./ListenerList");
/** An event listener list for asynchronous event listeners (i.e. the listeners perform asynchronous operations).
* Manages a list of listener functions and allows events to be sent to the listeners.
* When a listener is called, the event and an IDeferred are passed to the function and the function resolves/rejects the deferred.
*/
var AsyncListenerList = /** @class */ (function () {
function AsyncListenerList(promiseStatic) {
this.eventHandler = new ListenerList();
this.promiseStatic = promiseStatic;
}
AsyncListenerList.prototype.reset = function () {
this.eventHandler.reset();
};
// have to proxy all the methods because TypeScript 'extends ...' doesn't work with our gulp compilation process
AsyncListenerList.prototype.getListeners = function () {
return this.eventHandler.getListeners();
};
AsyncListenerList.prototype.getFireEventsSuccessCallback = function () {
return this.eventHandler.getFireEventsSuccessCallback();
};
AsyncListenerList.prototype.setFireEventsSuccessCallback = function (cb) {
this.eventHandler.setFireEventsSuccessCallback(cb);
};
AsyncListenerList.prototype.getFireEventsFailureCallback = function () {
return this.eventHandler.getFireEventsFailureCallback();
};
AsyncListenerList.prototype.setFireEventsFailureCallback = function (cb) {
this.eventHandler.setFireEventsFailureCallback(cb);
};
AsyncListenerList.prototype.getListenerAddedCallback = function () {
return this.eventHandler.getListenerAddedCallback();
};
AsyncListenerList.prototype.setListenerAddedCallback = function (cb) {
this.eventHandler.setListenerAddedCallback(cb);
};
AsyncListenerList.prototype.getListenerRemovedCallback = function () {
return this.eventHandler.getListenerRemovedCallback();
};
AsyncListenerList.prototype.setListenerRemovedCallback = function (cb) {
this.eventHandler.setListenerRemovedCallback(cb);
};
AsyncListenerList.prototype.addListener = function (listener) {
this.eventHandler.addListener(listener);
};
AsyncListenerList.prototype.addOneTimeListener = function (listener) {
this.eventHandler.addOneTimeListener(listener);
};
AsyncListenerList.prototype.addNTimeListener = function (removeAfterNCalls, listener) {
this.eventHandler.addNTimeListener(removeAfterNCalls, listener);
};
AsyncListenerList.prototype.removeListener = function (listener) {
this.eventHandler.removeListener(listener);
};
AsyncListenerList.prototype.removeListenerAt = function (index) {
this.eventHandler.removeListenerAt(index);
};
/**
* @param event: the event object being fired to listeners
*/
AsyncListenerList.prototype.fireEvent = function (event) {
var promiseImpl = this.promiseStatic;
var fireEventsSuccessCallback = this.getFireEventsSuccessCallback();
var fireEventsFailureCallback = this.getFireEventsFailureCallback();
var dfds = [];
this.eventHandler.fireEvent(event, function asyncListenerCaller(listener, event, index, size) {
var dfd = promiseImpl.defer();
dfds.push(dfd.promise);
listener(dfd, event[0]);
}, function asyncListenerAwaiter() {
promiseImpl.all(dfds).then(function (results) {
if (typeof fireEventsSuccessCallback === "function") {
fireEventsSuccessCallback(results);
}
}).catch(function (err) {
if (typeof fireEventsFailureCallback === "function") {
fireEventsFailureCallback(err);
}
else {
throw new Error(err);
}
});
});
};
return AsyncListenerList;
}());
module.exports = AsyncListenerList;