Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.6 KB
|
Line | |
---|
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 |
---|
Note:
See
TracBrowser
for help on using the repository browser.