distinctUntilChanged.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /** PURE_IMPORTS_START tslib,_Subscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. export function distinctUntilChanged(compare, keySelector) {
  5. return function (source) { return source.lift(new DistinctUntilChangedOperator(compare, keySelector)); };
  6. }
  7. var DistinctUntilChangedOperator = /*@__PURE__*/ (function () {
  8. function DistinctUntilChangedOperator(compare, keySelector) {
  9. this.compare = compare;
  10. this.keySelector = keySelector;
  11. }
  12. DistinctUntilChangedOperator.prototype.call = function (subscriber, source) {
  13. return source.subscribe(new DistinctUntilChangedSubscriber(subscriber, this.compare, this.keySelector));
  14. };
  15. return DistinctUntilChangedOperator;
  16. }());
  17. var DistinctUntilChangedSubscriber = /*@__PURE__*/ (function (_super) {
  18. tslib_1.__extends(DistinctUntilChangedSubscriber, _super);
  19. function DistinctUntilChangedSubscriber(destination, compare, keySelector) {
  20. var _this = _super.call(this, destination) || this;
  21. _this.keySelector = keySelector;
  22. _this.hasKey = false;
  23. if (typeof compare === 'function') {
  24. _this.compare = compare;
  25. }
  26. return _this;
  27. }
  28. DistinctUntilChangedSubscriber.prototype.compare = function (x, y) {
  29. return x === y;
  30. };
  31. DistinctUntilChangedSubscriber.prototype._next = function (value) {
  32. var key;
  33. try {
  34. var keySelector = this.keySelector;
  35. key = keySelector ? keySelector(value) : value;
  36. }
  37. catch (err) {
  38. return this.destination.error(err);
  39. }
  40. var result = false;
  41. if (this.hasKey) {
  42. try {
  43. var compare = this.compare;
  44. result = compare(this.key, key);
  45. }
  46. catch (err) {
  47. return this.destination.error(err);
  48. }
  49. }
  50. else {
  51. this.hasKey = true;
  52. }
  53. if (!result) {
  54. this.key = key;
  55. this.destination.next(value);
  56. }
  57. };
  58. return DistinctUntilChangedSubscriber;
  59. }(Subscriber));
  60. //# sourceMappingURL=distinctUntilChanged.js.map