-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(initial): Add initial operator.
The reverse of subsequent.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc | ||
*/ | ||
/*tslint:disable:no-unused-expression*/ | ||
|
||
import { delay } from "rxjs/operators"; | ||
import { marbles } from "rxjs-marbles"; | ||
import { initial } from "./initial"; | ||
|
||
describe("initial", () => { | ||
|
||
it("should debounce only initial notifications", marbles((m) => { | ||
|
||
const source = m.cold("abc---d|"); | ||
const sourceSubs = "^------!"; | ||
const expected = m.cold("-bca--d|"); | ||
|
||
const duration = m.time("---|"); | ||
const destination = source.pipe(initial(s => s.pipe(delay(duration, m.scheduler)))); | ||
m.expect(destination).toBeObservable(expected); | ||
m.expect(source).toHaveSubscriptions(sourceSubs); | ||
})); | ||
|
||
it("should support count", marbles((m) => { | ||
|
||
const source = m.cold("abc---d|"); | ||
const sourceSubs = "^------!"; | ||
const expected = m.cold("--cab-d|"); | ||
|
||
const duration = m.time("---|"); | ||
const destination = source.pipe(initial(2, s => s.pipe(delay(duration, m.scheduler)))); | ||
m.expect(destination).toBeObservable(expected); | ||
m.expect(source).toHaveSubscriptions(sourceSubs); | ||
})); | ||
}); |
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,56 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc | ||
*/ | ||
|
||
import { | ||
ConnectableObservable, | ||
merge, | ||
MonoTypeOperatorFunction, | ||
Observable, | ||
OperatorFunction | ||
} from "rxjs/"; | ||
|
||
import { publish, skip, take } from "rxjs/operators"; | ||
|
||
export function initial<T, R>( | ||
count: number, | ||
selector: (source: Observable<T>) => Observable<R> | ||
): OperatorFunction<T, T | R>; | ||
|
||
export function initial<T>( | ||
count: number, | ||
selector: (source: Observable<T>) => Observable<T> | ||
): MonoTypeOperatorFunction<T>; | ||
|
||
export function initial<T, R>( | ||
selector: (source: Observable<T>) => Observable<R> | ||
): OperatorFunction<T, T | R>; | ||
|
||
export function initial<T>( | ||
selector: (source: Observable<T>) => Observable<T> | ||
): MonoTypeOperatorFunction<T>; | ||
|
||
export function initial<T, R>( | ||
countOrSelector: number | ((source: Observable<T>) => Observable<R>), | ||
selector?: (source: Observable<T>) => Observable<R> | ||
): OperatorFunction<T, T | R> { | ||
|
||
let count: number; | ||
if (typeof countOrSelector === "number") { | ||
count = countOrSelector; | ||
} else { | ||
count = 1; | ||
selector = countOrSelector; | ||
} | ||
|
||
return (source: Observable<T>) => new Observable<T | R>(observer => { | ||
const published = source.pipe(publish()) as ConnectableObservable<T>; | ||
const subscription = merge( | ||
selector!(published.pipe(take(count))), | ||
published.pipe(skip(count)) | ||
).subscribe(observer); | ||
subscription.add(published.connect()); | ||
return subscription; | ||
}); | ||
} |