async-stream.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @file async-stream.js
  3. */
  4. 'use strict';
  5. Object.defineProperty(exports, '__esModule', {
  6. value: true
  7. });
  8. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  9. var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  12. function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  13. var _stream = require('./stream');
  14. var _stream2 = _interopRequireDefault(_stream);
  15. /**
  16. * A wrapper around the Stream class to use setTiemout
  17. * and run stream "jobs" Asynchronously
  18. *
  19. * @class AsyncStream
  20. * @extends Stream
  21. */
  22. var AsyncStream = (function (_Stream) {
  23. _inherits(AsyncStream, _Stream);
  24. function AsyncStream() {
  25. _classCallCheck(this, AsyncStream);
  26. _get(Object.getPrototypeOf(AsyncStream.prototype), 'constructor', this).call(this, _stream2['default']);
  27. this.jobs = [];
  28. this.delay = 1;
  29. this.timeout_ = null;
  30. }
  31. /**
  32. * process an async job
  33. *
  34. * @private
  35. */
  36. _createClass(AsyncStream, [{
  37. key: 'processJob_',
  38. value: function processJob_() {
  39. this.jobs.shift()();
  40. if (this.jobs.length) {
  41. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  42. } else {
  43. this.timeout_ = null;
  44. }
  45. }
  46. /**
  47. * push a job into the stream
  48. *
  49. * @param {Function} job the job to push into the stream
  50. */
  51. }, {
  52. key: 'push',
  53. value: function push(job) {
  54. this.jobs.push(job);
  55. if (!this.timeout_) {
  56. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  57. }
  58. }
  59. }]);
  60. return AsyncStream;
  61. })(_stream2['default']);
  62. exports['default'] = AsyncStream;
  63. module.exports = exports['default'];