windowCount.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { __values } from "tslib";
  2. import { Subject } from '../Subject';
  3. import { operate } from '../util/lift';
  4. import { createOperatorSubscriber } from './OperatorSubscriber';
  5. export function windowCount(windowSize, startWindowEvery) {
  6. if (startWindowEvery === void 0) { startWindowEvery = 0; }
  7. var startEvery = startWindowEvery > 0 ? startWindowEvery : windowSize;
  8. return operate(function (source, subscriber) {
  9. var windows = [new Subject()];
  10. var starts = [];
  11. var count = 0;
  12. subscriber.next(windows[0].asObservable());
  13. source.subscribe(createOperatorSubscriber(subscriber, function (value) {
  14. var e_1, _a;
  15. try {
  16. for (var windows_1 = __values(windows), windows_1_1 = windows_1.next(); !windows_1_1.done; windows_1_1 = windows_1.next()) {
  17. var window_1 = windows_1_1.value;
  18. window_1.next(value);
  19. }
  20. }
  21. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  22. finally {
  23. try {
  24. if (windows_1_1 && !windows_1_1.done && (_a = windows_1.return)) _a.call(windows_1);
  25. }
  26. finally { if (e_1) throw e_1.error; }
  27. }
  28. var c = count - windowSize + 1;
  29. if (c >= 0 && c % startEvery === 0) {
  30. windows.shift().complete();
  31. }
  32. if (++count % startEvery === 0) {
  33. var window_2 = new Subject();
  34. windows.push(window_2);
  35. subscriber.next(window_2.asObservable());
  36. }
  37. }, function () {
  38. while (windows.length > 0) {
  39. windows.shift().complete();
  40. }
  41. subscriber.complete();
  42. }, function (err) {
  43. while (windows.length > 0) {
  44. windows.shift().error(err);
  45. }
  46. subscriber.error(err);
  47. }, function () {
  48. starts = null;
  49. windows = null;
  50. }));
  51. });
  52. }
  53. //# sourceMappingURL=windowCount.js.map