distinct.js 2.4 KB

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