From b3f6f57ce1076241ee9a973858bb5c341517e89c Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 00:14:02 +0800 Subject: [PATCH 1/8] deprecate ConfigInterface; fix exported types --- src/config.ts | 6 +++--- src/index.ts | 14 ++++++++++++-- src/swr-config-context.ts | 4 ++-- src/types.ts | 38 +++++++++++++++++++++++++++----------- src/use-swr-infinite.ts | 9 +++++++-- src/use-swr.ts | 13 +++++++------ 6 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/config.ts b/src/config.ts index 3d7c8c74c..506f7873b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,6 @@ import { dequal } from 'dequal/lite' import { - ConfigInterface, + SWRConfiguration, RevalidateOptionInterface, revalidateType } from './types' @@ -14,7 +14,7 @@ const cache = new Cache() function onErrorRetry( _, __, - config: ConfigInterface, + config: SWRConfiguration, revalidate: revalidateType, opts: RevalidateOptionInterface ): void { @@ -47,7 +47,7 @@ const slowConnection = ['slow-2g', '2g'].indexOf(navigator['connection'].effectiveType) !== -1 // config -const defaultConfig: ConfigInterface = Object.assign( +const defaultConfig: SWRConfiguration = Object.assign( { // events onLoadingSlow: () => {}, diff --git a/src/index.ts b/src/index.ts index 44daca680..40d62ad59 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,27 @@ -export * from './use-swr' +// `useSWR` and related APIs import { default as useSWR } from './use-swr' +export default useSWR +export * from './use-swr' + +// `useSWRInfinite` export { useSWRInfinite, SWRInfiniteConfigInterface, SWRInfiniteResponseInterface } from './use-swr-infinite' + +// Cache related, to be replaced by the new APIs export { cache } from './config' + +// Types export { + // Legacy ConfigInterface, + // Latest + SWRConfiguration, revalidateType, RevalidateOptionInterface, keyInterface, responseInterface, CacheInterface } from './types' -export default useSWR diff --git a/src/swr-config-context.ts b/src/swr-config-context.ts index f3d2d1e7a..05fa8f625 100644 --- a/src/swr-config-context.ts +++ b/src/swr-config-context.ts @@ -1,8 +1,8 @@ import { createContext } from 'react' -import { ConfigInterface } from './types' +import { SWRConfiguration } from './types' -const SWRConfigContext = createContext>({}) +const SWRConfigContext = createContext({}) SWRConfigContext.displayName = 'SWRConfigContext' export default SWRConfigContext diff --git a/src/types.ts b/src/types.ts index 86aef5875..4a092e715 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,9 +1,12 @@ +// Internal types + export type fetcherFn = (...args: any) => Data | Promise -export interface ConfigInterface< + +export type Configuration< Data = any, Error = any, Fn extends fetcherFn = fetcherFn -> { +> = { errorRetryInterval: number errorRetryCount?: number loadingTimeout: number @@ -16,28 +19,24 @@ export interface ConfigInterface< revalidateOnMount?: boolean revalidateOnReconnect: boolean shouldRetryOnError: boolean - fetcher: Fn suspense: boolean + fetcher: Fn initialData?: Data isOnline: () => boolean isDocumentVisible: () => boolean isPaused: () => boolean - onLoadingSlow: (key: string, config: ConfigInterface) => void + onLoadingSlow: (key: string, config: Configuration) => void onSuccess: ( data: Data, key: string, - config: ConfigInterface - ) => void - onError: ( - err: Error, - key: string, - config: ConfigInterface + config: Configuration ) => void + onError: (err: Error, key: string, config: Configuration) => void onErrorRetry: ( err: Error, key: string, - config: ConfigInterface, + config: Configuration, revalidate: revalidateType, revalidateOpts: RevalidateOptionInterface ) => void @@ -112,3 +111,20 @@ export interface CacheInterface { } export type cacheListener = () => void + +// Public types + +/** + * @deprecated `ConfigInterface` will be renamed to `SWRConfiguration`. + */ +export type ConfigInterface< + Data = any, + Error = any, + Fn extends fetcherFn = fetcherFn +> = Partial> + +export type SWRConfiguration< + Data = any, + Error = any, + Fn extends fetcherFn = fetcherFn +> = Partial> diff --git a/src/use-swr-infinite.ts b/src/use-swr-infinite.ts index 63d8b88bc..9dfffe114 100644 --- a/src/use-swr-infinite.ts +++ b/src/use-swr-infinite.ts @@ -4,13 +4,18 @@ import defaultConfig, { cache } from './config' import SWRConfigContext from './swr-config-context' import useSWR from './use-swr' -import { keyType, fetcherFn, ConfigInterface, responseInterface } from './types' +import { + keyType, + fetcherFn, + SWRConfiguration, + responseInterface +} from './types' type KeyLoader = ( index: number, previousPageData: Data | null ) => keyType -type SWRInfiniteConfigInterface = ConfigInterface< +type SWRInfiniteConfigInterface = SWRConfiguration< Data[], Error, fetcherFn diff --git a/src/use-swr.ts b/src/use-swr.ts index 5847e8d72..b2f4846f9 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -14,7 +14,8 @@ import SWRConfigContext from './swr-config-context' import { actionType, broadcastStateInterface, - ConfigInterface, + Configuration, + SWRConfiguration, fetcherFn, keyInterface, mutateInterface, @@ -217,21 +218,21 @@ function useSWR( ): responseInterface function useSWR( key: keyInterface, - config?: Partial> + config?: SWRConfiguration ): responseInterface function useSWR( key: keyInterface, // `null` is used for a hack to manage shared state with SWR // https://github.com/vercel/swr/pull/918 fn?: fetcherFn | null, - config?: Partial> + config?: SWRConfiguration ): responseInterface function useSWR( _key: keyInterface, ...options: any[] ): responseInterface { let _fn: fetcherFn | undefined, - _config: Partial> = {} + _config: SWRConfiguration = {} if (options.length > 1) { _fn = options[0] _config = options[1] @@ -249,12 +250,12 @@ function useSWR( // `keyErr` is the cache key for error objects const [key, fnArgs, keyErr, keyValidating] = cache.serializeKey(_key) - const config: ConfigInterface = Object.assign( + const config = Object.assign( {}, defaultConfig, useContext(SWRConfigContext), _config - ) + ) as Configuration const configRef = useRef(config) useIsomorphicLayoutEffect(() => { From daf2106d1e9944d50a974e4cd6e67358a113eabd Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 00:26:13 +0800 Subject: [PATCH 2/8] deprecate SWRInfiniteConfigInterface and SWRInfiniteResponseInterface --- src/index.ts | 10 ++++----- src/types.ts | 49 +++++++++++++++++++++++++++++++++++++---- src/use-swr-infinite.ts | 46 ++++++++++---------------------------- 3 files changed, 62 insertions(+), 43 deletions(-) diff --git a/src/index.ts b/src/index.ts index 40d62ad59..d1153da14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,11 +4,7 @@ export default useSWR export * from './use-swr' // `useSWRInfinite` -export { - useSWRInfinite, - SWRInfiniteConfigInterface, - SWRInfiniteResponseInterface -} from './use-swr-infinite' +export { useSWRInfinite } from './use-swr-infinite' // Cache related, to be replaced by the new APIs export { cache } from './config' @@ -17,8 +13,12 @@ export { cache } from './config' export { // Legacy ConfigInterface, + SWRInfiniteConfigInterface, + SWRInfiniteResponseInterface, // Latest SWRConfiguration, + SWRInfiniteConfiguration, + SWRInfiniteResponse, revalidateType, RevalidateOptionInterface, keyInterface, diff --git a/src/types.ts b/src/types.ts index 4a092e715..d7aa4a3ab 100644 --- a/src/types.ts +++ b/src/types.ts @@ -114,6 +114,11 @@ export type cacheListener = () => void // Public types +export type SWRConfiguration< + Data = any, + Error = any, + Fn extends fetcherFn = fetcherFn +> = Partial> /** * @deprecated `ConfigInterface` will be renamed to `SWRConfiguration`. */ @@ -123,8 +128,44 @@ export type ConfigInterface< Fn extends fetcherFn = fetcherFn > = Partial> -export type SWRConfiguration< +export type SWRInfiniteConfiguration< Data = any, - Error = any, - Fn extends fetcherFn = fetcherFn -> = Partial> + Error = any +> = SWRConfiguration> & { + initialSize?: number + revalidateAll?: boolean + persistSize?: boolean +} +/** + * @deprecated `SWRInfiniteConfigInterface` will be renamed to `SWRInfiniteConfiguration`. + */ +export type SWRInfiniteConfigInterface< + Data = any, + Error = any +> = SWRConfiguration> & { + initialSize?: number + revalidateAll?: boolean + persistSize?: boolean +} + +export type SWRInfiniteResponse = responseInterface< + Data[], + Error +> & { + size: number + setSize: ( + size: number | ((size: number) => number) + ) => Promise +} +/** + * @deprecated `SWRInfiniteResponseInterface` will be renamed to `SWRInfiniteResponse`. + */ +export type SWRInfiniteResponseInterface< + Data = any, + Error = any +> = responseInterface & { + size: number + setSize: ( + size: number | ((size: number) => number) + ) => Promise +} diff --git a/src/use-swr-infinite.ts b/src/use-swr-infinite.ts index 9dfffe114..f2b15824b 100644 --- a/src/use-swr-infinite.ts +++ b/src/use-swr-infinite.ts @@ -7,51 +7,33 @@ import useSWR from './use-swr' import { keyType, fetcherFn, - SWRConfiguration, - responseInterface + SWRInfiniteConfiguration, + SWRInfiniteResponse } from './types' type KeyLoader = ( index: number, previousPageData: Data | null ) => keyType -type SWRInfiniteConfigInterface = SWRConfiguration< - Data[], - Error, - fetcherFn -> & { - initialSize?: number - revalidateAll?: boolean - persistSize?: boolean -} -type SWRInfiniteResponseInterface = responseInterface< - Data[], - Error -> & { - size: number - setSize: ( - size: number | ((size: number) => number) - ) => Promise -} function useSWRInfinite( getKey: KeyLoader -): SWRInfiniteResponseInterface +): SWRInfiniteResponse function useSWRInfinite( getKey: KeyLoader, - config?: Partial> -): SWRInfiniteResponseInterface + config?: Partial> +): SWRInfiniteResponse function useSWRInfinite( getKey: KeyLoader, fn?: fetcherFn, - config?: Partial> -): SWRInfiniteResponseInterface + config?: Partial> +): SWRInfiniteResponse function useSWRInfinite( getKey: KeyLoader, ...options: any[] -): SWRInfiniteResponseInterface { +): SWRInfiniteResponse { let _fn: fetcherFn | undefined, - _config: Partial> = {} + _config: Partial> = {} if (options.length > 1) { _fn = options[0] @@ -64,7 +46,7 @@ function useSWRInfinite( } } - const config: SWRInfiniteConfigInterface = Object.assign( + const config: SWRInfiniteConfiguration = Object.assign( {}, defaultConfig, useContext(SWRConfigContext), @@ -246,11 +228,7 @@ function useSWRInfinite( enumerable: true } }) - return swrInfinite as SWRInfiniteResponseInterface + return swrInfinite as SWRInfiniteResponse } -export { - useSWRInfinite, - SWRInfiniteConfigInterface, - SWRInfiniteResponseInterface -} +export { useSWRInfinite } From a7c9ca649e3bc2bb4b4a06cdc00cecd563ece88f Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 03:55:15 +0800 Subject: [PATCH 3/8] deprecate CacheInterface --- src/cache.ts | 4 +-- src/index.ts | 10 +++---- src/types.ts | 73 +++++++++++++++++++++++++++++++--------------------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index 027ab1b03..ca2a18dc1 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,7 +1,7 @@ -import { CacheInterface, keyInterface, cacheListener } from './types' +import { Cache as CacheType, keyInterface, cacheListener } from './types' import hash from './libs/hash' -export default class Cache implements CacheInterface { +export default class Cache implements CacheType { private __cache: Map private __listeners: cacheListener[] diff --git a/src/index.ts b/src/index.ts index d1153da14..3ceb1c81c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,11 +11,6 @@ export { cache } from './config' // Types export { - // Legacy - ConfigInterface, - SWRInfiniteConfigInterface, - SWRInfiniteResponseInterface, - // Latest SWRConfiguration, SWRInfiniteConfiguration, SWRInfiniteResponse, @@ -23,5 +18,10 @@ export { RevalidateOptionInterface, keyInterface, responseInterface, + Cache, + // Legacy, for backwards compatibility + ConfigInterface, + SWRInfiniteConfigInterface, + SWRInfiniteResponseInterface, CacheInterface } from './types' diff --git a/src/types.ts b/src/types.ts index d7aa4a3ab..ee6a190ae 100644 --- a/src/types.ts +++ b/src/types.ts @@ -99,26 +99,10 @@ export type actionType = { isValidating?: boolean } -export interface CacheInterface { - get(key: keyInterface): any - set(key: keyInterface, value: any): any - keys(): string[] - has(key: keyInterface): boolean - delete(key: keyInterface): void - clear(): void - serializeKey(key: keyInterface): [string, any, string, string] - subscribe(listener: cacheListener): () => void -} - export type cacheListener = () => void // Public types -export type SWRConfiguration< - Data = any, - Error = any, - Fn extends fetcherFn = fetcherFn -> = Partial> /** * @deprecated `ConfigInterface` will be renamed to `SWRConfiguration`. */ @@ -127,8 +111,16 @@ export type ConfigInterface< Error = any, Fn extends fetcherFn = fetcherFn > = Partial> +export type SWRConfiguration< + Data = any, + Error = any, + Fn extends fetcherFn = fetcherFn +> = Partial> -export type SWRInfiniteConfiguration< +/** + * @deprecated `SWRInfiniteConfigInterface` will be renamed to `SWRInfiniteConfiguration`. + */ +export type SWRInfiniteConfigInterface< Data = any, Error = any > = SWRConfiguration> & { @@ -136,10 +128,7 @@ export type SWRInfiniteConfiguration< revalidateAll?: boolean persistSize?: boolean } -/** - * @deprecated `SWRInfiniteConfigInterface` will be renamed to `SWRInfiniteConfiguration`. - */ -export type SWRInfiniteConfigInterface< +export type SWRInfiniteConfiguration< Data = any, Error = any > = SWRConfiguration> & { @@ -148,15 +137,6 @@ export type SWRInfiniteConfigInterface< persistSize?: boolean } -export type SWRInfiniteResponse = responseInterface< - Data[], - Error -> & { - size: number - setSize: ( - size: number | ((size: number) => number) - ) => Promise -} /** * @deprecated `SWRInfiniteResponseInterface` will be renamed to `SWRInfiniteResponse`. */ @@ -169,3 +149,36 @@ export type SWRInfiniteResponseInterface< size: number | ((size: number) => number) ) => Promise } +export type SWRInfiniteResponse = responseInterface< + Data[], + Error +> & { + size: number + setSize: ( + size: number | ((size: number) => number) + ) => Promise +} + +/** + * @deprecated `CacheInterface` will be renamed to `Cache`. + */ +export interface CacheInterface { + get(key: keyInterface): any + set(key: keyInterface, value: any): any + keys(): string[] + has(key: keyInterface): boolean + delete(key: keyInterface): void + clear(): void + serializeKey(key: keyInterface): [string, any, string, string] + subscribe(listener: cacheListener): () => void +} +export interface Cache { + get(key: keyInterface): any + set(key: keyInterface, value: any): any + keys(): string[] + has(key: keyInterface): boolean + delete(key: keyInterface): void + clear(): void + serializeKey(key: keyInterface): [string, any, string, string] + subscribe(listener: cacheListener): () => void +} From 9df4259c41c518278180fd062669264d30ed1e04 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 04:00:14 +0800 Subject: [PATCH 4/8] deprecate RevalidateOptionInterface and keyInterface --- src/cache.ts | 12 ++++++------ src/config.ts | 8 ++------ src/index.ts | 8 +++++--- src/types.ts | 53 +++++++++++++++++++++++++++++++------------------- src/use-swr.ts | 16 +++++++-------- 5 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index ca2a18dc1..7814d8fd3 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,4 +1,4 @@ -import { Cache as CacheType, keyInterface, cacheListener } from './types' +import { Cache as CacheType, Key, cacheListener } from './types' import hash from './libs/hash' export default class Cache implements CacheType { @@ -10,12 +10,12 @@ export default class Cache implements CacheType { this.__listeners = [] } - get(key: keyInterface): any { + get(key: Key): any { const [_key] = this.serializeKey(key) return this.__cache.get(_key) } - set(key: keyInterface, value: any): any { + set(key: Key, value: any): any { const [_key] = this.serializeKey(key) this.__cache.set(_key, value) this.notify() @@ -25,7 +25,7 @@ export default class Cache implements CacheType { return Array.from(this.__cache.keys()) } - has(key: keyInterface) { + has(key: Key) { const [_key] = this.serializeKey(key) return this.__cache.has(_key) } @@ -35,14 +35,14 @@ export default class Cache implements CacheType { this.notify() } - delete(key: keyInterface) { + delete(key: Key) { const [_key] = this.serializeKey(key) this.__cache.delete(_key) this.notify() } // TODO: introduce namespace for the cache - serializeKey(key: keyInterface): [string, any, string, string] { + serializeKey(key: Key): [string, any, string, string] { let args = null if (typeof key === 'function') { try { diff --git a/src/config.ts b/src/config.ts index 506f7873b..4d3816f6c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,9 +1,5 @@ import { dequal } from 'dequal/lite' -import { - SWRConfiguration, - RevalidateOptionInterface, - revalidateType -} from './types' +import { SWRConfiguration, RevalidateOptions, revalidateType } from './types' import Cache from './cache' import webPreset from './libs/web-preset' @@ -16,7 +12,7 @@ function onErrorRetry( __, config: SWRConfiguration, revalidate: revalidateType, - opts: RevalidateOptionInterface + opts: RevalidateOptions ): void { if (!config.isDocumentVisible()) { // if it's hidden, stop diff --git a/src/index.ts b/src/index.ts index 3ceb1c81c..393740dbc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,13 +15,15 @@ export { SWRInfiniteConfiguration, SWRInfiniteResponse, revalidateType, - RevalidateOptionInterface, - keyInterface, + RevalidateOptions, + Key, responseInterface, Cache, // Legacy, for backwards compatibility ConfigInterface, SWRInfiniteConfigInterface, SWRInfiniteResponseInterface, - CacheInterface + RevalidateOptionInterface, + CacheInterface, + keyInterface } from './types' diff --git a/src/types.ts b/src/types.ts index ee6a190ae..fb1dce009 100644 --- a/src/types.ts +++ b/src/types.ts @@ -38,7 +38,7 @@ export type Configuration< key: string, config: Configuration, revalidate: revalidateType, - revalidateOpts: RevalidateOptionInterface + revalidateOpts: RevalidateOptions ) => void registerOnFocus?: (cb: () => void) => void registerOnReconnect?: (cb: () => void) => void @@ -46,14 +46,9 @@ export type Configuration< compare: (a: Data | undefined, b: Data | undefined) => boolean } -export interface RevalidateOptionInterface { - retryCount?: number - dedupe?: boolean -} - export type keyType = string | any[] | null type keyFunction = () => keyType -export type keyInterface = keyFunction | keyType + export type updaterInterface = ( shouldRevalidate?: boolean, data?: Data, @@ -62,14 +57,14 @@ export type updaterInterface = ( dedupe?: boolean ) => boolean | Promise export type triggerInterface = ( - key: keyInterface, + key: Key, shouldRevalidate?: boolean ) => Promise export type mutateCallback = ( currentValue: undefined | Data ) => Promise | undefined | Data export type mutateInterface = ( - key: keyInterface, + key: Key, data?: Data | Promise | mutateCallback, shouldRevalidate?: boolean ) => Promise @@ -90,7 +85,7 @@ export type responseInterface = { isValidating: boolean } export type revalidateType = ( - revalidateOpts: RevalidateOptionInterface + revalidateOpts: RevalidateOptions ) => Promise export type actionType = { @@ -163,22 +158,40 @@ export type SWRInfiniteResponse = responseInterface< * @deprecated `CacheInterface` will be renamed to `Cache`. */ export interface CacheInterface { - get(key: keyInterface): any - set(key: keyInterface, value: any): any + get(key: Key): any + set(key: Key, value: any): any keys(): string[] - has(key: keyInterface): boolean - delete(key: keyInterface): void + has(key: Key): boolean + delete(key: Key): void clear(): void - serializeKey(key: keyInterface): [string, any, string, string] + serializeKey(key: Key): [string, any, string, string] subscribe(listener: cacheListener): () => void } export interface Cache { - get(key: keyInterface): any - set(key: keyInterface, value: any): any + get(key: Key): any + set(key: Key, value: any): any keys(): string[] - has(key: keyInterface): boolean - delete(key: keyInterface): void + has(key: Key): boolean + delete(key: Key): void clear(): void - serializeKey(key: keyInterface): [string, any, string, string] + serializeKey(key: Key): [string, any, string, string] subscribe(listener: cacheListener): () => void } + +/** + * @deprecated `RevalidateOptionInterface` will be renamed to `RevalidateOptions`. + */ +export interface RevalidateOptionInterface { + retryCount?: number + dedupe?: boolean +} +export interface RevalidateOptions { + retryCount?: number + dedupe?: boolean +} + +/** + * @deprecated `keyInterface` will be renamed to `Key`. + */ +export type keyInterface = keyFunction | keyType +export type Key = keyFunction | keyType diff --git a/src/use-swr.ts b/src/use-swr.ts index b2f4846f9..e2a0a4f0b 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -17,10 +17,10 @@ import { Configuration, SWRConfiguration, fetcherFn, - keyInterface, + Key, mutateInterface, responseInterface, - RevalidateOptionInterface, + RevalidateOptions, triggerInterface, updaterInterface } from './types' @@ -214,21 +214,21 @@ const mutate: mutateInterface = async ( } function useSWR( - key: keyInterface + key: Key ): responseInterface function useSWR( - key: keyInterface, + key: Key, config?: SWRConfiguration ): responseInterface function useSWR( - key: keyInterface, + key: Key, // `null` is used for a hack to manage shared state with SWR // https://github.com/vercel/swr/pull/918 fn?: fetcherFn | null, config?: SWRConfiguration ): responseInterface function useSWR( - _key: keyInterface, + _key: Key, ...options: any[] ): responseInterface { let _fn: fetcherFn | undefined, @@ -377,9 +377,7 @@ function useSWR( // start a revalidation const revalidate = useCallback( - async ( - revalidateOpts: RevalidateOptionInterface = {} - ): Promise => { + async (revalidateOpts: RevalidateOptions = {}): Promise => { if (!key || !fn) return false if (unmountedRef.current) return false if (configRef.current.isPaused()) return false From 06d24109976629f1e14dbc996b63901d55af25cf Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 04:05:11 +0800 Subject: [PATCH 5/8] deprecate revalidateType --- src/config.ts | 4 ++-- src/index.ts | 7 ++++--- src/types.ts | 13 ++++++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/config.ts b/src/config.ts index 4d3816f6c..b05cf98cd 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,5 @@ import { dequal } from 'dequal/lite' -import { SWRConfiguration, RevalidateOptions, revalidateType } from './types' +import { SWRConfiguration, RevalidateOptions, Revalidator } from './types' import Cache from './cache' import webPreset from './libs/web-preset' @@ -11,7 +11,7 @@ function onErrorRetry( _, __, config: SWRConfiguration, - revalidate: revalidateType, + revalidate: Revalidator, opts: RevalidateOptions ): void { if (!config.isDocumentVisible()) { diff --git a/src/index.ts b/src/index.ts index 393740dbc..4cadc6299 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ export { SWRConfiguration, SWRInfiniteConfiguration, SWRInfiniteResponse, - revalidateType, + Revalidator, RevalidateOptions, Key, responseInterface, @@ -23,7 +23,8 @@ export { ConfigInterface, SWRInfiniteConfigInterface, SWRInfiniteResponseInterface, + revalidateType, RevalidateOptionInterface, - CacheInterface, - keyInterface + keyInterface, + CacheInterface } from './types' diff --git a/src/types.ts b/src/types.ts index fb1dce009..ab3d48b9f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -84,9 +84,6 @@ export type responseInterface = { ) => Promise isValidating: boolean } -export type revalidateType = ( - revalidateOpts: RevalidateOptions -) => Promise export type actionType = { data?: Data @@ -195,3 +192,13 @@ export interface RevalidateOptions { */ export type keyInterface = keyFunction | keyType export type Key = keyFunction | keyType + +/** + * @deprecated `revalidateType` will be renamed to `Revalidator`. + */ +export type revalidateType = ( + revalidateOpts: RevalidateOptions +) => Promise +export type Revalidator = ( + revalidateOpts: RevalidateOptions +) => Promise From 06df0a47e9b3df5524a12b23425776f825626585 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 04:09:00 +0800 Subject: [PATCH 6/8] deprecate responseInterface --- src/index.ts | 3 ++- src/types.ts | 46 ++++++++++++++++++++++++++++++---------------- src/use-swr.ts | 16 +++++++--------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4cadc6299..8c4eea715 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ export { Revalidator, RevalidateOptions, Key, - responseInterface, + SWRResponse, Cache, // Legacy, for backwards compatibility ConfigInterface, @@ -26,5 +26,6 @@ export { revalidateType, RevalidateOptionInterface, keyInterface, + responseInterface, CacheInterface } from './types' diff --git a/src/types.ts b/src/types.ts index ab3d48b9f..a9e62c862 100644 --- a/src/types.ts +++ b/src/types.ts @@ -37,7 +37,7 @@ export type Configuration< err: Error, key: string, config: Configuration, - revalidate: revalidateType, + revalidate: Revalidator, revalidateOpts: RevalidateOptions ) => void registerOnFocus?: (cb: () => void) => void @@ -74,16 +74,6 @@ export type broadcastStateInterface = ( error?: Error, isValidating?: boolean ) => void -export type responseInterface = { - data?: Data - error?: Error - revalidate: () => Promise - mutate: ( - data?: Data | Promise | mutateCallback, - shouldRevalidate?: boolean - ) => Promise - isValidating: boolean -} export type actionType = { data?: Data @@ -132,16 +122,16 @@ export type SWRInfiniteConfiguration< /** * @deprecated `SWRInfiniteResponseInterface` will be renamed to `SWRInfiniteResponse`. */ -export type SWRInfiniteResponseInterface< - Data = any, - Error = any -> = responseInterface & { +export type SWRInfiniteResponseInterface = SWRResponse< + Data[], + Error +> & { size: number setSize: ( size: number | ((size: number) => number) ) => Promise } -export type SWRInfiniteResponse = responseInterface< +export type SWRInfiniteResponse = SWRResponse< Data[], Error > & { @@ -202,3 +192,27 @@ export type revalidateType = ( export type Revalidator = ( revalidateOpts: RevalidateOptions ) => Promise + +/** + * @deprecated `responseInterface` will be renamed to `SWRResponse`. + */ +export type responseInterface = { + data?: Data + error?: Error + revalidate: () => Promise + mutate: ( + data?: Data | Promise | mutateCallback, + shouldRevalidate?: boolean + ) => Promise + isValidating: boolean +} +export type SWRResponse = { + data?: Data + error?: Error + revalidate: () => Promise + mutate: ( + data?: Data | Promise | mutateCallback, + shouldRevalidate?: boolean + ) => Promise + isValidating: boolean +} diff --git a/src/use-swr.ts b/src/use-swr.ts index e2a0a4f0b..26df90d26 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -19,7 +19,7 @@ import { fetcherFn, Key, mutateInterface, - responseInterface, + SWRResponse, RevalidateOptions, triggerInterface, updaterInterface @@ -213,24 +213,22 @@ const mutate: mutateInterface = async ( return data } -function useSWR( - key: Key -): responseInterface +function useSWR(key: Key): SWRResponse function useSWR( key: Key, config?: SWRConfiguration -): responseInterface +): SWRResponse function useSWR( key: Key, // `null` is used for a hack to manage shared state with SWR // https://github.com/vercel/swr/pull/918 fn?: fetcherFn | null, config?: SWRConfiguration -): responseInterface +): SWRResponse function useSWR( _key: Key, ...options: any[] -): responseInterface { +): SWRResponse { let _fn: fetcherFn | undefined, _config: SWRConfiguration = {} if (options.length > 1) { @@ -346,7 +344,7 @@ function useSWR( [key] ) - const boundMutate: responseInterface['mutate'] = useCallback( + const boundMutate: SWRResponse['mutate'] = useCallback( (data, shouldRevalidate) => { return mutate(keyRef.current, data, shouldRevalidate) }, @@ -769,7 +767,7 @@ function useSWR( // revalidate will be deprecated in the 1.x release // because mutate() covers the same use case of revalidate(). // This remains only for backward compatibility - const state = { revalidate, mutate: boundMutate } as responseInterface< + const state = { revalidate, mutate: boundMutate } as SWRResponse< Data, Error > From 6a635a9739ac10d67c2484a6b0afe9b3b9867b36 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 04:11:19 +0800 Subject: [PATCH 7/8] reorder --- src/config.ts | 4 +- src/index.ts | 2 +- src/types.ts | 106 ++++++++++++++++++++++++------------------------- src/use-swr.ts | 4 +- 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/config.ts b/src/config.ts index b05cf98cd..31cc5dec7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,5 @@ import { dequal } from 'dequal/lite' -import { SWRConfiguration, RevalidateOptions, Revalidator } from './types' +import { SWRConfiguration, RevalidatorOptions, Revalidator } from './types' import Cache from './cache' import webPreset from './libs/web-preset' @@ -12,7 +12,7 @@ function onErrorRetry( __, config: SWRConfiguration, revalidate: Revalidator, - opts: RevalidateOptions + opts: RevalidatorOptions ): void { if (!config.isDocumentVisible()) { // if it's hidden, stop diff --git a/src/index.ts b/src/index.ts index 8c4eea715..fa36ab725 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,7 @@ export { SWRInfiniteConfiguration, SWRInfiniteResponse, Revalidator, - RevalidateOptions, + RevalidatorOptions, Key, SWRResponse, Cache, diff --git a/src/types.ts b/src/types.ts index a9e62c862..c37baf06d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -38,7 +38,7 @@ export type Configuration< key: string, config: Configuration, revalidate: Revalidator, - revalidateOpts: RevalidateOptions + revalidateOpts: RevalidatorOptions ) => void registerOnFocus?: (cb: () => void) => void registerOnReconnect?: (cb: () => void) => void @@ -99,6 +99,36 @@ export type SWRConfiguration< Fn extends fetcherFn = fetcherFn > = Partial> +/** + * @deprecated `keyInterface` will be renamed to `Key`. + */ +export type keyInterface = keyFunction | keyType +export type Key = keyFunction | keyType + +/** + * @deprecated `responseInterface` will be renamed to `SWRResponse`. + */ +export type responseInterface = { + data?: Data + error?: Error + revalidate: () => Promise + mutate: ( + data?: Data | Promise | mutateCallback, + shouldRevalidate?: boolean + ) => Promise + isValidating: boolean +} +export type SWRResponse = { + data?: Data + error?: Error + revalidate: () => Promise + mutate: ( + data?: Data | Promise | mutateCallback, + shouldRevalidate?: boolean + ) => Promise + isValidating: boolean +} + /** * @deprecated `SWRInfiniteConfigInterface` will be renamed to `SWRInfiniteConfiguration`. */ @@ -141,6 +171,28 @@ export type SWRInfiniteResponse = SWRResponse< ) => Promise } +/** + * @deprecated `RevalidateOptionInterface` will be renamed to `RevalidatorOptions`. + */ +export interface RevalidateOptionInterface { + retryCount?: number + dedupe?: boolean +} +export interface RevalidatorOptions { + retryCount?: number + dedupe?: boolean +} + +/** + * @deprecated `revalidateType` will be renamed to `Revalidator`. + */ +export type revalidateType = ( + revalidateOpts: RevalidatorOptions +) => Promise +export type Revalidator = ( + revalidateOpts: RevalidatorOptions +) => Promise + /** * @deprecated `CacheInterface` will be renamed to `Cache`. */ @@ -164,55 +216,3 @@ export interface Cache { serializeKey(key: Key): [string, any, string, string] subscribe(listener: cacheListener): () => void } - -/** - * @deprecated `RevalidateOptionInterface` will be renamed to `RevalidateOptions`. - */ -export interface RevalidateOptionInterface { - retryCount?: number - dedupe?: boolean -} -export interface RevalidateOptions { - retryCount?: number - dedupe?: boolean -} - -/** - * @deprecated `keyInterface` will be renamed to `Key`. - */ -export type keyInterface = keyFunction | keyType -export type Key = keyFunction | keyType - -/** - * @deprecated `revalidateType` will be renamed to `Revalidator`. - */ -export type revalidateType = ( - revalidateOpts: RevalidateOptions -) => Promise -export type Revalidator = ( - revalidateOpts: RevalidateOptions -) => Promise - -/** - * @deprecated `responseInterface` will be renamed to `SWRResponse`. - */ -export type responseInterface = { - data?: Data - error?: Error - revalidate: () => Promise - mutate: ( - data?: Data | Promise | mutateCallback, - shouldRevalidate?: boolean - ) => Promise - isValidating: boolean -} -export type SWRResponse = { - data?: Data - error?: Error - revalidate: () => Promise - mutate: ( - data?: Data | Promise | mutateCallback, - shouldRevalidate?: boolean - ) => Promise - isValidating: boolean -} diff --git a/src/use-swr.ts b/src/use-swr.ts index 26df90d26..7c4cb2539 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -20,7 +20,7 @@ import { Key, mutateInterface, SWRResponse, - RevalidateOptions, + RevalidatorOptions, triggerInterface, updaterInterface } from './types' @@ -375,7 +375,7 @@ function useSWR( // start a revalidation const revalidate = useCallback( - async (revalidateOpts: RevalidateOptions = {}): Promise => { + async (revalidateOpts: RevalidatorOptions = {}): Promise => { if (!key || !fn) return false if (unmountedRef.current) return false if (configRef.current.isPaused()) return false From 4d9a0bf530952f97f8e2818a63d2e93df081d526 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 7 Mar 2021 04:21:31 +0800 Subject: [PATCH 8/8] clean up internal types --- src/cache.ts | 6 +++--- src/types.ts | 48 ++++++++++++++++++++--------------------- src/use-swr-infinite.ts | 10 ++++----- src/use-swr.ts | 39 +++++++++++++-------------------- 4 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index 7814d8fd3..8561f5af8 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,9 +1,9 @@ -import { Cache as CacheType, Key, cacheListener } from './types' +import { Cache as CacheType, Key, CacheListener } from './types' import hash from './libs/hash' export default class Cache implements CacheType { private __cache: Map - private __listeners: cacheListener[] + private __listeners: CacheListener[] constructor(initialData: any = {}) { this.__cache = new Map(Object.entries(initialData)) @@ -68,7 +68,7 @@ export default class Cache implements CacheType { return [key, args, errorKey, isValidatingKey] } - subscribe(listener: cacheListener) { + subscribe(listener: CacheListener) { if (typeof listener !== 'function') { throw new Error('Expected the listener to be a function.') } diff --git a/src/types.ts b/src/types.ts index c37baf06d..b31c86d1c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,11 +1,11 @@ // Internal types -export type fetcherFn = (...args: any) => Data | Promise +export type Fetcher = (...args: any) => Data | Promise export type Configuration< Data = any, Error = any, - Fn extends fetcherFn = fetcherFn + Fn extends Fetcher = Fetcher > = { errorRetryInterval: number errorRetryCount?: number @@ -46,42 +46,40 @@ export type Configuration< compare: (a: Data | undefined, b: Data | undefined) => boolean } -export type keyType = string | any[] | null -type keyFunction = () => keyType +export type ValueKey = string | any[] | null -export type updaterInterface = ( +export type Updater = ( shouldRevalidate?: boolean, data?: Data, error?: Error, shouldDedupe?: boolean, dedupe?: boolean ) => boolean | Promise -export type triggerInterface = ( - key: Key, - shouldRevalidate?: boolean -) => Promise -export type mutateCallback = ( +export type Trigger = (key: Key, shouldRevalidate?: boolean) => Promise + +type MutatorCallback = ( currentValue: undefined | Data ) => Promise | undefined | Data -export type mutateInterface = ( + +export type Mutator = ( key: Key, - data?: Data | Promise | mutateCallback, + data?: Data | Promise | MutatorCallback, shouldRevalidate?: boolean ) => Promise -export type broadcastStateInterface = ( +export type Broadcaster = ( key: string, data: Data, error?: Error, isValidating?: boolean ) => void -export type actionType = { +export type Action = { data?: Data error?: Error isValidating?: boolean } -export type cacheListener = () => void +export type CacheListener = () => void // Public types @@ -91,19 +89,19 @@ export type cacheListener = () => void export type ConfigInterface< Data = any, Error = any, - Fn extends fetcherFn = fetcherFn + Fn extends Fetcher = Fetcher > = Partial> export type SWRConfiguration< Data = any, Error = any, - Fn extends fetcherFn = fetcherFn + Fn extends Fetcher = Fetcher > = Partial> /** * @deprecated `keyInterface` will be renamed to `Key`. */ -export type keyInterface = keyFunction | keyType -export type Key = keyFunction | keyType +export type keyInterface = ValueKey | (() => ValueKey) +export type Key = ValueKey | (() => ValueKey) /** * @deprecated `responseInterface` will be renamed to `SWRResponse`. @@ -113,7 +111,7 @@ export type responseInterface = { error?: Error revalidate: () => Promise mutate: ( - data?: Data | Promise | mutateCallback, + data?: Data | Promise | MutatorCallback, shouldRevalidate?: boolean ) => Promise isValidating: boolean @@ -123,7 +121,7 @@ export type SWRResponse = { error?: Error revalidate: () => Promise mutate: ( - data?: Data | Promise | mutateCallback, + data?: Data | Promise | MutatorCallback, shouldRevalidate?: boolean ) => Promise isValidating: boolean @@ -135,7 +133,7 @@ export type SWRResponse = { export type SWRInfiniteConfigInterface< Data = any, Error = any -> = SWRConfiguration> & { +> = SWRConfiguration> & { initialSize?: number revalidateAll?: boolean persistSize?: boolean @@ -143,7 +141,7 @@ export type SWRInfiniteConfigInterface< export type SWRInfiniteConfiguration< Data = any, Error = any -> = SWRConfiguration> & { +> = SWRConfiguration> & { initialSize?: number revalidateAll?: boolean persistSize?: boolean @@ -204,7 +202,7 @@ export interface CacheInterface { delete(key: Key): void clear(): void serializeKey(key: Key): [string, any, string, string] - subscribe(listener: cacheListener): () => void + subscribe(listener: CacheListener): () => void } export interface Cache { get(key: Key): any @@ -214,5 +212,5 @@ export interface Cache { delete(key: Key): void clear(): void serializeKey(key: Key): [string, any, string, string] - subscribe(listener: cacheListener): () => void + subscribe(listener: CacheListener): () => void } diff --git a/src/use-swr-infinite.ts b/src/use-swr-infinite.ts index f2b15824b..e0277cd08 100644 --- a/src/use-swr-infinite.ts +++ b/src/use-swr-infinite.ts @@ -5,8 +5,8 @@ import SWRConfigContext from './swr-config-context' import useSWR from './use-swr' import { - keyType, - fetcherFn, + ValueKey, + Fetcher, SWRInfiniteConfiguration, SWRInfiniteResponse } from './types' @@ -14,7 +14,7 @@ import { type KeyLoader = ( index: number, previousPageData: Data | null -) => keyType +) => ValueKey function useSWRInfinite( getKey: KeyLoader @@ -25,14 +25,14 @@ function useSWRInfinite( ): SWRInfiniteResponse function useSWRInfinite( getKey: KeyLoader, - fn?: fetcherFn, + fn?: Fetcher, config?: Partial> ): SWRInfiniteResponse function useSWRInfinite( getKey: KeyLoader, ...options: any[] ): SWRInfiniteResponse { - let _fn: fetcherFn | undefined, + let _fn: Fetcher | undefined, _config: Partial> = {} if (options.length > 1) { diff --git a/src/use-swr.ts b/src/use-swr.ts index 7c4cb2539..c065960e2 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -12,17 +12,17 @@ import { import defaultConfig, { cache } from './config' import SWRConfigContext from './swr-config-context' import { - actionType, - broadcastStateInterface, + Action, + Broadcaster, Configuration, SWRConfiguration, - fetcherFn, + Fetcher, Key, - mutateInterface, + Mutator, SWRResponse, RevalidatorOptions, - triggerInterface, - updaterInterface + Trigger, + Updater } from './types' const IS_SERVER = @@ -74,7 +74,7 @@ if (!IS_SERVER) { } } -const trigger: triggerInterface = (_key, shouldRevalidate = true) => { +const trigger: Trigger = (_key, shouldRevalidate = true) => { // we are ignoring the second argument which correspond to the arguments // the fetcher will receive when key is an array const [key, , keyErr, keyValidating] = cache.serializeKey(_key) @@ -104,12 +104,7 @@ const trigger: triggerInterface = (_key, shouldRevalidate = true) => { return Promise.resolve(cache.get(key)) } -const broadcastState: broadcastStateInterface = ( - key, - data, - error, - isValidating -) => { +const broadcastState: Broadcaster = (key, data, error, isValidating) => { const updaters = CACHE_REVALIDATORS[key] if (key && updaters) { for (let i = 0; i < updaters.length; ++i) { @@ -118,11 +113,7 @@ const broadcastState: broadcastStateInterface = ( } } -const mutate: mutateInterface = async ( - _key, - _data, - shouldRevalidate = true -) => { +const mutate: Mutator = async (_key, _data, shouldRevalidate = true) => { const [key, , keyErr] = cache.serializeKey(_key) if (!key) return @@ -222,14 +213,14 @@ function useSWR( key: Key, // `null` is used for a hack to manage shared state with SWR // https://github.com/vercel/swr/pull/918 - fn?: fetcherFn | null, + fn?: Fetcher | null, config?: SWRConfiguration ): SWRResponse function useSWR( _key: Key, ...options: any[] ): SWRResponse { - let _fn: fetcherFn | undefined, + let _fn: Fetcher | undefined, _config: SWRConfiguration = {} if (options.length > 1) { _fn = options[0] @@ -301,7 +292,7 @@ function useSWR( const [, rerender] = useState(null) let dispatch = useCallback( - (payload: actionType) => { + (payload: Action) => { let shouldUpdateState = false for (let k in payload) { if (stateRef.current[k] === payload[k]) { @@ -476,7 +467,7 @@ function useSWR( cache.set(keyValidating, false) // new state for the reducer - const newState: actionType = { + const newState: Action = { isValidating: false } @@ -613,7 +604,7 @@ function useSWR( } // register global cache update listener - const onUpdate: updaterInterface = ( + const onUpdate: Updater = ( shouldRevalidate = true, updatedData, updatedError, @@ -621,7 +612,7 @@ function useSWR( dedupe = true ) => { // update hook state - const newState: actionType = {} + const newState: Action = {} let needUpdate = false if (