Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

distinctUntilChanged.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Subscriber } from '../Subscriber';
  2. export function distinctUntilChanged(compare, keySelector) {
  3. return (source) => source.lift(new DistinctUntilChangedOperator(compare, keySelector));
  4. }
  5. class DistinctUntilChangedOperator {
  6. constructor(compare, keySelector) {
  7. this.compare = compare;
  8. this.keySelector = keySelector;
  9. }
  10. call(subscriber, source) {
  11. return source.subscribe(new DistinctUntilChangedSubscriber(subscriber, this.compare, this.keySelector));
  12. }
  13. }
  14. class DistinctUntilChangedSubscriber extends Subscriber {
  15. constructor(destination, compare, keySelector) {
  16. super(destination);
  17. this.keySelector = keySelector;
  18. this.hasKey = false;
  19. if (typeof compare === 'function') {
  20. this.compare = compare;
  21. }
  22. }
  23. compare(x, y) {
  24. return x === y;
  25. }
  26. _next(value) {
  27. let key;
  28. try {
  29. const { keySelector } = this;
  30. key = keySelector ? keySelector(value) : value;
  31. }
  32. catch (err) {
  33. return this.destination.error(err);
  34. }
  35. let result = false;
  36. if (this.hasKey) {
  37. try {
  38. const { compare } = this;
  39. result = compare(this.key, key);
  40. }
  41. catch (err) {
  42. return this.destination.error(err);
  43. }
  44. }
  45. else {
  46. this.hasKey = true;
  47. }
  48. if (!result) {
  49. this.key = key;
  50. this.destination.next(value);
  51. }
  52. }
  53. }
  54. //# sourceMappingURL=distinctUntilChanged.js.map