windowTime.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.windowTime = void 0;
  4. var Subject_1 = require("../Subject");
  5. var async_1 = require("../scheduler/async");
  6. var Subscription_1 = require("../Subscription");
  7. var lift_1 = require("../util/lift");
  8. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  9. var arrRemove_1 = require("../util/arrRemove");
  10. var args_1 = require("../util/args");
  11. var executeSchedule_1 = require("../util/executeSchedule");
  12. function windowTime(windowTimeSpan) {
  13. var _a, _b;
  14. var otherArgs = [];
  15. for (var _i = 1; _i < arguments.length; _i++) {
  16. otherArgs[_i - 1] = arguments[_i];
  17. }
  18. var scheduler = (_a = args_1.popScheduler(otherArgs)) !== null && _a !== void 0 ? _a : async_1.asyncScheduler;
  19. var windowCreationInterval = (_b = otherArgs[0]) !== null && _b !== void 0 ? _b : null;
  20. var maxWindowSize = otherArgs[1] || Infinity;
  21. return lift_1.operate(function (source, subscriber) {
  22. var windowRecords = [];
  23. var restartOnClose = false;
  24. var closeWindow = function (record) {
  25. var window = record.window, subs = record.subs;
  26. window.complete();
  27. subs.unsubscribe();
  28. arrRemove_1.arrRemove(windowRecords, record);
  29. restartOnClose && startWindow();
  30. };
  31. var startWindow = function () {
  32. if (windowRecords) {
  33. var subs = new Subscription_1.Subscription();
  34. subscriber.add(subs);
  35. var window_1 = new Subject_1.Subject();
  36. var record_1 = {
  37. window: window_1,
  38. subs: subs,
  39. seen: 0,
  40. };
  41. windowRecords.push(record_1);
  42. subscriber.next(window_1.asObservable());
  43. executeSchedule_1.executeSchedule(subs, scheduler, function () { return closeWindow(record_1); }, windowTimeSpan);
  44. }
  45. };
  46. if (windowCreationInterval !== null && windowCreationInterval >= 0) {
  47. executeSchedule_1.executeSchedule(subscriber, scheduler, startWindow, windowCreationInterval, true);
  48. }
  49. else {
  50. restartOnClose = true;
  51. }
  52. startWindow();
  53. var loop = function (cb) { return windowRecords.slice().forEach(cb); };
  54. var terminate = function (cb) {
  55. loop(function (_a) {
  56. var window = _a.window;
  57. return cb(window);
  58. });
  59. cb(subscriber);
  60. subscriber.unsubscribe();
  61. };
  62. source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  63. loop(function (record) {
  64. record.window.next(value);
  65. maxWindowSize <= ++record.seen && closeWindow(record);
  66. });
  67. }, function () { return terminate(function (consumer) { return consumer.complete(); }); }, function (err) { return terminate(function (consumer) { return consumer.error(err); }); }));
  68. return function () {
  69. windowRecords = null;
  70. };
  71. });
  72. }
  73. exports.windowTime = windowTime;
  74. //# sourceMappingURL=windowTime.js.map