Front end of the Slack clone application.

windowToggle.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /** PURE_IMPORTS_START .._Subject,.._Subscription,.._util_tryCatch,.._util_errorObject,.._OuterSubscriber,.._util_subscribeToResult PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subject } from '../Subject';
  10. import { Subscription } from '../Subscription';
  11. import { tryCatch } from '../util/tryCatch';
  12. import { errorObject } from '../util/errorObject';
  13. import { OuterSubscriber } from '../OuterSubscriber';
  14. import { subscribeToResult } from '../util/subscribeToResult';
  15. /**
  16. * Branch out the source Observable values as a nested Observable starting from
  17. * an emission from `openings` and ending when the output of `closingSelector`
  18. * emits.
  19. *
  20. * <span class="informal">It's like {@link bufferToggle}, but emits a nested
  21. * Observable instead of an array.</span>
  22. *
  23. * <img src="./img/windowToggle.png" width="100%">
  24. *
  25. * Returns an Observable that emits windows of items it collects from the source
  26. * Observable. The output Observable emits windows that contain those items
  27. * emitted by the source Observable between the time when the `openings`
  28. * Observable emits an item and when the Observable returned by
  29. * `closingSelector` emits an item.
  30. *
  31. * @example <caption>Every other second, emit the click events from the next 500ms</caption>
  32. * var clicks = Rx.Observable.fromEvent(document, 'click');
  33. * var openings = Rx.Observable.interval(1000);
  34. * var result = clicks.windowToggle(openings, i =>
  35. * i % 2 ? Rx.Observable.interval(500) : Rx.Observable.empty()
  36. * ).mergeAll();
  37. * result.subscribe(x => console.log(x));
  38. *
  39. * @see {@link window}
  40. * @see {@link windowCount}
  41. * @see {@link windowTime}
  42. * @see {@link windowWhen}
  43. * @see {@link bufferToggle}
  44. *
  45. * @param {Observable<O>} openings An observable of notifications to start new
  46. * windows.
  47. * @param {function(value: O): Observable} closingSelector A function that takes
  48. * the value emitted by the `openings` observable and returns an Observable,
  49. * which, when it emits (either `next` or `complete`), signals that the
  50. * associated window should complete.
  51. * @return {Observable<Observable<T>>} An observable of windows, which in turn
  52. * are Observables.
  53. * @method windowToggle
  54. * @owner Observable
  55. */
  56. export function windowToggle(openings, closingSelector) {
  57. return function (source) { return source.lift(new WindowToggleOperator(openings, closingSelector)); };
  58. }
  59. var WindowToggleOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  60. function WindowToggleOperator(openings, closingSelector) {
  61. this.openings = openings;
  62. this.closingSelector = closingSelector;
  63. }
  64. WindowToggleOperator.prototype.call = function (subscriber, source) {
  65. return source.subscribe(new WindowToggleSubscriber(subscriber, this.openings, this.closingSelector));
  66. };
  67. return WindowToggleOperator;
  68. }());
  69. /**
  70. * We need this JSDoc comment for affecting ESDoc.
  71. * @ignore
  72. * @extends {Ignored}
  73. */
  74. var WindowToggleSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  75. __extends(WindowToggleSubscriber, _super);
  76. function WindowToggleSubscriber(destination, openings, closingSelector) {
  77. _super.call(this, destination);
  78. this.openings = openings;
  79. this.closingSelector = closingSelector;
  80. this.contexts = [];
  81. this.add(this.openSubscription = subscribeToResult(this, openings, openings));
  82. }
  83. WindowToggleSubscriber.prototype._next = function (value) {
  84. var contexts = this.contexts;
  85. if (contexts) {
  86. var len = contexts.length;
  87. for (var i = 0; i < len; i++) {
  88. contexts[i].window.next(value);
  89. }
  90. }
  91. };
  92. WindowToggleSubscriber.prototype._error = function (err) {
  93. var contexts = this.contexts;
  94. this.contexts = null;
  95. if (contexts) {
  96. var len = contexts.length;
  97. var index = -1;
  98. while (++index < len) {
  99. var context = contexts[index];
  100. context.window.error(err);
  101. context.subscription.unsubscribe();
  102. }
  103. }
  104. _super.prototype._error.call(this, err);
  105. };
  106. WindowToggleSubscriber.prototype._complete = function () {
  107. var contexts = this.contexts;
  108. this.contexts = null;
  109. if (contexts) {
  110. var len = contexts.length;
  111. var index = -1;
  112. while (++index < len) {
  113. var context = contexts[index];
  114. context.window.complete();
  115. context.subscription.unsubscribe();
  116. }
  117. }
  118. _super.prototype._complete.call(this);
  119. };
  120. /** @deprecated internal use only */ WindowToggleSubscriber.prototype._unsubscribe = function () {
  121. var contexts = this.contexts;
  122. this.contexts = null;
  123. if (contexts) {
  124. var len = contexts.length;
  125. var index = -1;
  126. while (++index < len) {
  127. var context = contexts[index];
  128. context.window.unsubscribe();
  129. context.subscription.unsubscribe();
  130. }
  131. }
  132. };
  133. WindowToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  134. if (outerValue === this.openings) {
  135. var closingSelector = this.closingSelector;
  136. var closingNotifier = tryCatch(closingSelector)(innerValue);
  137. if (closingNotifier === errorObject) {
  138. return this.error(errorObject.e);
  139. }
  140. else {
  141. var window_1 = new Subject();
  142. var subscription = new Subscription();
  143. var context = { window: window_1, subscription: subscription };
  144. this.contexts.push(context);
  145. var innerSubscription = subscribeToResult(this, closingNotifier, context);
  146. if (innerSubscription.closed) {
  147. this.closeWindow(this.contexts.length - 1);
  148. }
  149. else {
  150. innerSubscription.context = context;
  151. subscription.add(innerSubscription);
  152. }
  153. this.destination.next(window_1);
  154. }
  155. }
  156. else {
  157. this.closeWindow(this.contexts.indexOf(outerValue));
  158. }
  159. };
  160. WindowToggleSubscriber.prototype.notifyError = function (err) {
  161. this.error(err);
  162. };
  163. WindowToggleSubscriber.prototype.notifyComplete = function (inner) {
  164. if (inner !== this.openSubscription) {
  165. this.closeWindow(this.contexts.indexOf(inner.context));
  166. }
  167. };
  168. WindowToggleSubscriber.prototype.closeWindow = function (index) {
  169. if (index === -1) {
  170. return;
  171. }
  172. var contexts = this.contexts;
  173. var context = contexts[index];
  174. var window = context.window, subscription = context.subscription;
  175. contexts.splice(index, 1);
  176. window.complete();
  177. subscription.unsubscribe();
  178. };
  179. return WindowToggleSubscriber;
  180. }(OuterSubscriber));
  181. //# sourceMappingURL=windowToggle.js.map