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

range.ts 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { SchedulerAction, SchedulerLike } from '../types';
  2. import { Observable } from '../Observable';
  3. /**
  4. * Creates an Observable that emits a sequence of numbers within a specified
  5. * range.
  6. *
  7. * <span class="informal">Emits a sequence of numbers in a range.</span>
  8. *
  9. * ![](range.png)
  10. *
  11. * `range` operator emits a range of sequential integers, in order, where you
  12. * select the `start` of the range and its `length`. By default, uses no
  13. * {@link SchedulerLike} and just delivers the notifications synchronously, but may use
  14. * an optional {@link SchedulerLike} to regulate those deliveries.
  15. *
  16. * ## Example
  17. * Emits the numbers 1 to 10</caption>
  18. * ```ts
  19. * import { range } from 'rxjs';
  20. *
  21. * const numbers = range(1, 10);
  22. * numbers.subscribe(x => console.log(x));
  23. * ```
  24. * @see {@link timer}
  25. * @see {@link index/interval}
  26. *
  27. * @param {number} [start=0] The value of the first integer in the sequence.
  28. * @param {number} count The number of sequential integers to generate.
  29. * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} to use for scheduling
  30. * the emissions of the notifications.
  31. * @return {Observable} An Observable of numbers that emits a finite range of
  32. * sequential integers.
  33. * @static true
  34. * @name range
  35. * @owner Observable
  36. */
  37. export function range(start: number = 0,
  38. count?: number,
  39. scheduler?: SchedulerLike): Observable<number> {
  40. return new Observable<number>(subscriber => {
  41. if (count === undefined) {
  42. count = start;
  43. start = 0;
  44. }
  45. let index = 0;
  46. let current = start;
  47. if (scheduler) {
  48. return scheduler.schedule(dispatch, 0, {
  49. index, count, start, subscriber
  50. });
  51. } else {
  52. do {
  53. if (index++ >= count) {
  54. subscriber.complete();
  55. break;
  56. }
  57. subscriber.next(current++);
  58. if (subscriber.closed) {
  59. break;
  60. }
  61. } while (true);
  62. }
  63. return undefined;
  64. });
  65. }
  66. /** @internal */
  67. export function dispatch(this: SchedulerAction<any>, state: any) {
  68. const { start, index, count, subscriber } = state;
  69. if (index >= count) {
  70. subscriber.complete();
  71. return;
  72. }
  73. subscriber.next(start);
  74. if (subscriber.closed) {
  75. return;
  76. }
  77. state.index = index + 1;
  78. state.start = start + 1;
  79. this.schedule(state);
  80. }