Skip to content

Commit

Permalink
feat(cancellation): allow fork to chain so cancel can be called after
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Feb 25, 2020
1 parent 622228c commit 59e1242
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
22 changes: 22 additions & 0 deletions docs/task-instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 20 additions & 20 deletions src/Task/Task.ts
Original file line number Diff line number Diff line change
@@ -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<E> = (error: E) => void;
Expand Down Expand Up @@ -37,27 +37,27 @@ export class Task<E, S> implements PromiseLike<S> {
public static flatten = flatten;

public isCanceled = false;
public fork: Fork<E, S>;
constructor(private computation: Fork<E, S>) {}

constructor(computation: Fork<E, S>) {
this.fork = (reject: Reject<E>, resolve: Resolve<S>) => {
if (this.isCanceled) {
return;
}
fork(reject: Reject<E>, resolve: Resolve<S>) {
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() {
Expand Down Expand Up @@ -313,7 +313,7 @@ export function fork<E, S>(
reject: Reject<E>,
resolve: Resolve<S>,
task: Task<E, S>
): void {
): Task<E, S> {
return task.fork(reject, resolve);
}

Expand Down
20 changes: 10 additions & 10 deletions src/Task/__tests__/cancellation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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();
});
});

0 comments on commit 59e1242

Please sign in to comment.