race.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /** PURE_IMPORTS_START .._util_isArray,.._observable_ArrayObservable,.._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 { isArray } from '../util/isArray';
  10. import { ArrayObservable } from '../observable/ArrayObservable';
  11. import { OuterSubscriber } from '../OuterSubscriber';
  12. import { subscribeToResult } from '../util/subscribeToResult';
  13. export function race() {
  14. var observables = [];
  15. for (var _i = 0; _i < arguments.length; _i++) {
  16. observables[_i - 0] = arguments[_i];
  17. }
  18. // if the only argument is an array, it was most likely called with
  19. // `race([obs1, obs2, ...])`
  20. if (observables.length === 1) {
  21. if (isArray(observables[0])) {
  22. observables = observables[0];
  23. }
  24. else {
  25. return observables[0];
  26. }
  27. }
  28. return new ArrayObservable(observables).lift(new RaceOperator());
  29. }
  30. export var RaceOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  31. function RaceOperator() {
  32. }
  33. RaceOperator.prototype.call = function (subscriber, source) {
  34. return source.subscribe(new RaceSubscriber(subscriber));
  35. };
  36. return RaceOperator;
  37. }());
  38. /**
  39. * We need this JSDoc comment for affecting ESDoc.
  40. * @ignore
  41. * @extends {Ignored}
  42. */
  43. export var RaceSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  44. __extends(RaceSubscriber, _super);
  45. function RaceSubscriber(destination) {
  46. _super.call(this, destination);
  47. this.hasFirst = false;
  48. this.observables = [];
  49. this.subscriptions = [];
  50. }
  51. RaceSubscriber.prototype._next = function (observable) {
  52. this.observables.push(observable);
  53. };
  54. RaceSubscriber.prototype._complete = function () {
  55. var observables = this.observables;
  56. var len = observables.length;
  57. if (len === 0) {
  58. this.destination.complete();
  59. }
  60. else {
  61. for (var i = 0; i < len && !this.hasFirst; i++) {
  62. var observable = observables[i];
  63. var subscription = subscribeToResult(this, observable, observable, i);
  64. if (this.subscriptions) {
  65. this.subscriptions.push(subscription);
  66. }
  67. this.add(subscription);
  68. }
  69. this.observables = null;
  70. }
  71. };
  72. RaceSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  73. if (!this.hasFirst) {
  74. this.hasFirst = true;
  75. for (var i = 0; i < this.subscriptions.length; i++) {
  76. if (i !== outerIndex) {
  77. var subscription = this.subscriptions[i];
  78. subscription.unsubscribe();
  79. this.remove(subscription);
  80. }
  81. }
  82. this.subscriptions = null;
  83. }
  84. this.destination.next(innerValue);
  85. };
  86. return RaceSubscriber;
  87. }(OuterSubscriber));
  88. //# sourceMappingURL=race.js.map