windowToggle.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /** PURE_IMPORTS_START tslib,_Subject,_Subscription,_OuterSubscriber,_util_subscribeToResult PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subject } from '../Subject';
  4. import { Subscription } from '../Subscription';
  5. import { OuterSubscriber } from '../OuterSubscriber';
  6. import { subscribeToResult } from '../util/subscribeToResult';
  7. export function windowToggle(openings, closingSelector) {
  8. return function (source) { return source.lift(new WindowToggleOperator(openings, closingSelector)); };
  9. }
  10. var WindowToggleOperator = /*@__PURE__*/ (function () {
  11. function WindowToggleOperator(openings, closingSelector) {
  12. this.openings = openings;
  13. this.closingSelector = closingSelector;
  14. }
  15. WindowToggleOperator.prototype.call = function (subscriber, source) {
  16. return source.subscribe(new WindowToggleSubscriber(subscriber, this.openings, this.closingSelector));
  17. };
  18. return WindowToggleOperator;
  19. }());
  20. var WindowToggleSubscriber = /*@__PURE__*/ (function (_super) {
  21. tslib_1.__extends(WindowToggleSubscriber, _super);
  22. function WindowToggleSubscriber(destination, openings, closingSelector) {
  23. var _this = _super.call(this, destination) || this;
  24. _this.openings = openings;
  25. _this.closingSelector = closingSelector;
  26. _this.contexts = [];
  27. _this.add(_this.openSubscription = subscribeToResult(_this, openings, openings));
  28. return _this;
  29. }
  30. WindowToggleSubscriber.prototype._next = function (value) {
  31. var contexts = this.contexts;
  32. if (contexts) {
  33. var len = contexts.length;
  34. for (var i = 0; i < len; i++) {
  35. contexts[i].window.next(value);
  36. }
  37. }
  38. };
  39. WindowToggleSubscriber.prototype._error = function (err) {
  40. var contexts = this.contexts;
  41. this.contexts = null;
  42. if (contexts) {
  43. var len = contexts.length;
  44. var index = -1;
  45. while (++index < len) {
  46. var context_1 = contexts[index];
  47. context_1.window.error(err);
  48. context_1.subscription.unsubscribe();
  49. }
  50. }
  51. _super.prototype._error.call(this, err);
  52. };
  53. WindowToggleSubscriber.prototype._complete = function () {
  54. var contexts = this.contexts;
  55. this.contexts = null;
  56. if (contexts) {
  57. var len = contexts.length;
  58. var index = -1;
  59. while (++index < len) {
  60. var context_2 = contexts[index];
  61. context_2.window.complete();
  62. context_2.subscription.unsubscribe();
  63. }
  64. }
  65. _super.prototype._complete.call(this);
  66. };
  67. WindowToggleSubscriber.prototype._unsubscribe = function () {
  68. var contexts = this.contexts;
  69. this.contexts = null;
  70. if (contexts) {
  71. var len = contexts.length;
  72. var index = -1;
  73. while (++index < len) {
  74. var context_3 = contexts[index];
  75. context_3.window.unsubscribe();
  76. context_3.subscription.unsubscribe();
  77. }
  78. }
  79. };
  80. WindowToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  81. if (outerValue === this.openings) {
  82. var closingNotifier = void 0;
  83. try {
  84. var closingSelector = this.closingSelector;
  85. closingNotifier = closingSelector(innerValue);
  86. }
  87. catch (e) {
  88. return this.error(e);
  89. }
  90. var window_1 = new Subject();
  91. var subscription = new Subscription();
  92. var context_4 = { window: window_1, subscription: subscription };
  93. this.contexts.push(context_4);
  94. var innerSubscription = subscribeToResult(this, closingNotifier, context_4);
  95. if (innerSubscription.closed) {
  96. this.closeWindow(this.contexts.length - 1);
  97. }
  98. else {
  99. innerSubscription.context = context_4;
  100. subscription.add(innerSubscription);
  101. }
  102. this.destination.next(window_1);
  103. }
  104. else {
  105. this.closeWindow(this.contexts.indexOf(outerValue));
  106. }
  107. };
  108. WindowToggleSubscriber.prototype.notifyError = function (err) {
  109. this.error(err);
  110. };
  111. WindowToggleSubscriber.prototype.notifyComplete = function (inner) {
  112. if (inner !== this.openSubscription) {
  113. this.closeWindow(this.contexts.indexOf(inner.context));
  114. }
  115. };
  116. WindowToggleSubscriber.prototype.closeWindow = function (index) {
  117. if (index === -1) {
  118. return;
  119. }
  120. var contexts = this.contexts;
  121. var context = contexts[index];
  122. var window = context.window, subscription = context.subscription;
  123. contexts.splice(index, 1);
  124. window.complete();
  125. subscription.unsubscribe();
  126. };
  127. return WindowToggleSubscriber;
  128. }(OuterSubscriber));
  129. //# sourceMappingURL=windowToggle.js.map