feat(nuxt): filter support for clearNuxtData (#7323)

This commit is contained in:
Alex Kozack 2022-09-07 16:25:37 +03:00 committed by GitHub
parent eab4706614
commit 6b3a1a4256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -10,7 +10,7 @@ This method is useful if you want to invalidate the data fetching for another pa
## Type ## Type
```ts ```ts
clearNuxtData (keys?: string | string[]): void clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void
``` ```
## Parameters ## Parameters

View File

@ -254,9 +254,14 @@ export function refreshNuxtData (keys?: string | string[]): Promise<void> {
return useNuxtApp().callHook('app:data:refresh', _keys) return useNuxtApp().callHook('app:data:refresh', _keys)
} }
export function clearNuxtData (keys?: string | string[]): void { export function clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void {
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()
const _keys = keys ? Array.from(keys).filter(key => key && typeof key === 'string') : Object.keys(nuxtApp.payload.data) const _allKeys = Object.keys(nuxtApp.payload.data)
const _keys: string[] = !keys
? _allKeys
: typeof keys === 'function'
? _allKeys.filter(keys)
: Array.isArray(keys) ? keys : [keys]
for (const key of _keys) { for (const key of _keys) {
if (key in nuxtApp.payload.data) { if (key in nuxtApp.payload.data) {