Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: propagate abort reason in derived signals #104

Merged
merged 1 commit into from
Nov 9, 2024
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
8 changes: 4 additions & 4 deletions src/common/Event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { TaskCancelledError } from '../errors/TaskCancelledError';

/**
Expand Down Expand Up @@ -79,18 +79,18 @@
}

/** Creates an Event that fires when the signal is aborted. */
export const onAbort = (signal: AbortSignal): { event: Event<void> } & IDisposable => {
const evt = new OneShotEvent<void>();
export const onAbort = (signal: AbortSignal): { event: Event<unknown> } & IDisposable => {
const evt = new OneShotEvent<unknown>();
if (signal.aborted) {
evt.emit();
evt.emit(signal.reason);
return { event: evt.addListener, dispose: () => {} };
}

const dispose = () => (signal as any).removeEventListener('abort', l);

// @types/node is currently missing the event types on AbortSignal
const l = () => {
evt.emit();
evt.emit(signal.reason);
dispose();
};

Expand Down
21 changes: 21 additions & 0 deletions src/common/abort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai';
import { deriveAbortController } from './abort';

describe('deriveAbortController', () => {
it('should return an aborted AbortController when the provided signal is already aborted', () => {
const parentCtrl = new AbortController();
parentCtrl.abort(new Error('asdf'));
const { ctrl } = deriveAbortController(parentCtrl.signal);
expect(ctrl.signal.aborted).to.be.true;
expect(ctrl.signal.reason).to.equal(parentCtrl.signal.reason);
});

it('should abort the new AbortController when the provided signal aborts', () => {
const parentCtrl = new AbortController();
const { ctrl } = deriveAbortController(parentCtrl.signal);
expect(ctrl.signal.aborted).to.be.false;
parentCtrl.abort(new Error('asdf'));
expect(ctrl.signal.aborted).to.be.true;
expect(ctrl.signal.reason).to.equal(parentCtrl.signal.reason);
});
});
4 changes: 2 additions & 2 deletions src/common/abort.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { IDisposable, onAbort } from './Event';

export const neverAbortedSignal = new AbortController().signal;
Expand All @@ -22,10 +22,10 @@
}

if (signal.aborted) {
ctrl.abort();
ctrl.abort(signal.reason);
} else {
const abortEvt = onAbort(signal);
abortEvt.event(() => ctrl.abort());
abortEvt.event(reason => ctrl.abort(reason));
dispose = abortEvt.dispose;
}

Expand Down