race.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 isArray_1 = require('../util/isArray');
  8. var ArrayObservable_1 = require('../observable/ArrayObservable');
  9. var OuterSubscriber_1 = require('../OuterSubscriber');
  10. var subscribeToResult_1 = require('../util/subscribeToResult');
  11. function race() {
  12. var observables = [];
  13. for (var _i = 0; _i < arguments.length; _i++) {
  14. observables[_i - 0] = arguments[_i];
  15. }
  16. // if the only argument is an array, it was most likely called with
  17. // `race([obs1, obs2, ...])`
  18. if (observables.length === 1) {
  19. if (isArray_1.isArray(observables[0])) {
  20. observables = observables[0];
  21. }
  22. else {
  23. return observables[0];
  24. }
  25. }
  26. return new ArrayObservable_1.ArrayObservable(observables).lift(new RaceOperator());
  27. }
  28. exports.race = race;
  29. var RaceOperator = (function () {
  30. function RaceOperator() {
  31. }
  32. RaceOperator.prototype.call = function (subscriber, source) {
  33. return source.subscribe(new RaceSubscriber(subscriber));
  34. };
  35. return RaceOperator;
  36. }());
  37. exports.RaceOperator = RaceOperator;
  38. /**
  39. * We need this JSDoc comment for affecting ESDoc.
  40. * @ignore
  41. * @extends {Ignored}
  42. */
  43. var RaceSubscriber = (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_1.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_1.OuterSubscriber));
  88. exports.RaceSubscriber = RaceSubscriber;
  89. //# sourceMappingURL=race.js.map