race.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import { Observable } from '../Observable';
  2. import { innerFrom } from './innerFrom';
  3. import { argsOrArgArray } from '../util/argsOrArgArray';
  4. import { createOperatorSubscriber } from '../operators/OperatorSubscriber';
  5. export function race() {
  6. var sources = [];
  7. for (var _i = 0; _i < arguments.length; _i++) {
  8. sources[_i] = arguments[_i];
  9. }
  10. sources = argsOrArgArray(sources);
  11. return sources.length === 1 ? innerFrom(sources[0]) : new Observable(raceInit(sources));
  12. }
  13. export function raceInit(sources) {
  14. return function (subscriber) {
  15. var subscriptions = [];
  16. var _loop_1 = function (i) {
  17. subscriptions.push(innerFrom(sources[i]).subscribe(createOperatorSubscriber(subscriber, function (value) {
  18. if (subscriptions) {
  19. for (var s = 0; s < subscriptions.length; s++) {
  20. s !== i && subscriptions[s].unsubscribe();
  21. }
  22. subscriptions = null;
  23. }
  24. subscriber.next(value);
  25. })));
  26. };
  27. for (var i = 0; subscriptions && !subscriber.closed && i < sources.length; i++) {
  28. _loop_1(i);
  29. }
  30. };
  31. }
  32. //# sourceMappingURL=race.js.map