-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Issue 1933 - Cancellation of requests when no longer needed #2275
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,15 @@ import type { defaultConfig } from './utils/config' | |
export type GlobalState = [ | ||
Record<string, RevalidateCallback[]>, // EVENT_REVALIDATORS | ||
Record<string, [number, number]>, // MUTATION: [ts, end_ts] | ||
Record<string, [any, number]>, // FETCH: [data, ts] | ||
Record< | ||
string, | ||
[ | ||
data: any, | ||
timestamp: number, | ||
revalidateCount: number, | ||
abort: AbortController | undefined | ||
] | ||
>, // FETCH: [data, timestamp, revalidateCount, abort] | ||
Record<string, FetcherResponse<any>>, // PRELOAD | ||
ScopedMutator, // Mutator | ||
(key: string, value: any, prev: any) => void, // Setter | ||
|
@@ -14,15 +22,24 @@ export type FetcherResponse<Data = unknown> = Data | Promise<Data> | |
export type BareFetcher<Data = unknown> = ( | ||
...args: any[] | ||
) => FetcherResponse<Data> | ||
/** | ||
* Second parameter provided to fetcher functions. At present this only allows | ||
* for 'signal' for cancelling a request in progress, but allows for future | ||
* enhancements. | ||
*/ | ||
export interface FetcherOptions { | ||
signal?: AbortSignal | ||
[key: string]: unknown | ||
} | ||
export type Fetcher< | ||
Data = unknown, | ||
SWRKey extends Key = Key | ||
> = SWRKey extends () => infer Arg | null | undefined | false | ||
? (arg: Arg) => FetcherResponse<Data> | ||
? (arg: Arg, options: FetcherOptions) => FetcherResponse<Data> | ||
: SWRKey extends null | undefined | false | ||
? never | ||
: SWRKey extends infer Arg | ||
? (arg: Arg) => FetcherResponse<Data> | ||
? (arg: Arg, options: FetcherOptions) => FetcherResponse<Data> | ||
: never | ||
|
||
export type BlockingData< | ||
|
@@ -200,6 +217,12 @@ export interface PublicConfiguration< | |
* @link https://swr.vercel.app/docs/advanced/react-native#customize-focus-and-reconnect-events | ||
*/ | ||
isVisible: () => boolean | ||
|
||
/** | ||
* Enable this to pass an instance of AbortSignal to your fetcher that will be aborted when there no useSWR hooks remaining that are listening for the result of the fetch (as | ||
* a result for example of navigation, or change of search input). This allows you to cancel a long running a request if the result is no longer relevant, e.g. when doing a search. | ||
*/ | ||
abortDiscardedRequests?: boolean | ||
Comment on lines
+220
to
+225
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. I would like for this feature to be enabled by default. Not the other way around. Let's emphasize what the new users want rather than reverse compatibility since the risk of problems is low. Would a new user who has no prior version of swr installed prefer this on by default or not? I think yes but I'm only one user. 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. Agreed 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. Lest setting this to
I think we should not offer this config, but rather always pass If you're worried about constructing a bunch of |
||
} | ||
|
||
export type FullConfiguration< | ||
|
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.
I'd omit an index signature because it may hide typos for the valid options (only
signal
in this case). It also prevents us from adding properties without introducing a potential breaking change.