You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.6 KiB

  1. var streamify = require('./streamify.js')
  2. , defer = require('./defer.js')
  3. ;
  4. // API
  5. module.exports = ReadableAsyncKit;
  6. /**
  7. * Base constructor for all streams
  8. * used to hold properties/methods
  9. */
  10. function ReadableAsyncKit()
  11. {
  12. ReadableAsyncKit.super_.apply(this, arguments);
  13. // list of active jobs
  14. this.jobs = {};
  15. // add stream methods
  16. this.destroy = destroy;
  17. this._start = _start;
  18. this._read = _read;
  19. }
  20. /**
  21. * Destroys readable stream,
  22. * by aborting outstanding jobs
  23. *
  24. * @returns {void}
  25. */
  26. function destroy()
  27. {
  28. if (this.destroyed)
  29. {
  30. return;
  31. }
  32. this.destroyed = true;
  33. if (typeof this.terminator == 'function')
  34. {
  35. this.terminator();
  36. }
  37. }
  38. /**
  39. * Starts provided jobs in async manner
  40. *
  41. * @private
  42. */
  43. function _start()
  44. {
  45. // first argument – runner function
  46. var runner = arguments[0]
  47. // take away first argument
  48. , args = Array.prototype.slice.call(arguments, 1)
  49. // second argument - input data
  50. , input = args[0]
  51. // last argument - result callback
  52. , endCb = streamify.callback.call(this, args[args.length - 1])
  53. ;
  54. args[args.length - 1] = endCb;
  55. // third argument - iterator
  56. args[1] = streamify.iterator.call(this, args[1]);
  57. // allow time for proper setup
  58. defer(function()
  59. {
  60. if (!this.destroyed)
  61. {
  62. this.terminator = runner.apply(null, args);
  63. }
  64. else
  65. {
  66. endCb(null, Array.isArray(input) ? [] : {});
  67. }
  68. }.bind(this));
  69. }
  70. /**
  71. * Implement _read to comply with Readable streams
  72. * Doesn't really make sense for flowing object mode
  73. *
  74. * @private
  75. */
  76. function _read()
  77. {
  78. }