make.js 924 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Module dependencies.
  3. */
  4. var Channel = require('./channel')
  5. var async = require('./async')
  6. /**
  7. * Expose `make`.
  8. */
  9. module.exports = make
  10. /**
  11. * Make a channel.
  12. *
  13. * @param {Number} bufferSize optional default=0
  14. * @return {Function}
  15. * @api public
  16. */
  17. function make(bufferSize) {
  18. var chan = new Channel(bufferSize)
  19. var func = function (a, b) {
  20. // yielded
  21. if (typeof a === 'function') {
  22. return chan.get(a)
  23. }
  24. // (err, res)
  25. if (a === null && typeof b !== 'undefined') {
  26. a = b
  27. }
  28. // value
  29. return chan.add(a)
  30. }
  31. // expose public channel methods
  32. func.close = chan.close.bind(chan)
  33. func.done = chan.done.bind(chan)
  34. // bind async helper
  35. func.async = async.bind(null, func)
  36. // expose empty value
  37. func.empty = chan.empty
  38. // cross reference the channel object and function for internal use
  39. func.__chan = chan
  40. chan.func = func
  41. return func
  42. }