distinct.d.ts 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Observable } from '../Observable';
  2. /**
  3. * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items.
  4. *
  5. * If a keySelector function is provided, then it will project each value from the source observable into a new value that it will
  6. * check for equality with previously projected values. If a keySelector function is not provided, it will use each value from the
  7. * source observable directly with an equality check against previous values.
  8. *
  9. * In JavaScript runtimes that support `Set`, this operator will use a `Set` to improve performance of the distinct value checking.
  10. *
  11. * In other runtimes, this operator will use a minimal implementation of `Set` that relies on an `Array` and `indexOf` under the
  12. * hood, so performance will degrade as more values are checked for distinction. Even in newer browsers, a long-running `distinct`
  13. * use might result in memory leaks. To help alleviate this in some scenarios, an optional `flushes` parameter is also provided so
  14. * that the internal `Set` can be "flushed", basically clearing it of values.
  15. *
  16. * @example <caption>A simple example with numbers</caption>
  17. * Observable.of(1, 1, 2, 2, 2, 1, 2, 3, 4, 3, 2, 1)
  18. * .distinct()
  19. * .subscribe(x => console.log(x)); // 1, 2, 3, 4
  20. *
  21. * @example <caption>An example using a keySelector function</caption>
  22. * interface Person {
  23. * age: number,
  24. * name: string
  25. * }
  26. *
  27. * Observable.of<Person>(
  28. * { age: 4, name: 'Foo'},
  29. * { age: 7, name: 'Bar'},
  30. * { age: 5, name: 'Foo'})
  31. * .distinct((p: Person) => p.name)
  32. * .subscribe(x => console.log(x));
  33. *
  34. * // displays:
  35. * // { age: 4, name: 'Foo' }
  36. * // { age: 7, name: 'Bar' }
  37. *
  38. * @see {@link distinctUntilChanged}
  39. * @see {@link distinctUntilKeyChanged}
  40. *
  41. * @param {function} [keySelector] Optional function to select which value you want to check as distinct.
  42. * @param {Observable} [flushes] Optional Observable for flushing the internal HashSet of the operator.
  43. * @return {Observable} An Observable that emits items from the source Observable with distinct values.
  44. * @method distinct
  45. * @owner Observable
  46. */
  47. export declare function distinct<T, K>(this: Observable<T>, keySelector?: (value: T) => K, flushes?: Observable<any>): Observable<T>;