Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/execution/incremental/Computation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ type MaybePromise<T> =
/** @internal **/
export class Computation<T> {
private _fn: () => PromiseOrValue<T>;
private _onCancel: ((reason?: unknown) => void) | undefined;
private _onAbort: ((reason?: unknown) => void) | undefined;
private _maybePromise?: MaybePromise<T>;
constructor(
fn: () => PromiseOrValue<T>,
onCancel?: (reason?: unknown) => void,
onAbort?: (reason?: unknown) => void,
) {
this._fn = fn;
this._onCancel = onCancel;
this._onAbort = onAbort;
}
prime(): MaybePromise<T> {
if (this._maybePromise) {
Expand Down Expand Up @@ -54,7 +54,7 @@ export class Computation<T> {
}
}
}
cancel(reason?: unknown): void {
abort(reason?: unknown): void {
const maybePromise = this._maybePromise;
if (!maybePromise) {
this._maybePromise = {
Expand All @@ -64,8 +64,8 @@ export class Computation<T> {
return;
}
const status = maybePromise.status;
if (status === 'pending' && this._onCancel) {
this._onCancel(reason);
if (status === 'pending' && this._onAbort) {
this._onAbort(reason);
this._maybePromise = {
status: 'rejected',
reason,
Expand Down
4 changes: 2 additions & 2 deletions src/execution/incremental/IncrementalExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export class IncrementalExecutor<
override cancel(reason?: unknown): void {
super.cancel(reason);
for (const task of this.tasks) {
task.computation.cancel(reason);
task.computation.abort(reason);
}
for (const stream of this.streams) {
const aborted = stream.queue.abort(reason);
Expand Down Expand Up @@ -594,7 +594,7 @@ export class IncrementalExecutor<
const filteredTasks: Array<ExecutionGroup> = [];
for (const task of tasks) {
if (collectedErrors.hasNulledPosition(task.path)) {
task.computation.cancel(cancellationReason);
task.computation.abort(cancellationReason);
} else {
filteredTasks.push(task);
}
Expand Down
2 changes: 1 addition & 1 deletion src/execution/incremental/WorkQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export function createWorkQueue<
reason: unknown,
cancelPromises: Array<Promise<unknown>>,
): void {
task.computation.cancel(reason);
task.computation.abort(reason);
const taskNode = taskNodes.get(task);
if (taskNode) {
for (const childStream of taskNode.childStreams) {
Expand Down
54 changes: 27 additions & 27 deletions src/execution/incremental/__tests__/Computation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,93 +106,93 @@ describe('Computation', () => {
expect(runCount).to.equal(1);
});

it('can be cancelled before running', () => {
let onCancelRan = false;
it('can be aborted before running', () => {
let onAbortRan = false;
const computation = new Computation(
() => ({ value: 123 }),
() => {
onCancelRan = true;
onAbortRan = true;
},
);
computation.cancel();
computation.abort();
expect(() => computation.result()).to.throw('Cancelled!');
expect(onCancelRan).to.equal(false);
expect(onAbortRan).to.equal(false);
});

it('cannot be cancelled after running synchronously', () => {
let onCancelRan = false;
it('cannot be aborted after running synchronously', () => {
let onAbortRan = false;
const computation = new Computation(
() => ({ value: 123 }),
() => {
onCancelRan = true;
onAbortRan = true;
},
);

computation.prime();
computation.cancel();
computation.abort();
expect(computation.result()).to.deep.equal({ value: 123 });
expect(onCancelRan).to.equal(false);
expect(onAbortRan).to.equal(false);
});

it('cannot be cancelled after erroring synchronously', () => {
let onCancelRan = false;
it('cannot be aborted after erroring synchronously', () => {
let onAbortRan = false;
const computation = new Computation(
() => {
throw new Error('failure');
},
() => {
onCancelRan = true;
onAbortRan = true;
},
);

computation.prime();
computation.cancel();
computation.abort();
expect(() => computation.result()).to.throw('failure');
expect(onCancelRan).to.equal(false);
expect(onAbortRan).to.equal(false);
});

it('can be cancelled while running asynchronously', () => {
let onCancelRan = false;
it('can be aborted while running asynchronously', () => {
let onAbortRan = false;
const computation = new Computation(
() =>
new Promise(() => {
// Never resolves.
}),
() => {
onCancelRan = true;
onAbortRan = true;
},
);

computation.prime();
computation.cancel();
expect(onCancelRan).to.equal(true);
computation.abort();
expect(onAbortRan).to.equal(true);
expect(() => computation.result()).to.throw('Cancelled!');
});

it('can be cancelled with a provided reason before running', () => {
it('can be aborted with a provided reason before running', () => {
const abortReason = new Error('aborted');
const computation = new Computation(() => ({ value: 123 }));

computation.cancel(abortReason);
computation.abort(abortReason);
expect(() => computation.result()).to.throw('aborted');
});

it('forwards cancellation reason to onCancel while running asynchronously', () => {
it('forwards abort reason to onAbort while running asynchronously', () => {
const abortReason = new Error('aborted');
let onCancelReason: unknown;
let onAbortReason: unknown;
const computation = new Computation(
() =>
new Promise(() => {
// Never resolves.
}),
(reason) => {
onCancelReason = reason;
onAbortReason = reason;
},
);

computation.prime();
computation.cancel(abortReason);
expect(onCancelReason).to.equal(abortReason);
computation.abort(abortReason);
expect(onAbortReason).to.equal(abortReason);
expect(() => computation.result()).to.throw('aborted');
});
});
Loading