1 | import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
|
---|
2 | export function distinct(keySelector, flushes) {
|
---|
3 | return (source) => source.lift(new DistinctOperator(keySelector, flushes));
|
---|
4 | }
|
---|
5 | class DistinctOperator {
|
---|
6 | constructor(keySelector, flushes) {
|
---|
7 | this.keySelector = keySelector;
|
---|
8 | this.flushes = flushes;
|
---|
9 | }
|
---|
10 | call(subscriber, source) {
|
---|
11 | return source.subscribe(new DistinctSubscriber(subscriber, this.keySelector, this.flushes));
|
---|
12 | }
|
---|
13 | }
|
---|
14 | export class DistinctSubscriber extends SimpleOuterSubscriber {
|
---|
15 | constructor(destination, keySelector, flushes) {
|
---|
16 | super(destination);
|
---|
17 | this.keySelector = keySelector;
|
---|
18 | this.values = new Set();
|
---|
19 | if (flushes) {
|
---|
20 | this.add(innerSubscribe(flushes, new SimpleInnerSubscriber(this)));
|
---|
21 | }
|
---|
22 | }
|
---|
23 | notifyNext() {
|
---|
24 | this.values.clear();
|
---|
25 | }
|
---|
26 | notifyError(error) {
|
---|
27 | this._error(error);
|
---|
28 | }
|
---|
29 | _next(value) {
|
---|
30 | if (this.keySelector) {
|
---|
31 | this._useKeySelector(value);
|
---|
32 | }
|
---|
33 | else {
|
---|
34 | this._finalizeNext(value, value);
|
---|
35 | }
|
---|
36 | }
|
---|
37 | _useKeySelector(value) {
|
---|
38 | let key;
|
---|
39 | const { destination } = this;
|
---|
40 | try {
|
---|
41 | key = this.keySelector(value);
|
---|
42 | }
|
---|
43 | catch (err) {
|
---|
44 | destination.error(err);
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | this._finalizeNext(key, value);
|
---|
48 | }
|
---|
49 | _finalizeNext(key, value) {
|
---|
50 | const { values } = this;
|
---|
51 | if (!values.has(key)) {
|
---|
52 | values.add(key);
|
---|
53 | this.destination.next(value);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | //# sourceMappingURL=distinct.js.map |
---|