-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): Add distinctUntilChanged and distinctUntilKeyChanged
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.distinctUntilChanged()', function () { | ||
it('should distinguish between values', function (done) { | ||
var expected = [1, 2, 1]; | ||
Observable | ||
.of(1, 1, 1, 2, 2, 1) | ||
.distinctUntilChanged() | ||
.subscribe(function(x) { | ||
expect(x).toBe(expected.shift()); | ||
}, null, done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.distinctUntilKeyChanged()', function () { | ||
it('should distinguish between values', function (done) { | ||
var expected = [{val: 1}, {val: 2}, {val: 1}]; | ||
Observable | ||
.of({val: 1}, {val: 1}, {val: 1}, {val: 2}, {val: 2}, {val: 1}) | ||
.distinctUntilKeyChanged("val") | ||
.subscribe(function(x) { | ||
expect(x).toDeepEqual(expected.shift()); | ||
}, null, done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
|
||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
import bindCallback from '../util/bindCallback'; | ||
|
||
export default function distinctUntilChanged<T>(compare?: (x: T, y: T) => boolean, thisArg?: any) { | ||
return this.lift(new DistinctUntilChangedOperator(thisArg ? | ||
<(x: T, y: T) => boolean> bindCallback(compare, thisArg, 2) : | ||
compare)); | ||
} | ||
|
||
export class DistinctUntilChangedOperator<T, R> extends Operator<T, R> { | ||
|
||
compare: (x: T, y: T) => boolean; | ||
|
||
constructor(compare?: (x: T, y: T) => boolean) { | ||
super(); | ||
this.compare = compare; | ||
} | ||
|
||
call(observer: Observer<T>): Observer<T> { | ||
return new DistinctUntilChangedSubscriber(observer, this.compare); | ||
} | ||
} | ||
|
||
export class DistinctUntilChangedSubscriber<T> extends Subscriber<T> { | ||
|
||
value: T; | ||
hasValue: boolean = false; | ||
|
||
constructor(destination: Observer<T>, compare?: (x: T, y: T) => boolean) { | ||
super(destination); | ||
if (typeof compare === "function") { | ||
this.compare = compare; | ||
} | ||
} | ||
|
||
compare(x: T, y: T) { | ||
return x === y; | ||
} | ||
|
||
_next(x) { | ||
|
||
let result: any = false; | ||
|
||
if(this.hasValue) { | ||
result = tryCatch(this.compare)(this.value, x); | ||
if (result === errorObject) { | ||
this.destination.error(errorObject.e); | ||
return; | ||
} | ||
} else { | ||
this.hasValue = true; | ||
} | ||
|
||
if (Boolean(result) === false) { | ||
this.value = x; | ||
this.destination.next(x); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import distinctUntilChanged from './distinctUntilChanged'; | ||
|
||
export default function distinctUntilKeyChanged<T>(key: string, compare?: (x: any, y: any) => boolean, thisArg?: any) { | ||
return distinctUntilChanged.call(this, function(x, y) { | ||
if (compare) { | ||
return compare.call(thisArg, x[key], y[key]); | ||
} | ||
return x[key] === y[key]; | ||
}); | ||
} |