-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
170 additions
and
32 deletions.
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
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,102 @@ | ||
import { Subject } from "rxjs"; | ||
import { expectCallAndReset } from "../test-helpers"; | ||
import { filterBehavior } from "./filter-behavior"; | ||
|
||
describe("filterBehavior()", () => { | ||
it("filters items based on the supplied predicate", () => { | ||
const source = new Subject(); | ||
const predicate = jasmine.createSpy(); | ||
const next = jasmine.createSpy(); | ||
source.pipe(filterBehavior(predicate)).subscribe(next); | ||
|
||
source.next(1); | ||
expectCallAndReset(next, 1); | ||
|
||
predicate.and.returnValue(true); | ||
source.next(2); | ||
expectCallAndReset(next, 2); | ||
source.next(3); | ||
expectCallAndReset(next, 3); | ||
|
||
predicate.and.returnValue(false); | ||
source.next(4); | ||
source.next(5); | ||
expect(next).not.toHaveBeenCalled(); | ||
|
||
predicate.and.returnValue(true); | ||
source.next(6); | ||
expectCallAndReset(next, 6); | ||
}); | ||
|
||
it("emits the first value unconditionally for each subscriber", () => { | ||
const source = new Subject(); | ||
const predicate = jasmine.createSpy().and.returnValue(false); | ||
const filtered$ = source.pipe(filterBehavior(predicate)); | ||
|
||
const next1 = jasmine.createSpy(); | ||
filtered$.subscribe(next1); | ||
source.next(1); | ||
source.next(2); | ||
source.next(3); | ||
|
||
const next2 = jasmine.createSpy(); | ||
filtered$.subscribe(next2); | ||
source.next(4); | ||
source.next(5); | ||
source.next(6); | ||
|
||
const next3 = jasmine.createSpy(); | ||
filtered$.subscribe(next3); | ||
source.next(7); | ||
source.next(8); | ||
source.next(9); | ||
|
||
expectCallAndReset(next1, 1); | ||
expectCallAndReset(next2, 4); | ||
expectCallAndReset(next3, 7); | ||
}); | ||
|
||
it("unsubscribes from upstream observable", () => { | ||
const source = new Subject(); | ||
const filtered$ = source.pipe(filterBehavior(() => true)); | ||
|
||
const sub1 = filtered$.subscribe(); | ||
const sub2 = filtered$.subscribe(); | ||
expect(source.observers.length).toBe(2); | ||
|
||
sub1.unsubscribe(); | ||
expect(source.observers.length).toBe(1); | ||
|
||
sub2.unsubscribe(); | ||
expect(source.observers.length).toBe(0); | ||
}); | ||
|
||
it("passes along errors", () => { | ||
const source = new Subject(); | ||
const cached = source.pipe(filterBehavior(() => false)); | ||
const error1 = jasmine.createSpy(); | ||
const error2 = jasmine.createSpy(); | ||
const err = Symbol(); | ||
|
||
cached.subscribe(undefined, error1); | ||
cached.subscribe(undefined, error2); | ||
source.error(err); | ||
|
||
expect(error1).toHaveBeenCalledWith(err); | ||
expect(error2).toHaveBeenCalledWith(err); | ||
}); | ||
|
||
it("passes along completion", () => { | ||
const source = new Subject(); | ||
const cached = source.pipe(filterBehavior(() => false)); | ||
const complete1 = jasmine.createSpy(); | ||
const complete2 = jasmine.createSpy(); | ||
|
||
cached.subscribe(undefined, undefined, complete1); | ||
cached.subscribe(undefined, undefined, complete2); | ||
source.complete(); | ||
|
||
expectCallAndReset(complete1); | ||
expectCallAndReset(complete2); | ||
}); | ||
}); |
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,31 @@ | ||
import { MonoTypeOperatorFunction } from "rxjs"; | ||
import { createPipeable } from "./create-pipeable"; | ||
|
||
/** | ||
* Works like `filter()`, but always lets through the first emission for each new subscriber. This makes it suitable for subscribers that expect the observable to behave like a `BehaviorSubject`, where the first emission is processed synchronously during the call to `subscribe()` (such as the `async` pipe in an Angular template). | ||
* | ||
* ``` | ||
* source: |-false--true--false--true--false--true-| | ||
* filterBehavior(identity): |-false--true---------true---------true-| | ||
* filterBehavior(identity): |-true---------true---------true-| | ||
* filterBehavior(identity): |-false--true---------true-| | ||
* ``` | ||
*/ | ||
export function filterBehavior<T>( | ||
predicate: (value: T) => boolean, | ||
): MonoTypeOperatorFunction<T> { | ||
return createPipeable<T, T>((upstream$, downstream, manager) => { | ||
let firstValue = true; | ||
manager.subscribeTo( | ||
upstream$, | ||
(value) => { | ||
if (firstValue || predicate(value)) { | ||
downstream.next(value); | ||
} | ||
firstValue = false; | ||
}, | ||
downstream.error.bind(downstream), | ||
downstream.complete.bind(downstream), | ||
); | ||
}); | ||
} |
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
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