windowToggle.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Subject } from '../Subject';
  2. import { Subscription } from '../Subscription';
  3. import { operate } from '../util/lift';
  4. import { innerFrom } from '../observable/innerFrom';
  5. import { createOperatorSubscriber } from './OperatorSubscriber';
  6. import { noop } from '../util/noop';
  7. import { arrRemove } from '../util/arrRemove';
  8. export function windowToggle(openings, closingSelector) {
  9. return operate((source, subscriber) => {
  10. const windows = [];
  11. const handleError = (err) => {
  12. while (0 < windows.length) {
  13. windows.shift().error(err);
  14. }
  15. subscriber.error(err);
  16. };
  17. innerFrom(openings).subscribe(createOperatorSubscriber(subscriber, (openValue) => {
  18. const window = new Subject();
  19. windows.push(window);
  20. const closingSubscription = new Subscription();
  21. const closeWindow = () => {
  22. arrRemove(windows, window);
  23. window.complete();
  24. closingSubscription.unsubscribe();
  25. };
  26. let closingNotifier;
  27. try {
  28. closingNotifier = innerFrom(closingSelector(openValue));
  29. }
  30. catch (err) {
  31. handleError(err);
  32. return;
  33. }
  34. subscriber.next(window.asObservable());
  35. closingSubscription.add(closingNotifier.subscribe(createOperatorSubscriber(subscriber, closeWindow, noop, handleError)));
  36. }, noop));
  37. source.subscribe(createOperatorSubscriber(subscriber, (value) => {
  38. const windowsCopy = windows.slice();
  39. for (const window of windowsCopy) {
  40. window.next(value);
  41. }
  42. }, () => {
  43. while (0 < windows.length) {
  44. windows.shift().complete();
  45. }
  46. subscriber.complete();
  47. }, handleError, () => {
  48. while (0 < windows.length) {
  49. windows.shift().unsubscribe();
  50. }
  51. }));
  52. });
  53. }
  54. //# sourceMappingURL=windowToggle.js.map