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

distinct.ts 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Observable } from '../Observable';
  2. import { Operator } from '../Operator';
  3. import { Subscriber } from '../Subscriber';
  4. import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
  5. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  6. /**
  7. * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items.
  8. *
  9. * If a keySelector function is provided, then it will project each value from the source observable into a new value that it will
  10. * check for equality with previously projected values. If a keySelector function is not provided, it will use each value from the
  11. * source observable directly with an equality check against previous values.
  12. *
  13. * In JavaScript runtimes that support `Set`, this operator will use a `Set` to improve performance of the distinct value checking.
  14. *
  15. * In other runtimes, this operator will use a minimal implementation of `Set` that relies on an `Array` and `indexOf` under the
  16. * hood, so performance will degrade as more values are checked for distinction. Even in newer browsers, a long-running `distinct`
  17. * use might result in memory leaks. To help alleviate this in some scenarios, an optional `flushes` parameter is also provided so
  18. * that the internal `Set` can be "flushed", basically clearing it of values.
  19. *
  20. * ## Examples
  21. * A simple example with numbers
  22. * ```ts
  23. * import { of } from 'rxjs';
  24. * import { distinct } from 'rxjs/operators';
  25. *
  26. * of(1, 1, 2, 2, 2, 1, 2, 3, 4, 3, 2, 1).pipe(
  27. * distinct(),
  28. * )
  29. * .subscribe(x => console.log(x)); // 1, 2, 3, 4
  30. * ```
  31. *
  32. * An example using a keySelector function
  33. * ```typescript
  34. * import { of } from 'rxjs';
  35. * import { distinct } from 'rxjs/operators';
  36. *
  37. * interface Person {
  38. * age: number,
  39. * name: string
  40. * }
  41. *
  42. * of<Person>(
  43. * { age: 4, name: 'Foo'},
  44. * { age: 7, name: 'Bar'},
  45. * { age: 5, name: 'Foo'},
  46. * ).pipe(
  47. * distinct((p: Person) => p.name),
  48. * )
  49. * .subscribe(x => console.log(x));
  50. *
  51. * // displays:
  52. * // { age: 4, name: 'Foo' }
  53. * // { age: 7, name: 'Bar' }
  54. * ```
  55. * @see {@link distinctUntilChanged}
  56. * @see {@link distinctUntilKeyChanged}
  57. *
  58. * @param {function} [keySelector] Optional function to select which value you want to check as distinct.
  59. * @param {Observable} [flushes] Optional Observable for flushing the internal HashSet of the operator.
  60. * @return {Observable} An Observable that emits items from the source Observable with distinct values.
  61. * @method distinct
  62. * @owner Observable
  63. */
  64. export function distinct<T, K>(keySelector?: (value: T) => K,
  65. flushes?: Observable<any>): MonoTypeOperatorFunction<T> {
  66. return (source: Observable<T>) => source.lift(new DistinctOperator(keySelector, flushes));
  67. }
  68. class DistinctOperator<T, K> implements Operator<T, T> {
  69. constructor(private keySelector?: (value: T) => K, private flushes?: Observable<any>) {
  70. }
  71. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  72. return source.subscribe(new DistinctSubscriber(subscriber, this.keySelector, this.flushes));
  73. }
  74. }
  75. /**
  76. * We need this JSDoc comment for affecting ESDoc.
  77. * @ignore
  78. * @extends {Ignored}
  79. */
  80. export class DistinctSubscriber<T, K> extends SimpleOuterSubscriber<T, T> {
  81. private values = new Set<K>();
  82. constructor(destination: Subscriber<T>, private keySelector?: (value: T) => K, flushes?: Observable<any>) {
  83. super(destination);
  84. if (flushes) {
  85. this.add(innerSubscribe(flushes, new SimpleInnerSubscriber(this)));
  86. }
  87. }
  88. notifyNext(): void {
  89. this.values.clear();
  90. }
  91. notifyError(error: any): void {
  92. this._error(error);
  93. }
  94. protected _next(value: T): void {
  95. if (this.keySelector) {
  96. this._useKeySelector(value);
  97. } else {
  98. this._finalizeNext(value, value);
  99. }
  100. }
  101. private _useKeySelector(value: T): void {
  102. let key: K;
  103. const { destination } = this;
  104. try {
  105. key = this.keySelector!(value);
  106. } catch (err) {
  107. destination.error!(err);
  108. return;
  109. }
  110. this._finalizeNext(key, value);
  111. }
  112. private _finalizeNext(key: K|T, value: T) {
  113. const { values } = this;
  114. if (!values.has(<K>key)) {
  115. values.add(<K>key);
  116. this.destination.next!(value);
  117. }
  118. }
  119. }