Skip to content

Commit

Permalink
refactor: use SWRGlobalState to store preloading requests
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Jun 16, 2022
1 parent e25b009 commit 85f160c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions _internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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, FetcherResponse<any>>, // PRELOAD
ScopedMutator, // Mutator
(key: string, value: any, prev: any) => void, // Setter
(key: string, callback: (current: any, prev: any) => void) => () => void // Subscriber
Expand Down
3 changes: 2 additions & 1 deletion _internal/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const initCache = <Data = any>(
EVENT_REVALIDATORS,
{},
{},
{},
mutate,
setter,
subscribe
Expand Down Expand Up @@ -132,5 +133,5 @@ export const initCache = <Data = any>(
return [provider, mutate, initProvider, unmount]
}

return [provider, (SWRGlobalState.get(provider) as GlobalState)[3]]
return [provider, (SWRGlobalState.get(provider) as GlobalState)[4]]
}
4 changes: 2 additions & 2 deletions _internal/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const createCacheHelper = <Data = any, T = State<Data, any>>(
// Setter
(info: T) => {
const prev = cache.get(key)
state[4](key as string, mergeObjects(prev, info), prev || EMPTY_CACHE)
state[5](key as string, mergeObjects(prev, info), prev || EMPTY_CACHE)
},
// Subscriber
state[5]
state[6]
] as const
}
14 changes: 8 additions & 6 deletions _internal/utils/preload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Middleware, Key, BareFetcher, FetcherResponse } from '../types'
import { Middleware, Key, BareFetcher, GlobalState } from '../types'
import { serialize } from './serialize'

const REQUEST = new Map<string, FetcherResponse<any>>()
import { cache } from './config'
import { SWRGlobalState } from './global-state'

export const preload = <Data = any>(key_: Key, fetcher: BareFetcher<Data>) => {
const req = fetcher(key_)
const key = serialize(key_)[0]
REQUEST.set(key, req)
const [, , , PRELOAD] = SWRGlobalState.get(cache) as GlobalState
PRELOAD[key] = req
return req
}

Expand All @@ -17,9 +18,10 @@ export const middleware: Middleware =
fetcher_ &&
((...args: any[]) => {
const key = serialize(key_)[0]
const req = REQUEST.get(key)
const [, , , PRELOAD] = SWRGlobalState.get(cache) as GlobalState
const req = PRELOAD[key]
if (req) {
REQUEST.delete(key)
delete PRELOAD[key]
return req
}
return fetcher_(...args)
Expand Down

0 comments on commit 85f160c

Please sign in to comment.