distinctUntilChanged.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Subscriber } from '../Subscriber';
  2. import { tryCatch } from '../util/tryCatch';
  3. import { errorObject } from '../util/errorObject';
  4. /* tslint:enable:max-line-length */
  5. /**
  6. * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item.
  7. *
  8. * If a comparator function is provided, then it will be called for each item to test for whether or not that value should be emitted.
  9. *
  10. * If a comparator function is not provided, an equality check is used by default.
  11. *
  12. * @example <caption>A simple example with numbers</caption>
  13. * Observable.of(1, 1, 2, 2, 2, 1, 1, 2, 3, 3, 4)
  14. * .distinctUntilChanged()
  15. * .subscribe(x => console.log(x)); // 1, 2, 1, 2, 3, 4
  16. *
  17. * @example <caption>An example using a compare function</caption>
  18. * interface Person {
  19. * age: number,
  20. * name: string
  21. * }
  22. *
  23. * Observable.of<Person>(
  24. * { age: 4, name: 'Foo'},
  25. * { age: 7, name: 'Bar'},
  26. * { age: 5, name: 'Foo'})
  27. * { age: 6, name: 'Foo'})
  28. * .distinctUntilChanged((p: Person, q: Person) => p.name === q.name)
  29. * .subscribe(x => console.log(x));
  30. *
  31. * // displays:
  32. * // { age: 4, name: 'Foo' }
  33. * // { age: 7, name: 'Bar' }
  34. * // { age: 5, name: 'Foo' }
  35. *
  36. * @see {@link distinct}
  37. * @see {@link distinctUntilKeyChanged}
  38. *
  39. * @param {function} [compare] Optional comparison function called to test if an item is distinct from the previous item in the source.
  40. * @return {Observable} An Observable that emits items from the source Observable with distinct values.
  41. * @method distinctUntilChanged
  42. * @owner Observable
  43. */
  44. export function distinctUntilChanged(compare, keySelector) {
  45. return (source) => source.lift(new DistinctUntilChangedOperator(compare, keySelector));
  46. }
  47. class DistinctUntilChangedOperator {
  48. constructor(compare, keySelector) {
  49. this.compare = compare;
  50. this.keySelector = keySelector;
  51. }
  52. call(subscriber, source) {
  53. return source.subscribe(new DistinctUntilChangedSubscriber(subscriber, this.compare, this.keySelector));
  54. }
  55. }
  56. /**
  57. * We need this JSDoc comment for affecting ESDoc.
  58. * @ignore
  59. * @extends {Ignored}
  60. */
  61. class DistinctUntilChangedSubscriber extends Subscriber {
  62. constructor(destination, compare, keySelector) {
  63. super(destination);
  64. this.keySelector = keySelector;
  65. this.hasKey = false;
  66. if (typeof compare === 'function') {
  67. this.compare = compare;
  68. }
  69. }
  70. compare(x, y) {
  71. return x === y;
  72. }
  73. _next(value) {
  74. const keySelector = this.keySelector;
  75. let key = value;
  76. if (keySelector) {
  77. key = tryCatch(this.keySelector)(value);
  78. if (key === errorObject) {
  79. return this.destination.error(errorObject.e);
  80. }
  81. }
  82. let result = false;
  83. if (this.hasKey) {
  84. result = tryCatch(this.compare)(this.key, key);
  85. if (result === errorObject) {
  86. return this.destination.error(errorObject.e);
  87. }
  88. }
  89. else {
  90. this.hasKey = true;
  91. }
  92. if (Boolean(result) === false) {
  93. this.key = key;
  94. this.destination.next(value);
  95. }
  96. }
  97. }
  98. //# sourceMappingURL=distinctUntilChanged.js.map