Skip to content

Commit

Permalink
Merge pull request #837 from thefrontside/good-halt-on-bad-task
Browse files Browse the repository at this point in the history
Task.halt() always succeeds as long as teardown succeeds.
  • Loading branch information
cowboyd authored Nov 29, 2023
2 parents e1bb551 + 5fec9c2 commit b0f8f38
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/run/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ export function createFrame<T>(options: FrameOptions<T>): Frame<T> {
}

if (!destruction.ok) {
setResults({ ok: false, error: destruction.error, exit });
setResults({ ok: false, error: destruction.error, exit, destruction });
} else {
if (exit.type === "aborted") {
setResults({ ok: true, value: void 0, exit });
setResults({ ok: true, value: void 0, exit, destruction });
} else if (exit.type === "result") {
let { result } = exit;
if (result.ok) {
setResults({ ok: true, value: void 0, exit });
setResults({ ok: true, value: void 0, exit, destruction });
} else {
setResults({ ok: false, error: result.error, exit });
setResults({ ok: false, error: result.error, exit, destruction });
}
} else {
setResults({ ok: false, error: exit.error, exit });
setResults({ ok: false, error: exit.error, exit, destruction });
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions lib/run/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export function createTask<T>(
};
let awaitHaltResult = (resolve: Resolve<void>, reject: Reject) => {
evaluate(function* () {
let result = yield* frame;
if (result.ok) {
resolve(result.value);
let { destruction } = yield* frame;
if (destruction.ok) {
resolve();
} else {
reject(result.error);
reject(destruction.error);
}
});
};
Expand Down
1 change: 1 addition & 0 deletions lib/run/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export type Exit<T> = {
*/
export type FrameResult<T> = Result<void> & {
exit: Exit<T>;
destruction: Result<void>;
};
9 changes: 9 additions & 0 deletions test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,13 @@ describe("run()", () => {
expect(error.message).toEqual("boom");
}
});

it("successfully halts when task fails, but shutdown succeeds ", async () => {
let task = run(function* () {
throw new Error("boom!");
});

await expect(task).rejects.toHaveProperty("message", "boom!");
await expect(task.halt()).resolves.toBe(undefined);
});
});

0 comments on commit b0f8f38

Please sign in to comment.