feat(frontend): add ignoreCancellation option for background tasks
Allow useNavigationAbortSignal to opt out of navigation-based abort for long-lived background tasks like polling. Set ignoreCancellation: true to keep requests alive across route changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -2,22 +2,36 @@
|
||||
* Navigation-aware request cancellation context.
|
||||
*
|
||||
* Provides a global cancellation mechanism tied to route transitions.
|
||||
* When the user navigates to a new route, all AbortSignals obtained from
|
||||
* this context are automatically aborted, cancelling in-flight requests
|
||||
* associated with the previous route.
|
||||
* When the user navigates to a new route, all AbortSignals whose associated
|
||||
* pathname no longer matches the current route are automatically aborted,
|
||||
* cancelling in-flight requests associated with the previous route.
|
||||
*
|
||||
* Design:
|
||||
* - Each request is associated with the pathname that was active when it started.
|
||||
* - On navigation, all controllers for the old pathname are aborted.
|
||||
* - Requests can opt out of cancellation by setting `ignoreCancellation: true`.
|
||||
* - Background tasks (polling, long-lived syncs) should opt out.
|
||||
*
|
||||
* Long-lived background fetches (e.g., polling with long TTL) can opt-out
|
||||
* by not using this context and instead managing their own lifecycle,
|
||||
* or by checking the signal early in their lifecycle.
|
||||
*
|
||||
* Design notes:
|
||||
* - Subscribers are notified immediately when navigation occurs
|
||||
* - Multiple consumers can safely subscribe and get independent signals
|
||||
* - Signals are generator functions to allow late binding
|
||||
*/
|
||||
|
||||
import { createContext } from "react";
|
||||
|
||||
/**
|
||||
* Options for getNavigationSignal().
|
||||
*/
|
||||
export interface GetNavigationSignalOptions {
|
||||
/**
|
||||
* If true, the signal will not be aborted on navigation.
|
||||
* Use for background tasks that should survive route changes.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
ignoreCancellation?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a fresh AbortSignal tied to the current route lifecycle.
|
||||
*
|
||||
@@ -32,16 +46,14 @@ export interface NavigationCancellationContextType {
|
||||
* to a different route. This is ideal for route-specific data fetches
|
||||
* that should not persist across page transitions.
|
||||
*
|
||||
* Example:
|
||||
* const signal = useNavigationAbortSignal();
|
||||
* const { items } = useListData({
|
||||
* fetcher: (sig) => fetchBans(sig || signal),
|
||||
* // ...
|
||||
* });
|
||||
* @param options - Optional configuration for the signal
|
||||
* @param options.ignoreCancellation - If true, the signal will not be
|
||||
* aborted on navigation. Use for background tasks that should survive
|
||||
* route changes.
|
||||
*
|
||||
* @returns An AbortSignal that lives for the duration of the current route
|
||||
*/
|
||||
getNavigationSignal(): AbortSignal;
|
||||
getNavigationSignal(options?: GetNavigationSignalOptions): AbortSignal;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user