Front end of the Slack clone application.

zip.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 ArrayObservable_1 = require('../observable/ArrayObservable');
  8. var isArray_1 = require('../util/isArray');
  9. var Subscriber_1 = require('../Subscriber');
  10. var OuterSubscriber_1 = require('../OuterSubscriber');
  11. var subscribeToResult_1 = require('../util/subscribeToResult');
  12. var iterator_1 = require('../symbol/iterator');
  13. /* tslint:enable:max-line-length */
  14. /**
  15. * @param observables
  16. * @return {Observable<R>}
  17. * @method zip
  18. * @owner Observable
  19. */
  20. function zip() {
  21. var observables = [];
  22. for (var _i = 0; _i < arguments.length; _i++) {
  23. observables[_i - 0] = arguments[_i];
  24. }
  25. return function zipOperatorFunction(source) {
  26. return source.lift.call(zipStatic.apply(void 0, [source].concat(observables)));
  27. };
  28. }
  29. exports.zip = zip;
  30. /* tslint:enable:max-line-length */
  31. /**
  32. * Combines multiple Observables to create an Observable whose values are calculated from the values, in order, of each
  33. * of its input Observables.
  34. *
  35. * If the latest parameter is a function, this function is used to compute the created value from the input values.
  36. * Otherwise, an array of the input values is returned.
  37. *
  38. * @example <caption>Combine age and name from different sources</caption>
  39. *
  40. * let age$ = Observable.of<number>(27, 25, 29);
  41. * let name$ = Observable.of<string>('Foo', 'Bar', 'Beer');
  42. * let isDev$ = Observable.of<boolean>(true, true, false);
  43. *
  44. * Observable
  45. * .zip(age$,
  46. * name$,
  47. * isDev$,
  48. * (age: number, name: string, isDev: boolean) => ({ age, name, isDev }))
  49. * .subscribe(x => console.log(x));
  50. *
  51. * // outputs
  52. * // { age: 27, name: 'Foo', isDev: true }
  53. * // { age: 25, name: 'Bar', isDev: true }
  54. * // { age: 29, name: 'Beer', isDev: false }
  55. *
  56. * @param observables
  57. * @return {Observable<R>}
  58. * @static true
  59. * @name zip
  60. * @owner Observable
  61. */
  62. function zipStatic() {
  63. var observables = [];
  64. for (var _i = 0; _i < arguments.length; _i++) {
  65. observables[_i - 0] = arguments[_i];
  66. }
  67. var project = observables[observables.length - 1];
  68. if (typeof project === 'function') {
  69. observables.pop();
  70. }
  71. return new ArrayObservable_1.ArrayObservable(observables).lift(new ZipOperator(project));
  72. }
  73. exports.zipStatic = zipStatic;
  74. var ZipOperator = (function () {
  75. function ZipOperator(project) {
  76. this.project = project;
  77. }
  78. ZipOperator.prototype.call = function (subscriber, source) {
  79. return source.subscribe(new ZipSubscriber(subscriber, this.project));
  80. };
  81. return ZipOperator;
  82. }());
  83. exports.ZipOperator = ZipOperator;
  84. /**
  85. * We need this JSDoc comment for affecting ESDoc.
  86. * @ignore
  87. * @extends {Ignored}
  88. */
  89. var ZipSubscriber = (function (_super) {
  90. __extends(ZipSubscriber, _super);
  91. function ZipSubscriber(destination, project, values) {
  92. if (values === void 0) { values = Object.create(null); }
  93. _super.call(this, destination);
  94. this.iterators = [];
  95. this.active = 0;
  96. this.project = (typeof project === 'function') ? project : null;
  97. this.values = values;
  98. }
  99. ZipSubscriber.prototype._next = function (value) {
  100. var iterators = this.iterators;
  101. if (isArray_1.isArray(value)) {
  102. iterators.push(new StaticArrayIterator(value));
  103. }
  104. else if (typeof value[iterator_1.iterator] === 'function') {
  105. iterators.push(new StaticIterator(value[iterator_1.iterator]()));
  106. }
  107. else {
  108. iterators.push(new ZipBufferIterator(this.destination, this, value));
  109. }
  110. };
  111. ZipSubscriber.prototype._complete = function () {
  112. var iterators = this.iterators;
  113. var len = iterators.length;
  114. if (len === 0) {
  115. this.destination.complete();
  116. return;
  117. }
  118. this.active = len;
  119. for (var i = 0; i < len; i++) {
  120. var iterator = iterators[i];
  121. if (iterator.stillUnsubscribed) {
  122. this.add(iterator.subscribe(iterator, i));
  123. }
  124. else {
  125. this.active--; // not an observable
  126. }
  127. }
  128. };
  129. ZipSubscriber.prototype.notifyInactive = function () {
  130. this.active--;
  131. if (this.active === 0) {
  132. this.destination.complete();
  133. }
  134. };
  135. ZipSubscriber.prototype.checkIterators = function () {
  136. var iterators = this.iterators;
  137. var len = iterators.length;
  138. var destination = this.destination;
  139. // abort if not all of them have values
  140. for (var i = 0; i < len; i++) {
  141. var iterator = iterators[i];
  142. if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
  143. return;
  144. }
  145. }
  146. var shouldComplete = false;
  147. var args = [];
  148. for (var i = 0; i < len; i++) {
  149. var iterator = iterators[i];
  150. var result = iterator.next();
  151. // check to see if it's completed now that you've gotten
  152. // the next value.
  153. if (iterator.hasCompleted()) {
  154. shouldComplete = true;
  155. }
  156. if (result.done) {
  157. destination.complete();
  158. return;
  159. }
  160. args.push(result.value);
  161. }
  162. if (this.project) {
  163. this._tryProject(args);
  164. }
  165. else {
  166. destination.next(args);
  167. }
  168. if (shouldComplete) {
  169. destination.complete();
  170. }
  171. };
  172. ZipSubscriber.prototype._tryProject = function (args) {
  173. var result;
  174. try {
  175. result = this.project.apply(this, args);
  176. }
  177. catch (err) {
  178. this.destination.error(err);
  179. return;
  180. }
  181. this.destination.next(result);
  182. };
  183. return ZipSubscriber;
  184. }(Subscriber_1.Subscriber));
  185. exports.ZipSubscriber = ZipSubscriber;
  186. var StaticIterator = (function () {
  187. function StaticIterator(iterator) {
  188. this.iterator = iterator;
  189. this.nextResult = iterator.next();
  190. }
  191. StaticIterator.prototype.hasValue = function () {
  192. return true;
  193. };
  194. StaticIterator.prototype.next = function () {
  195. var result = this.nextResult;
  196. this.nextResult = this.iterator.next();
  197. return result;
  198. };
  199. StaticIterator.prototype.hasCompleted = function () {
  200. var nextResult = this.nextResult;
  201. return nextResult && nextResult.done;
  202. };
  203. return StaticIterator;
  204. }());
  205. var StaticArrayIterator = (function () {
  206. function StaticArrayIterator(array) {
  207. this.array = array;
  208. this.index = 0;
  209. this.length = 0;
  210. this.length = array.length;
  211. }
  212. StaticArrayIterator.prototype[iterator_1.iterator] = function () {
  213. return this;
  214. };
  215. StaticArrayIterator.prototype.next = function (value) {
  216. var i = this.index++;
  217. var array = this.array;
  218. return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
  219. };
  220. StaticArrayIterator.prototype.hasValue = function () {
  221. return this.array.length > this.index;
  222. };
  223. StaticArrayIterator.prototype.hasCompleted = function () {
  224. return this.array.length === this.index;
  225. };
  226. return StaticArrayIterator;
  227. }());
  228. /**
  229. * We need this JSDoc comment for affecting ESDoc.
  230. * @ignore
  231. * @extends {Ignored}
  232. */
  233. var ZipBufferIterator = (function (_super) {
  234. __extends(ZipBufferIterator, _super);
  235. function ZipBufferIterator(destination, parent, observable) {
  236. _super.call(this, destination);
  237. this.parent = parent;
  238. this.observable = observable;
  239. this.stillUnsubscribed = true;
  240. this.buffer = [];
  241. this.isComplete = false;
  242. }
  243. ZipBufferIterator.prototype[iterator_1.iterator] = function () {
  244. return this;
  245. };
  246. // NOTE: there is actually a name collision here with Subscriber.next and Iterator.next
  247. // this is legit because `next()` will never be called by a subscription in this case.
  248. ZipBufferIterator.prototype.next = function () {
  249. var buffer = this.buffer;
  250. if (buffer.length === 0 && this.isComplete) {
  251. return { value: null, done: true };
  252. }
  253. else {
  254. return { value: buffer.shift(), done: false };
  255. }
  256. };
  257. ZipBufferIterator.prototype.hasValue = function () {
  258. return this.buffer.length > 0;
  259. };
  260. ZipBufferIterator.prototype.hasCompleted = function () {
  261. return this.buffer.length === 0 && this.isComplete;
  262. };
  263. ZipBufferIterator.prototype.notifyComplete = function () {
  264. if (this.buffer.length > 0) {
  265. this.isComplete = true;
  266. this.parent.notifyInactive();
  267. }
  268. else {
  269. this.destination.complete();
  270. }
  271. };
  272. ZipBufferIterator.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  273. this.buffer.push(innerValue);
  274. this.parent.checkIterators();
  275. };
  276. ZipBufferIterator.prototype.subscribe = function (value, index) {
  277. return subscribeToResult_1.subscribeToResult(this, this.observable, this, index);
  278. };
  279. return ZipBufferIterator;
  280. }(OuterSubscriber_1.OuterSubscriber));
  281. //# sourceMappingURL=zip.js.map