diff --git a/docs/task-instance.md b/docs/task-instance.md index 1fd65b57..e0cd8f33 100644 --- a/docs/task-instance.md +++ b/docs/task-instance.md @@ -2,6 +2,28 @@ The `Task` class is the core of this library. Below are all the methods that can be called on a task. To create a new task, refer to [Creating Tasks](docs/task-static.md). +## cancel + +Cancels a task, meaning it will never complete. + +{% tabs %} +{% tab title="Usage" %} + +```typescript +const task = Task.of(5).cancel(); +``` + +{% endtab %} + +{% tab title="Type Definition" %} + +```typescript +type cancel = () => void; +``` + +{% endtab %} +{% endtabs %} + ## map Given a task, when it has _succeeded_, pass the value through a mapping function. diff --git a/src/Task/Task.ts b/src/Task/Task.ts index d4436753..1d01fb02 100644 --- a/src/Task/Task.ts +++ b/src/Task/Task.ts @@ -1,4 +1,4 @@ -/* eslint-disable @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-explicit-any, @typescript-eslint/no-use-before-define */ +/* eslint-disable @typescript-eslint/no-misused-promises, @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-explicit-any, @typescript-eslint/no-use-before-define */ import { constant, identity, range } from "../util"; export type Reject = (error: E) => void; @@ -37,27 +37,27 @@ export class Task implements PromiseLike { public static flatten = flatten; public isCanceled = false; - public fork: Fork; + constructor(private computation: Fork) {} - constructor(computation: Fork) { - this.fork = (reject: Reject, resolve: Resolve) => { - if (this.isCanceled) { - return; - } + fork(reject: Reject, resolve: Resolve) { + if (this.isCanceled) { + return this; + } - computation( - err => { - if (!this.isCanceled) { - reject(err); - } - }, - value => { - if (!this.isCanceled) { - resolve(value); - } + this.computation( + err => { + if (!this.isCanceled) { + reject(err); } - ); - }; + }, + value => { + if (!this.isCanceled) { + resolve(value); + } + } + ); + + return this; } cancel() { @@ -313,7 +313,7 @@ export function fork( reject: Reject, resolve: Resolve, task: Task -): void { +): Task { return task.fork(reject, resolve); } diff --git a/src/Task/__tests__/cancellation.spec.ts b/src/Task/__tests__/cancellation.spec.ts index de4f64f9..477d58df 100644 --- a/src/Task/__tests__/cancellation.spec.ts +++ b/src/Task/__tests__/cancellation.spec.ts @@ -8,14 +8,14 @@ describe("cancellation", () => { const resolve = jest.fn(); const reject = jest.fn(); - const task = succeedIn(1000, SUCCESS_RESULT); - task.cancel(); - task.fork(reject, resolve); + succeedIn(1000, SUCCESS_RESULT) + .fork(reject, resolve) + .cancel(); + + jest.runAllTimers(); expect(resolve).not.toBeCalled(); expect(reject).not.toBeCalled(); - - jest.runAllTimers(); }); test("should be able to cancel a failed task", () => { @@ -24,13 +24,13 @@ describe("cancellation", () => { const resolve = jest.fn(); const reject = jest.fn(); - const task = failIn(1000, ERROR_RESULT); - task.cancel(); - task.fork(reject, resolve); + failIn(1000, ERROR_RESULT) + .fork(reject, resolve) + .cancel(); + + jest.runAllTimers(); expect(resolve).not.toBeCalled(); expect(reject).not.toBeCalled(); - - jest.runAllTimers(); }); });