race.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var isArray_1 = require("../util/isArray");
  17. var fromArray_1 = require("./fromArray");
  18. var OuterSubscriber_1 = require("../OuterSubscriber");
  19. var subscribeToResult_1 = require("../util/subscribeToResult");
  20. function race() {
  21. var observables = [];
  22. for (var _i = 0; _i < arguments.length; _i++) {
  23. observables[_i] = arguments[_i];
  24. }
  25. if (observables.length === 1) {
  26. if (isArray_1.isArray(observables[0])) {
  27. observables = observables[0];
  28. }
  29. else {
  30. return observables[0];
  31. }
  32. }
  33. return fromArray_1.fromArray(observables, undefined).lift(new RaceOperator());
  34. }
  35. exports.race = race;
  36. var RaceOperator = (function () {
  37. function RaceOperator() {
  38. }
  39. RaceOperator.prototype.call = function (subscriber, source) {
  40. return source.subscribe(new RaceSubscriber(subscriber));
  41. };
  42. return RaceOperator;
  43. }());
  44. exports.RaceOperator = RaceOperator;
  45. var RaceSubscriber = (function (_super) {
  46. __extends(RaceSubscriber, _super);
  47. function RaceSubscriber(destination) {
  48. var _this = _super.call(this, destination) || this;
  49. _this.hasFirst = false;
  50. _this.observables = [];
  51. _this.subscriptions = [];
  52. return _this;
  53. }
  54. RaceSubscriber.prototype._next = function (observable) {
  55. this.observables.push(observable);
  56. };
  57. RaceSubscriber.prototype._complete = function () {
  58. var observables = this.observables;
  59. var len = observables.length;
  60. if (len === 0) {
  61. this.destination.complete();
  62. }
  63. else {
  64. for (var i = 0; i < len && !this.hasFirst; i++) {
  65. var observable = observables[i];
  66. var subscription = subscribeToResult_1.subscribeToResult(this, observable, undefined, i);
  67. if (this.subscriptions) {
  68. this.subscriptions.push(subscription);
  69. }
  70. this.add(subscription);
  71. }
  72. this.observables = null;
  73. }
  74. };
  75. RaceSubscriber.prototype.notifyNext = function (_outerValue, innerValue, outerIndex) {
  76. if (!this.hasFirst) {
  77. this.hasFirst = true;
  78. for (var i = 0; i < this.subscriptions.length; i++) {
  79. if (i !== outerIndex) {
  80. var subscription = this.subscriptions[i];
  81. subscription.unsubscribe();
  82. this.remove(subscription);
  83. }
  84. }
  85. this.subscriptions = null;
  86. }
  87. this.destination.next(innerValue);
  88. };
  89. return RaceSubscriber;
  90. }(OuterSubscriber_1.OuterSubscriber));
  91. exports.RaceSubscriber = RaceSubscriber;
  92. //# sourceMappingURL=race.js.map