Skip to content
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

Revert "chore: Mark returned states and mutate callback argument as readonly" #1570

Merged
merged 1 commit into from
Oct 29, 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
13 changes: 3 additions & 10 deletions infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const infinite = ((<Data, Error, Args extends Arguments>(
): SWRInfiniteResponse<Data, Error> => {
const rerender = useState({})[1]
const didMountRef = useRef<boolean>(false)
const dataRef = useRef<Readonly<Data[]>>()
const dataRef = useRef<Data[]>()

const {
cache,
Expand Down Expand Up @@ -168,12 +168,7 @@ export const infinite = ((<Data, Error, Args extends Arguments>(

const mutate = useCallback(
(
data?:
| Data[]
| Readonly<Data[]>
| Promise<Data[]>
| Promise<Readonly<Data[]>>
| MutatorCallback<Data[]>,
data: Data[] | undefined | Promise<Data[]> | MutatorCallback<Data[]>,
shouldRevalidate = true
) => {
// It is possible that the key is still falsy.
Expand All @@ -196,9 +191,7 @@ export const infinite = ((<Data, Error, Args extends Arguments>(
)

// Function to load pages data from the cache based on the page size.
const resolvePagesFromCache = (
pageSize: number
): Readonly<Data[]> | undefined => {
const resolvePagesFromCache = (pageSize: number): Data[] | undefined => {
// return an array of page data
const data: Data[] = []

Expand Down
38 changes: 17 additions & 21 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as revalidateEvents from './constants/revalidate-events'

type Async<Data> = Data | Promise<Data>
export type FetcherResponse<Data = unknown> = Async<Data>
export type FetcherResponse<Data = unknown> = Data | Promise<Data>

export type Fetcher<Data = unknown, SWRKey extends Key = Key> =
/**
Expand Down Expand Up @@ -143,8 +142,8 @@ export type Arguments = string | null | ArgumentsTuple | Record<any, any>
export type Key = Arguments | (() => Arguments)

export type MutatorCallback<Data = any> = (
currentValue?: Readonly<Data>
) => Async<undefined | Data | Readonly<Data>>
currentValue?: Data
) => Promise<undefined | Data> | undefined | Data

export type Broadcaster<Data = any, Error = any> = (
cache: Cache<Data>,
Expand All @@ -161,35 +160,32 @@ export type State<Data, Error> = {
isValidating?: boolean
}

type MutatorData<Data> =
| Async<Data>
| Async<Readonly<Data>>
| MutatorCallback<Data>

export type Mutator<Data = any> = (
cache: Cache,
key: Key,
data?: MutatorData<Data>,
data?: Data | Promise<Data> | MutatorCallback<Data>,
shouldRevalidate?: boolean
) => Promise<Readonly<Data> | undefined>
) => Promise<Data | undefined>

export interface ScopedMutator<Data = any> {
/** This is used for bound mutator */
(key: Key, data?: MutatorData<Data>, shouldRevalidate?: boolean): Promise<
Readonly<Data> | undefined
>
(
key: Key,
data?: Data | Promise<Data> | MutatorCallback<Data>,
shouldRevalidate?: boolean
): Promise<Data | undefined>
/** This is used for global mutator */
<T = any>(
key: Key,
data?: MutatorData<T>,
data?: T | Promise<T> | MutatorCallback<T>,
shouldRevalidate?: boolean
): Promise<Readonly<T> | undefined>
): Promise<T | undefined>
}

export type KeyedMutator<Data> = (
data?: MutatorData<Data>,
data?: Data | Promise<Data> | MutatorCallback<Data>,
shouldRevalidate?: boolean
) => Promise<Readonly<Data> | undefined>
) => Promise<Data | undefined>

// Public types

Expand All @@ -200,10 +196,10 @@ export type SWRConfiguration<
> = Partial<PublicConfiguration<Data, Error, SWRKey>>

export interface SWRResponse<Data, Error> {
data?: Readonly<Data>
error?: Readonly<Error>
isValidating: boolean
data?: Data
error?: Error
mutate: KeyedMutator<Data>
isValidating: boolean
}

export type KeyLoader<Args extends Arguments = Arguments> =
Expand Down
35 changes: 0 additions & 35 deletions test/type/state.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('useSWR - local mutation', () => {
const { data: state, mutate: setState } = useSWR(`${baseKey}--${key}`, {
fallbackData
})
return [state, setState] as const
return [state, setState]
}

function Page() {
Expand Down