make.js
924 字节
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
/**
* Module dependencies.
*/
var Channel = require('./channel')
var async = require('./async')
/**
* Expose `make`.
*/
module.exports = make
/**
* Make a channel.
*
* @param {Number} bufferSize optional default=0
* @return {Function}
* @api public
*/
function make(bufferSize) {
var chan = new Channel(bufferSize)
var func = function (a, b) {
// yielded
if (typeof a === 'function') {
return chan.get(a)
}
// (err, res)
if (a === null && typeof b !== 'undefined') {
a = b
}
// value
return chan.add(a)
}
// expose public channel methods
func.close = chan.close.bind(chan)
func.done = chan.done.bind(chan)
// bind async helper
func.async = async.bind(null, func)
// expose empty value
func.empty = chan.empty
// cross reference the channel object and function for internal use
func.__chan = chan
chan.func = func
return func
}