Skip to content

Commit

Permalink
feat(startWithTimeout): Add operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Aug 19, 2018
1 parent 51ec2f4 commit 667cb3c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from "./rateLimit";
export * from "./refCountAuditTime";
export * from "./refCountOn";
export * from "./reschedule";
export * from "./startWithTimeout";
export * from "./subsequent";
export * from "./switchMapUntil";
export * from "./takeWhileInclusive";
Expand Down
44 changes: 44 additions & 0 deletions source/operators/startWithTimeout-spec.ts
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);
}));
});
14 changes: 14 additions & 0 deletions source/operators/startWithTimeout.ts
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)
)));
}

0 comments on commit 667cb3c

Please sign in to comment.