This repository has been archived by the owner on Mar 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async-calls-tracker.js
97 lines (76 loc) · 3.04 KB
/
async-calls-tracker.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
/*
* A node module that tracks a collection of async calls, and notify the main
* caller when all of them are done through a provided event handler, and forward
* the arguments passed to the callbacks by the observed calls.
*/
const EVENT_END = 'end';
const EVENT_CALLBACK = 'callback';
function tracker(){
const self = this;
self.observedCalls = [];
self.observedCallsArguments = [];
self.callbacks = [];
self.collectedCallbackArguments = [];
self.eventHandlers = [];
self.runningCalls = 0;
// set the event handler of each event
self.on = function(eventName, eventHandler){
if(typeof eventName !== 'string')
throw new TypeError('the first argument must be a string object');
if(typeof eventHandler !== 'function')
throw new TypeError('the second argument must be a function object');
self.eventHandlers[eventName] = eventHandler;
return self;
}
// collect the arguments passed to the callbacks from the observed async calls
self.collectCallbackArguments = function(callbackArgsWrapper){
self.collectedCallbackArguments[callbackArgsWrapper.callerIndex] = callbackArgsWrapper.args;
self.runningCalls--;
if(self.callbacks[callbackArgsWrapper.callerIndex])
self.callbacks[callbackArgsWrapper.callerIndex](...(callbackArgsWrapper.args));
if(self.eventHandlers[EVENT_CALLBACK])
self.eventHandlers[EVENT_CALLBACK](...(callbackArgsWrapper.args));
if((self.runningCalls === 0) && (self.eventHandlers[EVENT_END]))
self.eventHandlers[EVENT_END](self.collectedCallbackArguments);
};
// accepts the function to be observed, in addition to an arbtrary number of
// parameters to be passed to the observed function on its invokation
self.add = function(observedCall, ...args){
if(typeof observedCall !== 'function')
throw new TypeError('the first argument must be a function object');
self.observedCalls.push(observedCall);
self.observedCallsArguments.push(args);
self.callbacks.push(null);
self.collectedCallbackArguments.push(null);
return self;
};
self.addWithCallback = function(observedCall, callback, ...args){
if(typeof observedCall !== 'function')
throw new TypeError('the first argument must be a function object');
if(typeof callback !== 'function')
throw new TypeError('the second argument must be a function object');
self.observedCalls.push(observedCall);
self.observedCallsArguments.push(args);
self.callbacks.push(callback);
self.collectedCallbackArguments.push(null);
return self;
};
// invoke all async calls
self.invoke = function(){
if(self.runningCalls > 0)
throw new Error('this tracker is already running');
self.runningCalls = self.observedCalls.length;
for(var i = 0; i < self.observedCalls.length; ++i){
let index = i;
self.observedCallsArguments[i].push(function(...callbackArgs){
self.collectCallbackArguments({callerIndex:index, args:callbackArgs});
});
self.observedCalls[i](...(self.observedCallsArguments[i]));
}
return self;
};
}
function trackerFactory(){
return new tracker();
}
module.exports = trackerFactory;