Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

subscribeOn.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { Observable } from '../Observable';
  4. import { SubscribeOnObservable } from '../observable/SubscribeOnObservable';
  5. import { MonoTypeOperatorFunction, SchedulerLike, TeardownLogic } from '../types';
  6. /**
  7. * Asynchronously subscribes Observers to this Observable on the specified {@link SchedulerLike}.
  8. *
  9. * With `subscribeOn` you can decide what type of scheduler a specific Observable will be using when it is subscribed to.
  10. *
  11. * Schedulers control the speed and order of emissions to observers from an Observable stream.
  12. *
  13. * ![](subscribeOn.png)
  14. *
  15. * ## Example
  16. * Given the following code:
  17. * ```javascript
  18. * import { of, merge } from 'rxjs';
  19. *
  20. * const a = of(1, 2, 3, 4);
  21. * const b = of(5, 6, 7, 8, 9);
  22. * merge(a, b).subscribe(console.log);
  23. * ```
  24. *
  25. * Both Observable `a` and `b` will emit their values directly and synchronously once they are subscribed to.
  26. * This will result in the output of `1 2 3 4 5 6 7 8 9`.
  27. *
  28. * But if we instead us the `subscribeOn` operator declaring that we want to use the {@link asyncScheduler} for values emited by Observable `a`:
  29. * ```javascript
  30. * import { of, merge, asyncScheduler } from 'rxjs';
  31. * import { subscribeOn } from 'rxjs/operators';
  32. *
  33. * const a = of(1, 2, 3, 4).pipe(subscribeOn(asyncScheduler));
  34. * const b = of(5, 6, 7, 8, 9);
  35. * merge(a, b).subscribe(console.log);
  36. * ```
  37. *
  38. * The output will instead be `5 6 7 8 9 1 2 3 4`.
  39. * The reason for this is that Observable `b` emits its values directly and synchronously like before
  40. * but the emissions from `a` are scheduled on the event loop because we are now using the {@link asyncScheduler} for that specific Observable.
  41. *
  42. * @param {SchedulerLike} scheduler - The {@link SchedulerLike} to perform subscription actions on.
  43. * @return {Observable<T>} The source Observable modified so that its subscriptions happen on the specified {@link SchedulerLike}.
  44. .
  45. * @method subscribeOn
  46. * @owner Observable
  47. */
  48. export function subscribeOn<T>(scheduler: SchedulerLike, delay: number = 0): MonoTypeOperatorFunction<T> {
  49. return function subscribeOnOperatorFunction(source: Observable<T>): Observable<T> {
  50. return source.lift(new SubscribeOnOperator<T>(scheduler, delay));
  51. };
  52. }
  53. class SubscribeOnOperator<T> implements Operator<T, T> {
  54. constructor(private scheduler: SchedulerLike,
  55. private delay: number) {
  56. }
  57. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  58. return new SubscribeOnObservable<T>(
  59. source, this.delay, this.scheduler
  60. ).subscribe(subscriber);
  61. }
  62. }