-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (103 loc) · 2.82 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
export function queued() {
let timeout = null;
let fn;
let opts;
let queue = [];
// The first argument must be the function to wrap
if (typeof arguments[0] !== 'function') {
throw({ message: "First argument must be a function." });
} else {
fn = [].shift.call(arguments);
}
// Accept { timout: <milliseconds> }
if (typeof arguments[0] === 'object') {
opts = [].shift.call(arguments);
if (opts.hasOwnProperty('timeout')) {
if (typeof opts.timeout === 'number') {
timeout = opts.timeout;
} else {
throw({ message: "'timeout' must be a number" });
}
}
}
let invoke = function() {
// Any calls left in the queue?
if (!queue[0]) { return; }
// Set up release callback for this invocation. It will either be called
// by the user or triggered by the timeout.
let released = false;
let release = function() {
if (released) { return; }
released = true;
// Clear this call's timer.
clearTimeout(timer);
// Remove this call from the queue. A call stays in the queue during
// its execution.
queue.shift();
// Place next invoke() in runtime's event queue, to be executed as
// soon as the current execution stack is cleared.
if (queue.length > 0) {
setTimeout(invoke, 0);
}
};
// Set up the timeout
let timer;
if (timeout) {
timer = setTimeout(release, timeout);
}
// Call the user's function
return fn.call(fn, release, ...queue[0]);
}
return function() {
// Enqueue this call
queue.push(arguments);
// If there were no other calls in the queue, then invoke immediately.
// Otherwise, this call will be invoked by an earlier call's release
// callback.
if (queue.length === 1) {
return invoke();
}
// TODO return a Promise
return null;
};
}
export function passThrough() {
let locked = false;
let timeout = null;
let timer;
let fn;
let opts;
// The first argument must be the function to wrap
if (typeof arguments[0] !== 'function') {
throw({ message: "First argument must be a function." });
} else {
fn = [].shift.call(arguments);
}
// Accept { timout: <milliseconds> }
if (typeof arguments[0] === 'object') {
opts = [].shift.call(arguments);
if (opts.hasOwnProperty('timeout')) {
if (typeof opts.timeout === 'number') {
timeout = opts.timeout;
} else {
throw({ message: "'timeout' must be a number" });
}
}
}
let release = function() {
locked = false;
clearTimeout(timer);
};
let obtain = function() {
if (locked) { return false; }
if (timeout) {
timer = setTimeout(release, timeout);
}
locked = true;
return true;
};
return function() {
if (!obtain()) { return; }
return fn.call(fn, release, ...arguments);
};
}