Skip to content

Commit

Permalink
Revert "mark data and error as readonly (#1514)" (#1570)
Browse files Browse the repository at this point in the history
This reverts commit af6353c.
  • Loading branch information
shuding committed Oct 29, 2021
1 parent 62ad926 commit e2b6476
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 67 deletions.
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 @@ -149,8 +148,8 @@ export type Arguments =
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 @@ -167,35 +166,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 @@ -206,10 +202,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

0 comments on commit e2b6476

Please sign in to comment.