Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dematerialize.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { Notification } from '../Notification';
  5. import { OperatorFunction } from '../types';
  6. /**
  7. * Converts an Observable of {@link Notification} objects into the emissions
  8. * that they represent.
  9. *
  10. * <span class="informal">Unwraps {@link Notification} objects as actual `next`,
  11. * `error` and `complete` emissions. The opposite of {@link materialize}.</span>
  12. *
  13. * ![](dematerialize.png)
  14. *
  15. * `dematerialize` is assumed to operate an Observable that only emits
  16. * {@link Notification} objects as `next` emissions, and does not emit any
  17. * `error`. Such Observable is the output of a `materialize` operation. Those
  18. * notifications are then unwrapped using the metadata they contain, and emitted
  19. * as `next`, `error`, and `complete` on the output Observable.
  20. *
  21. * Use this operator in conjunction with {@link materialize}.
  22. *
  23. * ## Example
  24. * Convert an Observable of Notifications to an actual Observable
  25. * ```ts
  26. * import { of, Notification } from 'rxjs';
  27. * import { dematerialize } from 'rxjs/operators';
  28. *
  29. * const notifA = new Notification('N', 'A');
  30. * const notifB = new Notification('N', 'B');
  31. * const notifE = new Notification('E', undefined,
  32. * new TypeError('x.toUpperCase is not a function')
  33. * );
  34. * const materialized = of(notifA, notifB, notifE);
  35. * const upperCase = materialized.pipe(dematerialize());
  36. * upperCase.subscribe(x => console.log(x), e => console.error(e));
  37. *
  38. * // Results in:
  39. * // A
  40. * // B
  41. * // TypeError: x.toUpperCase is not a function
  42. * ```
  43. *
  44. * @see {@link Notification}
  45. * @see {@link materialize}
  46. *
  47. * @return {Observable} An Observable that emits items and notifications
  48. * embedded in Notification objects emitted by the source Observable.
  49. * @method dematerialize
  50. * @owner Observable
  51. */
  52. export function dematerialize<T>(): OperatorFunction<Notification<T>, T> {
  53. return function dematerializeOperatorFunction(source: Observable<Notification<T>>) {
  54. return source.lift(new DeMaterializeOperator());
  55. };
  56. }
  57. class DeMaterializeOperator<T extends Notification<any>, R> implements Operator<T, R> {
  58. call(subscriber: Subscriber<any>, source: any): any {
  59. return source.subscribe(new DeMaterializeSubscriber(subscriber));
  60. }
  61. }
  62. /**
  63. * We need this JSDoc comment for affecting ESDoc.
  64. * @ignore
  65. * @extends {Ignored}
  66. */
  67. class DeMaterializeSubscriber<T extends Notification<any>> extends Subscriber<T> {
  68. constructor(destination: Subscriber<any>) {
  69. super(destination);
  70. }
  71. protected _next(value: T) {
  72. value.observe(this.destination);
  73. }
  74. }