Front end of the Slack clone application.

distinctUntilChanged.js 4.0KB

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