-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Add a new optional AbortController parameter to defer #10792
Draft
brophdawg11
wants to merge
3
commits into
dev
Choose a base branch
from
brophdawg11/defer-abort-controller
base: dev
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
} from "./history"; | ||
import type { | ||
ActionFunction, | ||
ActionFunctionArgs, | ||
AgnosticDataRouteMatch, | ||
AgnosticDataRouteObject, | ||
AgnosticRouteMatch, | ||
|
@@ -23,6 +24,7 @@ import type { | |
HTMLFormMethod, | ||
ImmutableRouteKey, | ||
LoaderFunction, | ||
LoaderFunctionArgs, | ||
MapRoutePropertiesFunction, | ||
MutationFormMethod, | ||
RedirectResult, | ||
|
@@ -3627,8 +3629,25 @@ async function callLoaderOrAction( | |
let abortPromise = new Promise((_, r) => (reject = r)); | ||
onReject = () => reject(); | ||
request.signal.addEventListener("abort", onReject); | ||
|
||
let runHandlerAndMaybeAbortDeferreds = async ( | ||
handler: ActionFunction | LoaderFunction, | ||
args: LoaderFunctionArgs | ActionFunctionArgs | ||
) => { | ||
// Since we Promise.race the handler against the request signal, if the | ||
// request is aborted we never get access to the returned value from the | ||
// handler. If we get a defer() instance _after_ we've already aborted | ||
// the request, we should proxy that cancellation along so the defer() | ||
// controller also aborts. | ||
let result = await handler(args); | ||
if (args.request.signal.aborted && isDeferredData(result)) { | ||
result.cancel(); | ||
} | ||
return result; | ||
}; | ||
|
||
return Promise.race([ | ||
handler({ | ||
runHandlerAndMaybeAbortDeferreds(handler, { | ||
request, | ||
params: match.params, | ||
context: opts.requestContext, | ||
|
@@ -3792,6 +3811,17 @@ async function callLoaderOrAction( | |
} | ||
|
||
if (isDeferredData(result)) { | ||
if (request.signal.aborted) { | ||
// Don't think this is technically possible since we race the loader | ||
// against the request signal and we would short circuit via the error path | ||
// above on interruption. Included to be safe though :) | ||
result.cancel(); | ||
} else { | ||
// Abort the defer() instance if we abort while waiting on other loaders | ||
// from this navigation | ||
let deferResult = result; | ||
request.signal.addEventListener("abort", () => deferResult.cancel()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cancel |
||
} | ||
return { | ||
type: ResultType.deferred, | ||
deferredData: result, | ||
|
This file contains 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 |
---|---|---|
|
@@ -1276,7 +1276,11 @@ export class DeferredData { | |
init?: ResponseInit; | ||
deferredKeys: string[] = []; | ||
|
||
constructor(data: Record<string, unknown>, responseInit?: ResponseInit) { | ||
constructor( | ||
data: Record<string, unknown>, | ||
responseInit: ResponseInit, | ||
controller: AbortController | ||
) { | ||
invariant( | ||
data && typeof data === "object" && !Array.isArray(data), | ||
"defer() only accepts plain objects" | ||
|
@@ -1286,7 +1290,7 @@ export class DeferredData { | |
// cancellation | ||
let reject: (e: AbortedDeferredError) => void; | ||
this.abortPromise = new Promise((_, r) => (reject = r)); | ||
this.controller = new AbortController(); | ||
this.controller = controller; | ||
let onAbort = () => | ||
reject(new AbortedDeferredError("Deferred data aborted")); | ||
this.unlistenAbortSignal = () => | ||
|
@@ -1455,13 +1459,18 @@ function unwrapTrackedPromise(value: any) { | |
|
||
export type DeferFunction = ( | ||
data: Record<string, unknown>, | ||
init?: number | ResponseInit | ||
init?: number | ResponseInit | null, | ||
controller?: AbortController | ||
) => DeferredData; | ||
|
||
export const defer: DeferFunction = (data, init = {}) => { | ||
let responseInit = typeof init === "number" ? { status: init } : init; | ||
|
||
return new DeferredData(data, responseInit); | ||
export const defer: DeferFunction = (data, init, controller) => { | ||
let responseInit = | ||
typeof init === "number" ? { status: init } : init != null ? init : {}; | ||
return new DeferredData( | ||
data, | ||
responseInit, | ||
controller || new AbortController() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use user |
||
); | ||
}; | ||
|
||
export type RedirectFunction = ( | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cancel
defer()
instances created after therequest
has already been aborted