windowWhen.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /**
  5. * Branch out the source Observable values as a nested Observable using a
  6. * factory function of closing Observables to determine when to start a new
  7. * window.
  8. *
  9. * <span class="informal">It's like {@link bufferWhen}, but emits a nested
  10. * Observable instead of an array.</span>
  11. *
  12. * <img src="./img/windowWhen.png" width="100%">
  13. *
  14. * Returns an Observable that emits windows of items it collects from the source
  15. * Observable. The output Observable emits connected, non-overlapping windows.
  16. * It emits the current window and opens a new one whenever the Observable
  17. * produced by the specified `closingSelector` function emits an item. The first
  18. * window is opened immediately when subscribing to the output Observable.
  19. *
  20. * @example <caption>Emit only the first two clicks events in every window of [1-5] random seconds</caption>
  21. * var clicks = Rx.Observable.fromEvent(document, 'click');
  22. * var result = clicks
  23. * .windowWhen(() => Rx.Observable.interval(1000 + Math.random() * 4000))
  24. * .map(win => win.take(2)) // each window has at most 2 emissions
  25. * .mergeAll(); // flatten the Observable-of-Observables
  26. * result.subscribe(x => console.log(x));
  27. *
  28. * @see {@link window}
  29. * @see {@link windowCount}
  30. * @see {@link windowTime}
  31. * @see {@link windowToggle}
  32. * @see {@link bufferWhen}
  33. *
  34. * @param {function(): Observable} closingSelector A function that takes no
  35. * arguments and returns an Observable that signals (on either `next` or
  36. * `complete`) when to close the previous window and start a new one.
  37. * @return {Observable<Observable<T>>} An observable of windows, which in turn
  38. * are Observables.
  39. * @method windowWhen
  40. * @owner Observable
  41. */
  42. function windowWhen(closingSelector) {
  43. return operators_1.windowWhen(closingSelector)(this);
  44. }
  45. exports.windowWhen = windowWhen;
  46. //# sourceMappingURL=windowWhen.js.map