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

pairwise.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { OperatorFunction } from '../types';
  5. /**
  6. * Groups pairs of consecutive emissions together and emits them as an array of
  7. * two values.
  8. *
  9. * <span class="informal">Puts the current value and previous value together as
  10. * an array, and emits that.</span>
  11. *
  12. * ![](pairwise.png)
  13. *
  14. * The Nth emission from the source Observable will cause the output Observable
  15. * to emit an array [(N-1)th, Nth] of the previous and the current value, as a
  16. * pair. For this reason, `pairwise` emits on the second and subsequent
  17. * emissions from the source Observable, but not on the first emission, because
  18. * there is no previous value in that case.
  19. *
  20. * ## Example
  21. * On every click (starting from the second), emit the relative distance to the previous click
  22. * ```ts
  23. * import { fromEvent } from 'rxjs';
  24. * import { pairwise, map } from 'rxjs/operators';
  25. *
  26. * const clicks = fromEvent(document, 'click');
  27. * const pairs = clicks.pipe(pairwise());
  28. * const distance = pairs.pipe(
  29. * map(pair => {
  30. * const x0 = pair[0].clientX;
  31. * const y0 = pair[0].clientY;
  32. * const x1 = pair[1].clientX;
  33. * const y1 = pair[1].clientY;
  34. * return Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
  35. * }),
  36. * );
  37. * distance.subscribe(x => console.log(x));
  38. * ```
  39. *
  40. * @see {@link buffer}
  41. * @see {@link bufferCount}
  42. *
  43. * @return {Observable<Array<T>>} An Observable of pairs (as arrays) of
  44. * consecutive values from the source Observable.
  45. * @method pairwise
  46. * @owner Observable
  47. */
  48. export function pairwise<T>(): OperatorFunction<T, [T, T]> {
  49. return (source: Observable<T>) => source.lift(new PairwiseOperator());
  50. }
  51. class PairwiseOperator<T> implements Operator<T, [T, T]> {
  52. call(subscriber: Subscriber<[T, T]>, source: any): any {
  53. return source.subscribe(new PairwiseSubscriber(subscriber));
  54. }
  55. }
  56. /**
  57. * We need this JSDoc comment for affecting ESDoc.
  58. * @ignore
  59. * @extends {Ignored}
  60. */
  61. class PairwiseSubscriber<T> extends Subscriber<T> {
  62. private prev: T;
  63. private hasPrev: boolean = false;
  64. constructor(destination: Subscriber<[T, T]>) {
  65. super(destination);
  66. }
  67. _next(value: T): void {
  68. let pair: [T, T] | undefined;
  69. if (this.hasPrev) {
  70. pair = [this.prev, value];
  71. } else {
  72. this.hasPrev = true;
  73. }
  74. this.prev = value;
  75. if (pair) {
  76. this.destination.next(pair);
  77. }
  78. }
  79. }