zip.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /** PURE_IMPORTS_START .._observable_ArrayObservable,.._util_isArray,.._Subscriber,.._OuterSubscriber,.._util_subscribeToResult,.._symbol_iterator 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 { ArrayObservable } from '../observable/ArrayObservable';
  10. import { isArray } from '../util/isArray';
  11. import { Subscriber } from '../Subscriber';
  12. import { OuterSubscriber } from '../OuterSubscriber';
  13. import { subscribeToResult } from '../util/subscribeToResult';
  14. import { iterator as Symbol_iterator } from '../symbol/iterator';
  15. /* tslint:enable:max-line-length */
  16. /**
  17. * @param observables
  18. * @return {Observable<R>}
  19. * @method zip
  20. * @owner Observable
  21. */
  22. export function zip() {
  23. var observables = [];
  24. for (var _i = 0; _i < arguments.length; _i++) {
  25. observables[_i - 0] = arguments[_i];
  26. }
  27. return function zipOperatorFunction(source) {
  28. return source.lift.call(zipStatic.apply(void 0, [source].concat(observables)));
  29. };
  30. }
  31. /* tslint:enable:max-line-length */
  32. /**
  33. * Combines multiple Observables to create an Observable whose values are calculated from the values, in order, of each
  34. * of its input Observables.
  35. *
  36. * If the latest parameter is a function, this function is used to compute the created value from the input values.
  37. * Otherwise, an array of the input values is returned.
  38. *
  39. * @example <caption>Combine age and name from different sources</caption>
  40. *
  41. * let age$ = Observable.of<number>(27, 25, 29);
  42. * let name$ = Observable.of<string>('Foo', 'Bar', 'Beer');
  43. * let isDev$ = Observable.of<boolean>(true, true, false);
  44. *
  45. * Observable
  46. * .zip(age$,
  47. * name$,
  48. * isDev$,
  49. * (age: number, name: string, isDev: boolean) => ({ age, name, isDev }))
  50. * .subscribe(x => console.log(x));
  51. *
  52. * // outputs
  53. * // { age: 27, name: 'Foo', isDev: true }
  54. * // { age: 25, name: 'Bar', isDev: true }
  55. * // { age: 29, name: 'Beer', isDev: false }
  56. *
  57. * @param observables
  58. * @return {Observable<R>}
  59. * @static true
  60. * @name zip
  61. * @owner Observable
  62. */
  63. export function zipStatic() {
  64. var observables = [];
  65. for (var _i = 0; _i < arguments.length; _i++) {
  66. observables[_i - 0] = arguments[_i];
  67. }
  68. var project = observables[observables.length - 1];
  69. if (typeof project === 'function') {
  70. observables.pop();
  71. }
  72. return new ArrayObservable(observables).lift(new ZipOperator(project));
  73. }
  74. export var ZipOperator = /*@__PURE__*/ (/*@__PURE__*/ 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. /**
  84. * We need this JSDoc comment for affecting ESDoc.
  85. * @ignore
  86. * @extends {Ignored}
  87. */
  88. export var ZipSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  89. __extends(ZipSubscriber, _super);
  90. function ZipSubscriber(destination, project, values) {
  91. if (values === void 0) {
  92. values = Object.create(null);
  93. }
  94. _super.call(this, destination);
  95. this.iterators = [];
  96. this.active = 0;
  97. this.project = (typeof project === 'function') ? project : null;
  98. this.values = values;
  99. }
  100. ZipSubscriber.prototype._next = function (value) {
  101. var iterators = this.iterators;
  102. if (isArray(value)) {
  103. iterators.push(new StaticArrayIterator(value));
  104. }
  105. else if (typeof value[Symbol_iterator] === 'function') {
  106. iterators.push(new StaticIterator(value[Symbol_iterator]()));
  107. }
  108. else {
  109. iterators.push(new ZipBufferIterator(this.destination, this, value));
  110. }
  111. };
  112. ZipSubscriber.prototype._complete = function () {
  113. var iterators = this.iterators;
  114. var len = iterators.length;
  115. if (len === 0) {
  116. this.destination.complete();
  117. return;
  118. }
  119. this.active = len;
  120. for (var i = 0; i < len; i++) {
  121. var iterator = iterators[i];
  122. if (iterator.stillUnsubscribed) {
  123. this.add(iterator.subscribe(iterator, i));
  124. }
  125. else {
  126. this.active--; // not an observable
  127. }
  128. }
  129. };
  130. ZipSubscriber.prototype.notifyInactive = function () {
  131. this.active--;
  132. if (this.active === 0) {
  133. this.destination.complete();
  134. }
  135. };
  136. ZipSubscriber.prototype.checkIterators = function () {
  137. var iterators = this.iterators;
  138. var len = iterators.length;
  139. var destination = this.destination;
  140. // abort if not all of them have values
  141. for (var i = 0; i < len; i++) {
  142. var iterator = iterators[i];
  143. if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
  144. return;
  145. }
  146. }
  147. var shouldComplete = false;
  148. var args = [];
  149. for (var i = 0; i < len; i++) {
  150. var iterator = iterators[i];
  151. var result = iterator.next();
  152. // check to see if it's completed now that you've gotten
  153. // the next value.
  154. if (iterator.hasCompleted()) {
  155. shouldComplete = true;
  156. }
  157. if (result.done) {
  158. destination.complete();
  159. return;
  160. }
  161. args.push(result.value);
  162. }
  163. if (this.project) {
  164. this._tryProject(args);
  165. }
  166. else {
  167. destination.next(args);
  168. }
  169. if (shouldComplete) {
  170. destination.complete();
  171. }
  172. };
  173. ZipSubscriber.prototype._tryProject = function (args) {
  174. var result;
  175. try {
  176. result = this.project.apply(this, args);
  177. }
  178. catch (err) {
  179. this.destination.error(err);
  180. return;
  181. }
  182. this.destination.next(result);
  183. };
  184. return ZipSubscriber;
  185. }(Subscriber));
  186. var StaticIterator = /*@__PURE__*/ (/*@__PURE__*/ 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 = /*@__PURE__*/ (/*@__PURE__*/ 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[Symbol_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 = /*@__PURE__*/ (/*@__PURE__*/ 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[Symbol_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(this, this.observable, this, index);
  278. };
  279. return ZipBufferIterator;
  280. }(OuterSubscriber));
  281. //# sourceMappingURL=zip.js.map