race.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /** PURE_IMPORTS_START tslib,_util_isArray,_fromArray,_OuterSubscriber,_util_subscribeToResult PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { isArray } from '../util/isArray';
  4. import { fromArray } from './fromArray';
  5. import { OuterSubscriber } from '../OuterSubscriber';
  6. import { subscribeToResult } from '../util/subscribeToResult';
  7. export function race() {
  8. var observables = [];
  9. for (var _i = 0; _i < arguments.length; _i++) {
  10. observables[_i] = arguments[_i];
  11. }
  12. if (observables.length === 1) {
  13. if (isArray(observables[0])) {
  14. observables = observables[0];
  15. }
  16. else {
  17. return observables[0];
  18. }
  19. }
  20. return fromArray(observables, undefined).lift(new RaceOperator());
  21. }
  22. var RaceOperator = /*@__PURE__*/ (function () {
  23. function RaceOperator() {
  24. }
  25. RaceOperator.prototype.call = function (subscriber, source) {
  26. return source.subscribe(new RaceSubscriber(subscriber));
  27. };
  28. return RaceOperator;
  29. }());
  30. export { RaceOperator };
  31. var RaceSubscriber = /*@__PURE__*/ (function (_super) {
  32. tslib_1.__extends(RaceSubscriber, _super);
  33. function RaceSubscriber(destination) {
  34. var _this = _super.call(this, destination) || this;
  35. _this.hasFirst = false;
  36. _this.observables = [];
  37. _this.subscriptions = [];
  38. return _this;
  39. }
  40. RaceSubscriber.prototype._next = function (observable) {
  41. this.observables.push(observable);
  42. };
  43. RaceSubscriber.prototype._complete = function () {
  44. var observables = this.observables;
  45. var len = observables.length;
  46. if (len === 0) {
  47. this.destination.complete();
  48. }
  49. else {
  50. for (var i = 0; i < len && !this.hasFirst; i++) {
  51. var observable = observables[i];
  52. var subscription = subscribeToResult(this, observable, undefined, i);
  53. if (this.subscriptions) {
  54. this.subscriptions.push(subscription);
  55. }
  56. this.add(subscription);
  57. }
  58. this.observables = null;
  59. }
  60. };
  61. RaceSubscriber.prototype.notifyNext = function (_outerValue, innerValue, outerIndex) {
  62. if (!this.hasFirst) {
  63. this.hasFirst = true;
  64. for (var i = 0; i < this.subscriptions.length; i++) {
  65. if (i !== outerIndex) {
  66. var subscription = this.subscriptions[i];
  67. subscription.unsubscribe();
  68. this.remove(subscription);
  69. }
  70. }
  71. this.subscriptions = null;
  72. }
  73. this.destination.next(innerValue);
  74. };
  75. return RaceSubscriber;
  76. }(OuterSubscriber));
  77. export { RaceSubscriber };
  78. //# sourceMappingURL=race.js.map