windowToggle.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { __values } from "tslib";
  2. import { Subject } from '../Subject';
  3. import { Subscription } from '../Subscription';
  4. import { operate } from '../util/lift';
  5. import { innerFrom } from '../observable/innerFrom';
  6. import { createOperatorSubscriber } from './OperatorSubscriber';
  7. import { noop } from '../util/noop';
  8. import { arrRemove } from '../util/arrRemove';
  9. export function windowToggle(openings, closingSelector) {
  10. return operate(function (source, subscriber) {
  11. var windows = [];
  12. var handleError = function (err) {
  13. while (0 < windows.length) {
  14. windows.shift().error(err);
  15. }
  16. subscriber.error(err);
  17. };
  18. innerFrom(openings).subscribe(createOperatorSubscriber(subscriber, function (openValue) {
  19. var window = new Subject();
  20. windows.push(window);
  21. var closingSubscription = new Subscription();
  22. var closeWindow = function () {
  23. arrRemove(windows, window);
  24. window.complete();
  25. closingSubscription.unsubscribe();
  26. };
  27. var closingNotifier;
  28. try {
  29. closingNotifier = innerFrom(closingSelector(openValue));
  30. }
  31. catch (err) {
  32. handleError(err);
  33. return;
  34. }
  35. subscriber.next(window.asObservable());
  36. closingSubscription.add(closingNotifier.subscribe(createOperatorSubscriber(subscriber, closeWindow, noop, handleError)));
  37. }, noop));
  38. source.subscribe(createOperatorSubscriber(subscriber, function (value) {
  39. var e_1, _a;
  40. var windowsCopy = windows.slice();
  41. try {
  42. for (var windowsCopy_1 = __values(windowsCopy), windowsCopy_1_1 = windowsCopy_1.next(); !windowsCopy_1_1.done; windowsCopy_1_1 = windowsCopy_1.next()) {
  43. var window_1 = windowsCopy_1_1.value;
  44. window_1.next(value);
  45. }
  46. }
  47. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  48. finally {
  49. try {
  50. if (windowsCopy_1_1 && !windowsCopy_1_1.done && (_a = windowsCopy_1.return)) _a.call(windowsCopy_1);
  51. }
  52. finally { if (e_1) throw e_1.error; }
  53. }
  54. }, function () {
  55. while (0 < windows.length) {
  56. windows.shift().complete();
  57. }
  58. subscriber.complete();
  59. }, handleError, function () {
  60. while (0 < windows.length) {
  61. windows.shift().unsubscribe();
  62. }
  63. }));
  64. });
  65. }
  66. //# sourceMappingURL=windowToggle.js.map