-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
do.js
170 lines (149 loc) · 3.87 KB
/
do.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
'use strict';
function Do() {}
const chain = function (fn, ...args) {
const current = (done) => {
if (done) current.done = done;
if (current.prev) {
current.prev.next = current;
current.prev();
} else {
current.forward();
}
return current;
};
const prev = this instanceof Do ? this : null;
const fields = { prev, fn, args, done: null };
Object.setPrototypeOf(current, Do.prototype);
return Object.assign(current, fields);
};
Do.prototype.do = function (fn, ...args) {
return chain.call(this, fn, ...args);
};
Do.prototype.forward = function () {
if (this.fn)
this.fn(...this.args, (err, data) => {
const next = this.next;
if (next) {
if (next.fn) next.forward();
} else if (this.done) {
this.done(err, data);
}
});
};
function Collector() {}
Collector.prototype.collect = function (key, err, value) {
if (this.finished) return this;
if (err) {
this.finalize(err, this.data);
return this;
}
if (this.expectKeys && !this.expectKeys.has(key)) {
if (this.unique) {
const err = new Error('Unexpected key: ' + key);
this.finalize(err, this.data);
return this;
}
} else if (!this.keys.has(key)) {
this.count++;
}
this.data[key] = value;
this.keys.add(key);
if (this.expected === this.count) {
this.finalize(null, this.data);
}
return this;
};
Collector.prototype.pick = function (key, value) {
this.collect(key, null, value);
return this;
};
Collector.prototype.fail = function (key, err) {
this.collect(key, err);
return this;
};
Collector.prototype.take = function (key, fn, ...args) {
fn(...args, (err, data) => {
this.collect(key, err, data);
});
return this;
};
Collector.prototype.callback = function (key) {
return (...args) => this(key, ...args);
};
Collector.prototype.timeout = function (msec) {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
if (msec > 0) {
this.timer = setTimeout(() => {
const err = new Error('Collector timed out');
this.finalize(err, this.data);
}, msec);
}
return this;
};
Collector.prototype.done = function (callback) {
this.finish = callback;
return this;
};
Collector.prototype.finalize = function (err, data) {
if (this.finished) return this;
if (this.finish) {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
this.finished = true;
if (this.finish) this.finish(err, data);
}
return this;
};
Collector.prototype.distinct = function (value = true) {
this.unique = value;
return this;
};
Collector.prototype.cancel = function (err) {
err = err || new Error('Collector cancelled');
this.finalize(err, this.data);
return this;
};
Collector.prototype.then = function (fulfill, reject) {
this.finish = (err, result) => {
if (err) reject(err);
else fulfill(result);
};
return this;
};
// Collector instance constructor
// expected <number> or array of string,
// Returns: <Function> Collector
const collect = (expected) => {
const expectKeys = Array.isArray(expected) ? new Set(expected) : null;
const fields = {
expectKeys,
expected: expectKeys ? expectKeys.size : expected,
keys: new Set(),
count: 0,
timer: null,
finish: null,
unique: false,
finished: false,
data: {},
};
const collector = (...args) => {
if (args.length === 1) return collector.callback(args[0]);
if (args.length === 2) {
if (args[1] instanceof Error) return collector.fail(...args);
else return collector.pick(...args);
}
if (typeof args[1] === 'function') return collector.take(...args);
return collector.collect(...args);
};
Object.setPrototypeOf(collector, Collector.prototype);
return Object.assign(collector, fields);
};
const ex = (...args) =>
(typeof args[0] === 'function' ? chain : collect)(...args);
ex.do = ex;
module.exports = ex;