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

timeout.ts 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { async } from '../scheduler/async';
  2. import { isDate } from '../util/isDate';
  3. import { Operator } from '../Operator';
  4. import { Subscriber } from '../Subscriber';
  5. import { Observable } from '../Observable';
  6. import { TimeoutError } from '../util/TimeoutError';
  7. import { MonoTypeOperatorFunction, SchedulerAction, SchedulerLike, TeardownLogic } from '../types';
  8. import { timeoutWith } from './timeoutWith';
  9. import { throwError } from '../observable/throwError';
  10. /**
  11. *
  12. * Errors if Observable does not emit a value in given time span.
  13. *
  14. * <span class="informal">Timeouts on Observable that doesn't emit values fast enough.</span>
  15. *
  16. * ![](timeout.png)
  17. *
  18. * `timeout` operator accepts as an argument either a number or a Date.
  19. *
  20. * If number was provided, it returns an Observable that behaves like a source
  21. * Observable, unless there is a period of time where there is no value emitted.
  22. * So if you provide `100` as argument and first value comes after 50ms from
  23. * the moment of subscription, this value will be simply re-emitted by the resulting
  24. * Observable. If however after that 100ms passes without a second value being emitted,
  25. * stream will end with an error and source Observable will be unsubscribed.
  26. * These checks are performed throughout whole lifecycle of Observable - from the moment
  27. * it was subscribed to, until it completes or errors itself. Thus every value must be
  28. * emitted within specified period since previous value.
  29. *
  30. * If provided argument was Date, returned Observable behaves differently. It throws
  31. * if Observable did not complete before provided Date. This means that periods between
  32. * emission of particular values do not matter in this case. If Observable did not complete
  33. * before provided Date, source Observable will be unsubscribed. Other than that, resulting
  34. * stream behaves just as source Observable.
  35. *
  36. * `timeout` accepts also a Scheduler as a second parameter. It is used to schedule moment (or moments)
  37. * when returned Observable will check if source stream emitted value or completed.
  38. *
  39. * ## Examples
  40. * Check if ticks are emitted within certain timespan
  41. * ```ts
  42. * import { interval } from 'rxjs';
  43. * import { timeout } from 'rxjs/operators';
  44. *
  45. * const seconds = interval(1000);
  46. *
  47. * seconds.pipe(timeout(1100)) // Let's use bigger timespan to be safe,
  48. * // since `interval` might fire a bit later then scheduled.
  49. * .subscribe(
  50. * value => console.log(value), // Will emit numbers just as regular `interval` would.
  51. * err => console.log(err), // Will never be called.
  52. * );
  53. *
  54. * seconds.pipe(timeout(900))
  55. * .subscribe(
  56. * value => console.log(value), // Will never be called.
  57. * err => console.log(err), // Will emit error before even first value is emitted,
  58. * // since it did not arrive within 900ms period.
  59. * );
  60. * ```
  61. *
  62. * Use Date to check if Observable completed
  63. * ```ts
  64. * import { interval } from 'rxjs';
  65. * import { timeout } from 'rxjs/operators';
  66. *
  67. * const seconds = interval(1000);
  68. *
  69. * seconds.pipe(
  70. * timeout(new Date("December 17, 2020 03:24:00")),
  71. * )
  72. * .subscribe(
  73. * value => console.log(value), // Will emit values as regular `interval` would
  74. * // until December 17, 2020 at 03:24:00.
  75. * err => console.log(err) // On December 17, 2020 at 03:24:00 it will emit an error,
  76. * // since Observable did not complete by then.
  77. * );
  78. * ```
  79. * @see {@link timeoutWith}
  80. *
  81. * @param {number|Date} due Number specifying period within which Observable must emit values
  82. * or Date specifying before when Observable should complete
  83. * @param {SchedulerLike} [scheduler] Scheduler controlling when timeout checks occur.
  84. * @return {Observable<T>} Observable that mirrors behaviour of source, unless timeout checks fail.
  85. * @method timeout
  86. * @owner Observable
  87. */
  88. export function timeout<T>(due: number | Date,
  89. scheduler: SchedulerLike = async): MonoTypeOperatorFunction<T> {
  90. return timeoutWith(due, throwError(new TimeoutError()), scheduler);
  91. }