Skip to content

Code refactoring #1077

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

Merged
merged 2 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
41 changes: 41 additions & 0 deletions src/resolve-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useContext } from 'react'

import defaultConfig from './config'
import SWRConfigContext from './config-context'

import { Fetcher } from './types'

// Resolve arguments for SWR hooks.
// This function itself is a hook because it uses `useContext` inside.
export default function useArgs<KeyType, ConfigType, Data>(
args:
| readonly [KeyType]
| readonly [KeyType, Fetcher<Data> | null]
| readonly [KeyType, ConfigType | undefined]
| readonly [KeyType, Fetcher<Data> | null, ConfigType | undefined]
): [KeyType, (typeof defaultConfig) & ConfigType, Fetcher<Data> | null] {
const config = Object.assign(
{},
defaultConfig,
useContext(SWRConfigContext),
args.length > 2
? args[2]
: args.length === 2 && typeof args[1] === 'object'
? args[1]
: {}
) as (typeof defaultConfig) & ConfigType

// In TypeScript `args.length > 2` is not same as `args.lenth === 3`.
// We do a safe type assertion here.
const fn = (args.length > 2
? args[1]
: args.length === 2 && typeof args[1] === 'function'
? args[1]
: // Pass fn as null will disable revalidate
// https://paco.sh/blog/shared-hook-state-with-swr
args[1] === null
? args[1]
: config.fetcher) as Fetcher<Data> | null

return [args[0], config, fn]
}
37 changes: 11 additions & 26 deletions src/use-swr-infinite.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// TODO: use @ts-expect-error
import { useContext, useRef, useState, useCallback } from 'react'
import { useRef, useState, useCallback } from 'react'

import defaultConfig, { cache } from './config'
import { cache } from './config'
import { useIsomorphicLayoutEffect } from './env'
import SWRConfigContext from './swr-config-context'
import useArgs from './resolve-args'
import useSWR from './use-swr'

import {
Expand All @@ -17,37 +17,22 @@ import {
function useSWRInfinite<Data = any, Error = any>(
...args:
| readonly [KeyLoader<Data>]
| readonly [KeyLoader<Data>, Fetcher<Data>]
| readonly [KeyLoader<Data>, Fetcher<Data> | null]
| readonly [
KeyLoader<Data>,
SWRInfiniteConfiguration<Data, Error> | undefined
]
| readonly [
KeyLoader<Data>,
Fetcher<Data>,
Fetcher<Data> | null,
SWRInfiniteConfiguration<Data, Error> | undefined
]
): SWRInfiniteResponse<Data, Error> {
const getKey = args[0]

const config = Object.assign(
{},
defaultConfig,
useContext(SWRConfigContext),
args.length > 2
? args[2]
: args.length === 2 && typeof args[1] === 'object'
? args[1]
: {}
)
// in typescript args.length > 2 is not same as args.lenth === 3
// we do a safe type assertion here
// args.length === 3
const fn = (args.length > 2
? args[1]
: args.length === 2 && typeof args[1] === 'function'
? args[1]
: config.fetcher) as Fetcher<Data>
const [getKey, config, fn] = useArgs<
KeyLoader<Data>,
SWRInfiniteConfiguration<Data, Error>,
Data
>(args)

const {
initialSize = 1,
Expand Down Expand Up @@ -145,7 +130,7 @@ function useSWRInfinite<Data = any, Error = any>(
typeof dataRef.current !== 'undefined') ||
(originalData && !config.compare(originalData[i], pageData))

if (shouldFetchPage) {
if (fn && shouldFetchPage) {
if (pageArgs !== null) {
pageData = await fn(...pageArgs)
} else {
Expand Down
32 changes: 5 additions & 27 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// TODO: use @ts-expect-error
import { useCallback, useContext, useRef, useDebugValue } from 'react'
import { useCallback, useRef, useDebugValue } from 'react'

import defaultConfig, { cache } from './config'
import { IS_SERVER, rAF, useIsomorphicLayoutEffect } from './env'
import SWRConfigContext from './swr-config-context'
import SWRConfigContext from './config-context'
import useStateWithDeps from './state'
import useArgs from './resolve-args'

import {
State,
Expand Down Expand Up @@ -223,33 +224,10 @@ function useSWR<Data = any, Error = any>(
SWRConfiguration<Data, Error> | undefined
]
): SWRResponse<Data, Error> {
// Resolve arguments
const _key = args[0]
const config = Object.assign(
{},
defaultConfig,
useContext(SWRConfigContext),
args.length > 2
? args[2]
: args.length === 2 && typeof args[1] === 'object'
? args[1]
: {}
const [_key, config, fn] = useArgs<Key, SWRConfiguration<Data, Error>, Data>(
args
)

// In TypeScript `args.length > 2` is not same as `args.lenth === 3`.
// We do a safe type assertion here.
const fn = (args.length > 2
? args[1]
: args.length === 2 && typeof args[1] === 'function'
? args[1]
: /**
* Pass fn as null will disable revalidate
* https://paco.sh/blog/shared-hook-state-with-swr
*/
args[1] === null
? args[1]
: config.fetcher) as Fetcher<Data> | null

// `key` is the identifier of the SWR `data` state.
// `keyErr` and `keyValidating` are indentifiers of `error` and `isValidating`
// which are derived from `key`.
Expand Down