-
-
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(startWithTimeout): Add operator.
- Loading branch information
Showing
3 changed files
with
59 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,44 @@ | ||
/** | ||
* @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 { startWithTimeout } from "./startWithTimeout"; | ||
import { marbles } from "rxjs-marbles"; | ||
|
||
describe("startWithTimeout", () => { | ||
|
||
it("should do nothing if the source emits within the duration", marbles(m => { | ||
|
||
const source = m.cold("--a--b--c--|"); | ||
const subs = "^----------!"; | ||
const expected = m.cold("--a--b--c--|"); | ||
|
||
const destination = source.pipe(startWithTimeout("z", m.time("---|"))); | ||
m.expect(destination).toBeObservable(expected); | ||
m.expect(source).toHaveSubscriptions(subs); | ||
})); | ||
|
||
it("should emit the value if the source is empty", marbles(m => { | ||
|
||
const source = m.cold("-----------|"); | ||
const subs = "^----------!"; | ||
const expected = m.cold("---z-------|"); | ||
|
||
const destination = source.pipe(startWithTimeout("z", m.time("---|"))); | ||
m.expect(destination).toBeObservable(expected); | ||
m.expect(source).toHaveSubscriptions(subs); | ||
})); | ||
|
||
it("should emit the value if the source emits after the duration", marbles(m => { | ||
|
||
const source = m.cold("-----a-b-c-|"); | ||
const subs = "^----------!"; | ||
const expected = m.cold("---z-a-b-c-|"); | ||
|
||
const destination = source.pipe(startWithTimeout("z", m.time("---|"))); | ||
m.expect(destination).toBeObservable(expected); | ||
m.expect(source).toHaveSubscriptions(subs); | ||
})); | ||
}); |
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,14 @@ | ||
/** | ||
* @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 { concat, MonoTypeOperatorFunction, race, SchedulerLike, timer } from "rxjs"; | ||
import { mapTo, publish } from "rxjs/operators"; | ||
|
||
export function startWithTimeout<T>(value: T, duration: number | Date, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T> { | ||
return source => source.pipe(publish(published => race( | ||
published, | ||
concat(timer(duration, scheduler).pipe(mapTo(value)), published) | ||
))); | ||
} |