Front end of the Slack clone application.

windowToggle.js 7.1KB

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