Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update state in a transition * support snapshotting external state * wip * move types:check to pre-commit * refactor: switch to useSyncExternalStoreWithSelector (#1953) * type: extends useConfig cache interface (#1938) * remove mount check for react18 (#1927) reactwg/react-18#82 related pr #787 #433 * lint: dont check unused vars with underscore prefix (#1939) * test: upgrade to jest 28 (#1942) * Upgrade to jest 28 * Upgrade to jest 28 * feat: useSyncExternalStoreWithSelector * refactor: remove stateUpdate and boardcast state update should be handled by uSESW * type: fix test type error * remove pnpm.lock * fix: import cjs for codesanbox * refactor: add selector * refactor: add cachestate interface and try fix custom cache * fix: custom cache init * refactor: remove useless flag * chore: codesanbox ci * refactor: remove force render in infinite * build: add _internal * chore: mark warning test * fix: dts generation * codesanbox ci * chore: rename swr folder to core Co-authored-by: Jiachi Liu <inbox@huozhi.im> * chore: useSES should be external (#1960) * rename type * types: update cache types * chore: move @types/useSES to devDependencies Co-authored-by: Shu Ding <g@shud.in> Co-authored-by: Jiachi Liu <inbox@huozhi.im>
- Loading branch information
1 parent
caa61a7
commit 4d65099
Showing
57 changed files
with
747 additions
and
517 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import SWRConfig from './utils/config-context' | ||
import * as revalidateEvents from './constants' | ||
|
||
export { SWRConfig, revalidateEvents } | ||
|
||
export { initCache } from './utils/cache' | ||
export { defaultConfig, cache, mutate, compare } from './utils/config' | ||
export * from './utils/env' | ||
export { SWRGlobalState } from './utils/global-state' | ||
export { stableHash } from './utils/hash' | ||
export * from './utils/helper' | ||
export { mergeConfigs } from './utils/merge-config' | ||
export { internalMutate } from './utils/mutate' | ||
export { normalize } from './utils/normalize-args' | ||
export { withArgs } from './utils/resolve-args' | ||
export { serialize } from './utils/serialize' | ||
export { useStateWithDeps } from './utils/state' | ||
export { subscribeCallback } from './utils/subscribe-key' | ||
export { getTimestamp } from './utils/timestamp' | ||
export { useSWRConfig } from './utils/use-swr-config' | ||
export { preset, defaultConfigOptions } from './utils/web-preset' | ||
export { withMiddleware } from './utils/with-middleware' | ||
|
||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "swr-internal", | ||
"version": "0.0.1", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.esm.js", | ||
"types": "./dist/_internal", | ||
"exports": "./dist/index.mjs", | ||
"peerDependencies": { | ||
"react": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "..", | ||
"outDir": "./dist" | ||
}, | ||
"include": ["./**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { defaultConfigOptions } from './web-preset' | ||
import { IS_SERVER } from './env' | ||
import { UNDEFINED, mergeObjects, noop } from './helper' | ||
import { internalMutate } from './mutate' | ||
import { SWRGlobalState } from './global-state' | ||
import * as revalidateEvents from '../constants' | ||
|
||
import { | ||
Cache, | ||
ScopedMutator, | ||
RevalidateEvent, | ||
RevalidateCallback, | ||
ProviderConfiguration, | ||
GlobalState | ||
} from '../types' | ||
|
||
const revalidateAllKeys = ( | ||
revalidators: Record<string, RevalidateCallback[]>, | ||
type: RevalidateEvent | ||
) => { | ||
for (const key in revalidators) { | ||
if (revalidators[key][0]) revalidators[key][0](type) | ||
} | ||
} | ||
|
||
export const initCache = <Data = any>( | ||
provider: Cache<Data>, | ||
options?: Partial<ProviderConfiguration> | ||
): | ||
| [Cache<Data>, ScopedMutator<Data>, () => void, () => void] | ||
| [Cache<Data>, ScopedMutator<Data>] | ||
| undefined => { | ||
// The global state for a specific provider will be used to deduplicate | ||
// requests and store listeners. As well as a mutate function that bound to | ||
// the cache. | ||
|
||
// Provider's global state might be already initialized. Let's try to get the | ||
// global state associated with the provider first. | ||
if (!SWRGlobalState.has(provider)) { | ||
const opts = mergeObjects(defaultConfigOptions, options) | ||
|
||
// If there's no global state bound to the provider, create a new one with the | ||
// new mutate function. | ||
const EVENT_REVALIDATORS = {} | ||
const mutate = internalMutate.bind( | ||
UNDEFINED, | ||
provider | ||
) as ScopedMutator<Data> | ||
let unmount = noop | ||
|
||
const subscriptions: Record<string, ((current: any, prev: any) => void)[]> = | ||
{} | ||
const subscribe = ( | ||
key: string, | ||
callback: (current: any, prev: any) => void | ||
) => { | ||
const subs = subscriptions[key] || [] | ||
subscriptions[key] = subs | ||
|
||
subs.push(callback) | ||
return () => { | ||
subs.splice(subs.indexOf(callback), 1) | ||
} | ||
} | ||
const setter = (key: string, value: any, prev: any) => { | ||
provider.set(key, value) | ||
const subs = subscriptions[key] | ||
if (subs) { | ||
for (let i = subs.length; i--; ) { | ||
subs[i](value, prev) | ||
} | ||
} | ||
} | ||
|
||
const initProvider = () => { | ||
if (!SWRGlobalState.has(provider)) { | ||
// Update the state if it's new, or the provider has been extended. | ||
SWRGlobalState.set(provider, [ | ||
EVENT_REVALIDATORS, | ||
{}, | ||
{}, | ||
mutate, | ||
setter, | ||
subscribe | ||
]) | ||
if (!IS_SERVER) { | ||
// When listening to the native events for auto revalidations, | ||
// we intentionally put a delay (setTimeout) here to make sure they are | ||
// fired after immediate JavaScript executions, which can possibly be | ||
// React's state updates. | ||
// This avoids some unnecessary revalidations such as | ||
// https://github.com/vercel/swr/issues/1680. | ||
const releaseFocus = opts.initFocus( | ||
setTimeout.bind( | ||
UNDEFINED, | ||
revalidateAllKeys.bind( | ||
UNDEFINED, | ||
EVENT_REVALIDATORS, | ||
revalidateEvents.FOCUS_EVENT | ||
) | ||
) | ||
) | ||
const releaseReconnect = opts.initReconnect( | ||
setTimeout.bind( | ||
UNDEFINED, | ||
revalidateAllKeys.bind( | ||
UNDEFINED, | ||
EVENT_REVALIDATORS, | ||
revalidateEvents.RECONNECT_EVENT | ||
) | ||
) | ||
) | ||
unmount = () => { | ||
releaseFocus && releaseFocus() | ||
releaseReconnect && releaseReconnect() | ||
// When un-mounting, we need to remove the cache provider from the state | ||
// storage too because it's a side-effect. Otherwise when re-mounting we | ||
// will not re-register those event listeners. | ||
SWRGlobalState.delete(provider) | ||
} | ||
} | ||
} | ||
} | ||
initProvider() | ||
|
||
// This is a new provider, we need to initialize it and setup DOM events | ||
// listeners for `focus` and `reconnect` actions. | ||
|
||
// We might want to inject an extra layer on top of `provider` in the future, | ||
// such as key serialization, auto GC, etc. | ||
// For now, it's just a `Map` interface without any modifications. | ||
return [provider, mutate, initProvider, unmount] | ||
} | ||
|
||
return [provider, (SWRGlobalState.get(provider) as GlobalState)[3]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Cache, GlobalState } from '../types' | ||
|
||
// Global state used to deduplicate requests and store listeners | ||
export const SWRGlobalState = new WeakMap<Cache, GlobalState>() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.