forked from brentropy/chan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan.js
233 lines (196 loc) · 4.28 KB
/
chan.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
var make
, Channel
, selectors = []; // selectors waiting for channel activity
/**
* Make a channel.
*
* @param {Function|Object} [empty]
* @return {Function}
* @api public
*/
make = function make(empty) {
var chan = new Channel(empty)
, func;
func = function(a, b) {
// yielded
if (typeof a === 'function') {
chan.get(a);
return;
}
// (err, res)
if (a === null && typeof b !== 'undefined') {
a = b;
}
// value
return chan.add(a);
};
// expose Channel.protoptype.close
func.close = chan.close.bind(chan);
// expose Channel.protoptype.done
func.done = chan.done.bind(chan);
// expose empty value
func.empty = chan.empty;
// save a reference to the channel
func.__chan = chan;
return func;
};
/**
* Initialize a `Channel`.
*
* @param {Function|Object} [empty=Object]
* @api priate
*/
Channel = function Channel(empty) {
var EmptyCtor;
this.queue = [];
this.items = [];
this.isClosed = false;
this.isDone = false;
if (typeof empty !== 'object') {
EmptyCtor = typeof empty === 'function' ? empty : Object;
empty = new EmptyCtor();
}
this.empty = empty;
};
/**
* Get an item with `cb`.
*
* @param {Function} cb
* @api private
*/
Channel.prototype.get = function(cb){
if (this.done()) {
this.callEmpty(cb);
} else if (this.items.length > 0) {
this.call(cb, this.items.shift());
} else {
this.queue.push(cb);
}
};
/**
* Add `val` to the channel.
*
* @param {Mixed} val
* @api private
*/
Channel.prototype.add = function(val){
if (this.isClosed) {
throw new Error('Cannot add to closed channel');
} else if (this.queue.length > 0) {
this.call(this.queue.shift(), val);
notifySelectors(this);
} else {
this.items.push(val);
notifySelectors(this);
}
};
/**
* Invoke `cb` with `val` facilitate both
* `chan(value)` and the `chan(error, value)`
* use-cases.
*
* @param {Function} cb
* @param {Mixed} val
* @api private
*/
Channel.prototype.call = function(cb, val){
if (val instanceof Error) {
cb(val);
} else {
cb(null, val);
}
this.done();
};
/**
* Invoke `cb` callback with the empty value.
*
* @param {Function} cb
* @api private
*/
Channel.prototype.callEmpty = function(cb) {
this.call(cb, this.empty);
};
/**
* Prevennt future values from being added to
* the channel.
*
* @api private
*/
Channel.prototype.close = function() {
this.isClosed = true;
return this.done();
};
/**
* Check to see if the channel is done and
* call pending callbacks if necessary.
*
* @return {Boolean}
* @api private
*/
Channel.prototype.done = function() {
if (!this.isDone && this.isClosed && this.items.length === 0) {
this.isDone = true;
this.queue.forEach(
function(cb) {
this.callEmpty(cb);
},
this
);
}
return this.isDone;
};
/**
* Expose `make()`.
*/
module.exports = make;
/**
* Wait for activity on a list of channels.
*
* @param {channel[]} channels
* @api public
*/
module.exports.select = select;
function select(channels) {
return function(cb) {
selectors.push({ channels: channels, cb: cb });
// check to see if there are any waiting messages
channels.some(function (ch) {
var channel = ch.__chan;
return !channel.isDone && channel.items.length &&
notifySelectors(channel);
});
};
}
/**
* Notify anyone waiting on a select()
*
* @api private
*/
function notifySelectors(ch) {
selectors.forEach(function (selector, pos) {
// is this selector waiting for this channel?
var selectorHasChannel = selector.channels.some(
function (func) {
return ch === func.__chan;
});
if (selectorHasChannel) {
// find the channels with items ready
var hits = selector.channels.filter(function (func) {
var channel = func.__chan;
return !channel.isDone && channel.items.length;
});
if (hits.length) {
var idx = 0;
// if multiple ready channels, then pick a channel at random
if (hits.length > 1) {
idx = Math.floor(Math.random() * hits.length);
}
// remove selector from notification list
selectors.splice(pos, 1);
selector.cb(null, hits[idx]);
return true;
}
}
});
}