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

publishLast.ts 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Observable } from '../Observable';
  2. import { AsyncSubject } from '../AsyncSubject';
  3. import { multicast } from './multicast';
  4. import { ConnectableObservable } from '../observable/ConnectableObservable';
  5. import { UnaryFunction } from '../types';
  6. /**
  7. * Returns a connectable observable sequence that shares a single subscription to the
  8. * underlying sequence containing only the last notification.
  9. *
  10. * ![](publishLast.png)
  11. *
  12. * Similar to {@link publish}, but it waits until the source observable completes and stores
  13. * the last emitted value.
  14. * Similarly to {@link publishReplay} and {@link publishBehavior}, this keeps storing the last
  15. * value even if it has no more subscribers. If subsequent subscriptions happen, they will
  16. * immediately get that last stored value and complete.
  17. *
  18. * ## Example
  19. *
  20. * ```ts
  21. * import { interval } from 'rxjs';
  22. * import { publishLast, tap, take } from 'rxjs/operators';
  23. *
  24. * const connectable =
  25. * interval(1000)
  26. * .pipe(
  27. * tap(x => console.log("side effect", x)),
  28. * take(3),
  29. * publishLast());
  30. *
  31. * connectable.subscribe(
  32. * x => console.log( "Sub. A", x),
  33. * err => console.log("Sub. A Error", err),
  34. * () => console.log( "Sub. A Complete"));
  35. *
  36. * connectable.subscribe(
  37. * x => console.log( "Sub. B", x),
  38. * err => console.log("Sub. B Error", err),
  39. * () => console.log( "Sub. B Complete"));
  40. *
  41. * connectable.connect();
  42. *
  43. * // Results:
  44. * // "side effect 0"
  45. * // "side effect 1"
  46. * // "side effect 2"
  47. * // "Sub. A 2"
  48. * // "Sub. B 2"
  49. * // "Sub. A Complete"
  50. * // "Sub. B Complete"
  51. * ```
  52. *
  53. * @see {@link ConnectableObservable}
  54. * @see {@link publish}
  55. * @see {@link publishReplay}
  56. * @see {@link publishBehavior}
  57. *
  58. * @return {ConnectableObservable} An observable sequence that contains the elements of a
  59. * sequence produced by multicasting the source sequence.
  60. * @method publishLast
  61. * @owner Observable
  62. */
  63. export function publishLast<T>(): UnaryFunction<Observable<T>, ConnectableObservable<T>> {
  64. return (source: Observable<T>) => multicast(new AsyncSubject<T>())(source);
  65. }