Skip to content

Commit

Permalink
Merge f378055 into d14e27a
Browse files Browse the repository at this point in the history
  • Loading branch information
krisselden committed May 20, 2019
2 parents d14e27a + f378055 commit e02cda5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ node_js:
- "stable"
after_script:
- "cat ./coverage/lcov.info | yarn coveralls"
branches:
only:
- master
- /^v\d+\.\d+\.\d+/
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface Cancellation<Kind extends string = string> {
export type Complete<Result> = (result: Result) => void;

export type RaceCancellation = <Result>(
task: Task<Result>
task: Task<Result> | PromiseLike<Result>
) => Promise<Result | Cancellation>;

export type NewCancellation = () => Cancellation;
Expand Down
10 changes: 6 additions & 4 deletions src/newRaceCancellation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export default function newRaceCancellation(

function raceCancellation<Result>(
cancellation: Oneshot<unknown>,
task: Task<Result>,
task: Task<Result> | PromiseLike<Result>,
intoCancellation: IntoCancellation
): Promise<Result | Cancellation> {
return cancellation[hasCompleted]
? cancellation().then(intoCancellation)
: Promise.race([task(), cancellation().then(intoCancellation)]);
return typeof task === "function"
? cancellation[hasCompleted]
? cancellation().then(intoCancellation)
: Promise.race([task(), cancellation().then(intoCancellation)])
: Promise.race([task, cancellation().then(intoCancellation)]);
}

function newIntoCancellation(
Expand Down
7 changes: 5 additions & 2 deletions src/noopRaceCancellation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { RaceCancellation, Task } from "./interfaces";
* in a backwards compatible way by using this as the default.
*/
const noopRaceCancellation: RaceCancellation = <Result>(
task: Task<Result>
): Promise<Result> => Promise.resolve().then(task);
task: Task<Result> | PromiseLike<Result>
): Promise<Result> =>
typeof task === "function"
? Promise.resolve().then(task)
: Promise.resolve(task);

export default noopRaceCancellation;
18 changes: 18 additions & 0 deletions tests/cancellableRaceTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { cancellableRace } = require("race-cancellation");

QUnit.module("cancellableRace", () => {
QUnit.test("cancel before race already rejected promise", async assert => {
assert.expect(1);

const [raceCancellation, cancel] = cancellableRace();

cancel();
const task = Promise.reject(new Error("failed task"));

try {
await raceCancellation(task);
} catch (e) {
assert.equal(e.message, "failed task");
}
});
});
8 changes: 5 additions & 3 deletions tests/combineRaceCancellationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
combineRaceCancellation,
cancellableRace,
throwIfCancelled,
noopRaceCancellation,
} = require("race-cancellation");

QUnit.module("combineRaceCancellation", () => {
Expand All @@ -14,7 +15,7 @@ QUnit.module("combineRaceCancellation", () => {

QUnit.test("calling combine with b as undefined", async assert => {
const raceCancellation = combineRaceCancellation(
task => Promise.resolve().then(task),
noopRaceCancellation,
undefined
);
const expected = new Date();
Expand All @@ -23,8 +24,9 @@ QUnit.module("combineRaceCancellation", () => {
});

QUnit.test("calling combine with a as undefined", async assert => {
const raceCancellation = combineRaceCancellation(undefined, task =>
Promise.resolve().then(task)
const raceCancellation = combineRaceCancellation(
undefined,
noopRaceCancellation
);
const expected = new Date();
const actual = await raceCancellation(() => Promise.resolve(expected));
Expand Down
6 changes: 6 additions & 0 deletions tests/noopRaceCancellationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ QUnit.module("noopRaceCancellation", () => {
const actual = await noopRaceCancellation(() => Promise.resolve(expected));
assert.equal(actual, expected);
});

QUnit.test("it just resolves the promise", async assert => {
const expected = new Date();
const actual = await noopRaceCancellation(Promise.resolve(expected));
assert.equal(actual, expected);
});
});

0 comments on commit e02cda5

Please sign in to comment.