-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (78 loc) · 2.67 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
var EventEmitter = require('events');
function Areq(emitter, areqTimeout) {
if (! (emitter instanceof EventEmitter))
throw new TypeError('Input emitter should be an EventEmitter.');
this._emitter = emitter;
this._areqTimeout = areqTimeout || 30000;
this._pendings = {}; // { evtName: { deferred, listener }, ... }
}
Areq.prototype.changeDefaultTimeout = function (time) {
if (typeof time !== 'numner' || time < 1)
throw new TypeError('Time for timeout should be a number and greater than 1ms.');
this._areqTimeout = time;
return this._areqTimeout;
};
Areq.prototype.getRecord = function (evt) {
throwIfEvtNotString(evt);
return this._pendings[evt];
};
Areq.prototype.isEventPending = function (evt) {
throwIfEvtNotString(evt);
return !!this._pendings[evt];
};
Areq.prototype.register = function (evt, deferred, listener, time) {
var self = this,
registered = false,
areqTimeout = time || this._areqTimeout;
if (typeof listener !== 'function')
throw new TypeError('listener should be a function.');
if (this.getRecord(evt)) { // someone waiting same event, throw if evt is not a string
registered = false;
} else {
if (!deferred.hasOwnProperty('promise'))
throw new TypeError('deferred should be a deferred object of Promise.');
this._emitter.once(evt, listener);
this._pendings[evt] = {
listener: listener,
deferred: deferred
}
registered = true;
deferred.promise.timeout(areqTimeout).fail(function(err) {
self.reject(evt, err);
}).done();
}
return registered;
};
Areq.prototype.deregister = function (evt) {
var rec = this.getRecord(evt),
emitter = this._emitter;
if (rec) {
if (rec.deferred)
rec.deferred = null;
if (rec.listener) {
emitter.removeListener(evt, rec.listener);
rec.listener = null;
}
this._pendings[evt] = null;
delete this._pendings[evt];
}
};
Areq.prototype.resolve = function (evt, value) {
var rec = this.getRecord(evt),
deferred = rec ? rec.deferred : null;
if (deferred && deferred.promise.isPending())
deferred.resolve(value);
this.deregister(evt);
};
Areq.prototype.reject = function (evt, err) {
var rec = this.getRecord(evt),
deferred = rec ? rec.deferred : null;
if (deferred && deferred.promise.isPending())
deferred.reject(err);
this.deregister(evt);
};
function throwIfEvtNotString (evt) {
if (typeof evt !== 'string')
throw new TypeError('evt should be a string.');
}
module.exports = Areq;