race.js 1.0 KB

12345678910111213141516171819202122232425
  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(...sources) {
  6. sources = argsOrArgArray(sources);
  7. return sources.length === 1 ? innerFrom(sources[0]) : new Observable(raceInit(sources));
  8. }
  9. export function raceInit(sources) {
  10. return (subscriber) => {
  11. let subscriptions = [];
  12. for (let i = 0; subscriptions && !subscriber.closed && i < sources.length; i++) {
  13. subscriptions.push(innerFrom(sources[i]).subscribe(createOperatorSubscriber(subscriber, (value) => {
  14. if (subscriptions) {
  15. for (let s = 0; s < subscriptions.length; s++) {
  16. s !== i && subscriptions[s].unsubscribe();
  17. }
  18. subscriptions = null;
  19. }
  20. subscriber.next(value);
  21. })));
  22. }
  23. };
  24. }
  25. //# sourceMappingURL=race.js.map