-
Notifications
You must be signed in to change notification settings - Fork 21
Simplify and stabilise AbortSignal.any ponyfill #593
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d22d01
better abortsignalany
enisdenjo 96561e9
changesets
enisdenjo 80ac86b
handle whatwg-node signals
enisdenjo ea9405a
abort-signal-any is zero-dep
enisdenjo 5247edc
use event target if this is unavailable
enisdenjo 97eb897
chore(dependencies): updated changesets for modified dependencies
github-actions[bot] 35ae446
always use event
enisdenjo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
.changeset/@graphql-hive_gateway-abort-signal-any-593-dependencies.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@graphql-hive/gateway-abort-signal-any': patch | ||
--- | ||
|
||
dependencies updates: | ||
|
||
- Removed dependency [`@graphql-tools/utils@^10.7.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.7.0) (from `dependencies`) | ||
- Removed dependency [`tslib@^2.8.1` ↗︎](https://www.npmjs.com/package/tslib/v/2.8.1) (from `dependencies`) | ||
- Removed dependency [`graphql@^15.0.0 || ^16.9.0 || ^17.0.0` ↗︎](https://www.npmjs.com/package/graphql/v/15.0.0) (from `peerDependencies`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-hive/gateway-abort-signal-any': major | ||
--- | ||
|
||
`abortSignalAny` uses `AbortSignal.any` if available and falls back to `AbortController` for ponyfilling |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-hive/gateway-abort-signal-any': major | ||
--- | ||
|
||
`abortSignalAny` returns the native `AbortSignal` | ||
|
||
Instead of the custom `AbortSignalFromAny`. There is no need for the complication of adding signals to existing sets, chaining signals enough as well as simpler. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-hive/gateway-abort-signal-any': major | ||
--- | ||
|
||
`AbortSignal.any` ponyfill under `abortSignalAny` is now stable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-hive/gateway-runtime': patch | ||
--- | ||
|
||
Use improved AbortSignal.any ponyfill fixing potential memory leaks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,28 @@ | ||
import { registerAbortSignalListener } from '@graphql-tools/utils'; | ||
export function abortSignalAny(iterable: Iterable<AbortSignal>) { | ||
const signals = Array.from(iterable); | ||
|
||
export type AbortSignalFromAny = AbortSignal & { | ||
signals: Set<AbortSignal>; | ||
addSignals(signals: Iterable<AbortSignal>): void; | ||
}; | ||
const aborted = signals.find((s) => s.aborted); | ||
if (aborted) { | ||
// some signal is already aborted, immediately abort | ||
return aborted; | ||
} | ||
|
||
export function isAbortSignalFromAny( | ||
signal?: AbortSignal | null, | ||
): signal is AbortSignalFromAny { | ||
return signal != null && 'signals' in signal && 'addSignals' in signal; | ||
} | ||
// TODO: we cant use the native abortsignal.any because it doesnt work with signals coming from whatwg-node (ServerAdapterRequestAbortSignal) | ||
// if ('any' in AbortSignal) { | ||
// return AbortSignal.any(signals); | ||
// } | ||
|
||
export function abortSignalAny(givenSignals: Iterable<AbortSignal>) { | ||
const signals = new Set<AbortSignal>(); | ||
let singleSignal: AbortSignal | undefined; | ||
for (const signal of givenSignals) { | ||
if (isAbortSignalFromAny(signal)) { | ||
for (const childSignal of signal.signals) { | ||
singleSignal = childSignal; | ||
signals.add(childSignal); | ||
} | ||
} else { | ||
singleSignal = signal; | ||
signals.add(signal); | ||
} | ||
} | ||
if (signals.size < 2) { | ||
return singleSignal; | ||
} | ||
if (signals.size === 0) { | ||
return undefined; | ||
} | ||
// otherwise ready a controller and listen for abort signals | ||
const ctrl = new AbortController(); | ||
function onAbort(this: AbortSignal, ev?: Event) { | ||
const signal = this || (ev?.target as AbortSignal); | ||
ctrl.abort(signal?.reason); | ||
function abort(event: Event) { | ||
ctrl.abort((event.target as AbortSignal).reason); | ||
// do cleanup | ||
for (const signal of signals) { | ||
signal.removeEventListener('abort', abort); | ||
} | ||
} | ||
for (const signal of signals) { | ||
registerAbortSignalListener(signal, onAbort); | ||
signal.addEventListener('abort', abort); | ||
} | ||
Object.defineProperties(ctrl.signal, { | ||
signals: { value: signals }, | ||
addSignals: { | ||
value(newSignals: Iterable<AbortSignal>) { | ||
for (const signal of newSignals) { | ||
if (isAbortSignalFromAny(signal)) { | ||
for (const childSignal of signal.signals) { | ||
if (!signals.has(childSignal)) { | ||
signals.add(childSignal); | ||
registerAbortSignalListener(childSignal, onAbort); | ||
} | ||
} | ||
} else { | ||
if (!signals.has(signal)) { | ||
signals.add(signal); | ||
registerAbortSignalListener(signal, onAbort); | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}); | ||
return ctrl.signal as AbortSignalFromAny; | ||
return ctrl.signal; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.