8000 feat: mutateTag by koba04 · Pull Request #4070 · vercel/swr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: mutateTag #4070

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/_internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { INFINITE_PREFIX } from './constants'
export { SWRConfig, revalidateEvents, INFINITE_PREFIX }

export { initCache } from './utils/cache'
export { defaultConfig, cache, mutate, compare } from './utils/config'
export {
defaultConfig,
cache,
mutate,
mutateTag,
compare
} from './utils/config'
import { setupDevTools } from './utils/devtools'
export * from './utils/env'
export { SWRGlobalState } from './utils/global-state'
Expand Down
13 changes: 13 additions & 0 deletions src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type GlobalState = [
Record<string, [any, number]>, // FETCH: [data, ts]
Record<string, FetcherResponse<any>>, // PRELOAD
ScopedMutator, // Mutator
TagMutator, // TagMutator
(key: string, value: any, prev: any) => void, // Setter
(key: string, callback: (current: any, prev: any) => void) => () => void // Subscriber
]
Expand Down Expand Up @@ -45,6 +46,7 @@ export type BlockingData<
export interface InternalConfiguration {
cache: Cache
mutate: ScopedMutator
mutateTag: TagMutator
}

/**
Expand Down Expand Up @@ -206,6 +208,10 @@ export interface PublicConfiguration<
* @link https://swr.vercel.app/docs/advanced/react-native#customize-focus-and-reconnect-events
*/
isVisible: () => boolean
/**
8000 * tags to associate with the data, you can mutate data based on these tags
*/
tag: string[]
}

export type FullConfiguration<
Expand Down Expand Up @@ -391,6 +397,7 @@ export type State<Data = any, Error = any> = {
error?: Error
isValidating?: boolean
isLoading?: boolean
_tag?: string[]
}

export type MutatorFn<Data = any> = (
Expand Down Expand Up @@ -435,6 +442,12 @@ export interface ScopedMutator {
): Promise<T | undefined>
}

export type TagMutator = <Data = any, MutationData = Data>(
tag: string,
data?: MutationData | Promise<MutationData> | MutatorCallback<MutationData>,
opts?: boolean | MutatorOptions<Data, MutationData>
) => Promise<Array<MutationData | undefined>>

/**
* @typeParam Data - The type of the data related to the key
* @typeParam MutationData - The type of the data returned by the mutator
Expand Down
19 changes: 13 additions & 6 deletions src/_internal/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defaultConfigOptions } from './web-preset'
import { IS_SERVER } from './env'
import { UNDEFINED, mergeObjects, noop } from './shared'
import { internalMutate } from './mutate'
import { internalMutate, internalMutateTag } from './mutate'
import { SWRGlobalState } from './global-state'
import * as revalidateEvents from '../events'

Expand All @@ -11,7 +11,8 @@ import type {
RevalidateEvent,
RevalidateCallback,
ProviderConfiguration,
GlobalState
GlobalState,
TagMutator
} from '../types'

const revalidateAllKeys = (
Expand All @@ -27,8 +28,8 @@ export const initCache = <Data = any>(
provider: Cache<Data>,
options?: Partial<ProviderConfiguration>
):
| [Cache<Data>, ScopedMutator, () => void, () => void]
| [Cache<Data>, ScopedMutator]
| [Cache<Data>, ScopedMutator, TagMutator, () => void, () => void]
| [Cache<Data>, ScopedMutator, TagMutator]
| undefined => {
// The global state for a specific provider will be used to deduplicate
// requests and store listeners. As well as a mutate function that is bound to
Expand All @@ -44,6 +45,7 @@ export const initCache = <Data = any>(
const EVENT_REVALIDATORS = {}

const mutate = internalMutate.bind(UNDEFINED, provider) as ScopedMutator
const mutateTag = internalMutateTag.bind(UNDEFINED, provider) as TagMutator
8000 let unmount = noop

const subscriptions: Record<string, ((current: any, prev: any) => void)[]> =
Expand Down Expand Up @@ -77,6 +79,7 @@ export const initCache = <Data = any>(
{},
{},
mutate,
mutateTag,
setter,
subscribe
])
Expand Down Expand Up @@ -126,8 +129,12 @@ export const initCache = <Data = any>(
// 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, mutate, mutateTag, initProvider, unmount]
}

return [provider, (SWRGlobalState.get(provider) as GlobalState)[4]]
return [
provider,
(SWRGlobalState.get(provider) as GlobalState)[4],
(SWRGlobalState.get(provider) as GlobalState)[5]
]
}
5 changes: 3 additions & 2 deletions src/_internal/utils/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ const SWRConfig: FC<
if (cacheContext) {
;(extendedConfig as any).cache = cacheContext[0]
;(extendedConfig as any).mutate = cacheContext[1]
;(extendedConfig as any).mutateTag = cacheContext[2]
}

// Unsubscribe events.
useIsomorphicLayoutEffect(() => {
if (cacheContext) {
cacheContext[2] && cacheContext[2]()
return cacheContext[3]
cacheContext[3] && cacheContext[3]()
return cacheContext[4]
}
}, [])

Expand Down
12 changes: 9 additions & 3 deletions src/_internal/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
RevalidatorOptions,
Revalidator,
ScopedMutator,
Cache
Cache,
TagMutator
} from '../types'

import { initCache } from './cache'
Expand Down Expand Up @@ -42,8 +43,12 @@ const >
const compare = dequal

// Default cache provider
const [cache, mutate] = initCache(new Map()) as [Cache<any>, ScopedMutator]
export { cache, mutate, compare }
const [cache, mutate, mutateTag] = initCache(new Map()) as [
Cache<any>,
ScopedMutator,
TagMutator
]
export { cache, mutate, mutateTag, compare }

// Default config
export const defaultConfig: FullConfiguration = mergeObjects(
Expand Down Expand Up @@ -72,6 +77,7 @@ export const defaultConfig: FullConfiguration = mergeObjects(
isPaused: () => false,
cache,
mutate,
mutateTag,
fallback: {}
},
// use web preset by default
Expand Down
4 changes: 2 additions & 2 deletions src/_internal/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export const createCacheHelper = <Data = any, T = State<Data, any>>(
INITIAL_CACHE[key] = prev
}

state[5](key, mergeObjects(prev, info), prev || EMPTY_CACHE)
state[6](key, mergeObjects(prev, info), prev || EMPTY_CACHE)
}
},
// Subscriber
state[6],
state[7],
// Get server cache snapshot
() => {
if (!isUndefined(key)) {
Expand Down
Loading
Loading
0