distinct.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /** PURE_IMPORTS_START tslib,_innerSubscribe PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  4. export function distinct(keySelector, flushes) {
  5. return function (source) { return source.lift(new DistinctOperator(keySelector, flushes)); };
  6. }
  7. var DistinctOperator = /*@__PURE__*/ (function () {
  8. function DistinctOperator(keySelector, flushes) {
  9. this.keySelector = keySelector;
  10. this.flushes = flushes;
  11. }
  12. DistinctOperator.prototype.call = function (subscriber, source) {
  13. return source.subscribe(new DistinctSubscriber(subscriber, this.keySelector, this.flushes));
  14. };
  15. return DistinctOperator;
  16. }());
  17. var DistinctSubscriber = /*@__PURE__*/ (function (_super) {
  18. tslib_1.__extends(DistinctSubscriber, _super);
  19. function DistinctSubscriber(destination, keySelector, flushes) {
  20. var _this = _super.call(this, destination) || this;
  21. _this.keySelector = keySelector;
  22. _this.values = new Set();
  23. if (flushes) {
  24. _this.add(innerSubscribe(flushes, new SimpleInnerSubscriber(_this)));
  25. }
  26. return _this;
  27. }
  28. DistinctSubscriber.prototype.notifyNext = function () {
  29. this.values.clear();
  30. };
  31. DistinctSubscriber.prototype.notifyError = function (error) {
  32. this._error(error);
  33. };
  34. DistinctSubscriber.prototype._next = function (value) {
  35. if (this.keySelector) {
  36. this._useKeySelector(value);
  37. }
  38. else {
  39. this._finalizeNext(value, value);
  40. }
  41. };
  42. DistinctSubscriber.prototype._useKeySelector = function (value) {
  43. var key;
  44. var destination = this.destination;
  45. try {
  46. key = this.keySelector(value);
  47. }
  48. catch (err) {
  49. destination.error(err);
  50. return;
  51. }
  52. this._finalizeNext(key, value);
  53. };
  54. DistinctSubscriber.prototype._finalizeNext = function (key, value) {
  55. var values = this.values;
  56. if (!values.has(key)) {
  57. values.add(key);
  58. this.destination.next(value);
  59. }
  60. };
  61. return DistinctSubscriber;
  62. }(SimpleOuterSubscriber));
  63. export { DistinctSubscriber };
  64. //# sourceMappingURL=distinct.js.map