-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (156 loc) · 5.9 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
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
'use strict';
const crypto = require('crypto');
const ResponseTimer = require('./lib/ResponseTimer.js');
const EventEmitter = require('events').EventEmitter;
module.exports = exports = new DynaThrottle();
function DynaThrottle () {
const execTimer = new ResponseTimer();
const emitter = new EventEmitter();
let stacks = {};
function main (promise, data, onFulfill, onReject) {
if (typeof promise !== 'function') {
throw new Error('dyna-throttle: first argument must be a function that returns a Promise');
}
const promiseFunc = promise.bind({});
let namespace = promise.name;
createNamespaceIfNotExists(namespace).then(() => {
let id = newId();
stacks[namespace].stack.push(id);
emitter.on(id, function () {
const key = stacks[namespace].timer.start();
// remove timed out promises when the timer emits a timeout event
// This pattern is the reverse of typical events - timer id is the event name
stacks[namespace].timer.on(key, function (event) {
switch (event) {
case 'timeout':
complete();
onReject(new Error(`Promise timed out after ${stacks[namespace].timer.getTimeout()}ms (namespace: ${namespace})`))
}
});
// execute the promise function when it's time to execute
promiseFunc(data).then(function (data) {
complete();
onFulfill(data);
}).catch(function (err) {
complete();
onReject(err);
});
function complete () {
stacks[namespace].timer.stop(key);
stacks[namespace].last = new Date();
stacks[namespace].stack.shift();
}
});
});
return null;
}
main.getDelay = function (namespace) {
return new Promise(function (fulfill, reject) {
createNamespaceIfNotExists(namespace)
.then(function (ns) {
fulfill(stacks[ns].timer.getAverage() * stacks[ns].factor);
}).catch(reject);
})
};
main.setFactor = function (namespace, factor) {
return new Promise(function (fulfill, reject) {
if (typeof factor !== 'number' || factor < 1) {
reject(new Error('invalid factor passed to setFactor: ' + factor + ' (must be >= 1)'));
return;
}
createNamespaceIfNotExists(namespace)
.then(function (namespace) {
stacks[namespace]['factor'] = factor;
}).catch(reject);
});
};
main.setMaxSamples = function (namespace, maxSamples) {
return new Promise(function (fulfill, reject) {
if (typeof maxSamples !== 'number' || maxSamples < 2) {
reject(new Error('invalid maxSamples passed to setMaxSamples: ' + maxSamples + ' (must be >= 2)'));
return;
}
createNamespaceIfNotExists(namespace)
.then(function (namespace) {
fulfill(stacks[namespace]['timer'].setMaxSamples(maxSamples));
}).catch(reject);
})
};
main.setTimeout = function (namespace, timeout) {
return new Promise(function (fulfill, reject) {
if (typeof timeout !== 'number' || timeout < 0) {
reject(new Error('invalid timeout passed to setTimeout: ' + timeout + ' (must be >= 0)'));
return;
}
createNamespaceIfNotExists(namespace)
.then(function (namespace) {
fulfill(stacks[namespace]['timer'].setTimeout(timeout));
}).catch(reject);
})
};
function createNamespaceIfNotExists (namespace) {
let ns = '';
switch (typeof namespace) {
case 'function':
ns = namespace.name;
break;
case 'string':
ns = namespace;
break;
default:
break;
}
return new Promise(function (fulfill, reject) {
try {
if (!stacks.hasOwnProperty(ns)) {
stacks[ns] = {
timer : new ResponseTimer(),
last : new Date(),
lastId : null,
factor : 10,
stack : []
}
}
} catch (err) {
reject(err);
return;
}
fulfill(ns);
})
}
(function run () {
let timer = execTimer.start();
for (let prop in stacks) {
if (stacks.hasOwnProperty(prop)) {
let t = stacks[prop].timer;
let last = stacks[prop].last.getTime();
let factor = stacks[prop].factor;
let avg = t.getAverage();
if (avg * factor > Date.now() - last) {
continue;
}
let id = stacks[prop].stack[0];
if (!id) {
continue;
}
if (!stacks[prop].lastId) {
stacks[prop].lastId = id;
emitter.emit(id);
continue;
}
if (id !== stacks[prop].lastId) {
emitter.emit(id);
stacks[prop].lastId = id;
}
}
}
execTimer.stop(timer);
setTimeout(run, execTimer.getAverage()*10);
})();
return main;
}
function newId () {
let hash = new crypto.createHash('md5');
hash.update(Math.random().toString() + Date.now());
return hash.digest('hex');
}