distinctUntilChanged.js 4.0 KB

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