-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
454 additions
and
63 deletions.
There are no files selected for viewing
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,65 @@ | ||
/* eslint-disable eslint-comments/disable-enable-pair */ | ||
/* eslint-disable no-param-reassign */ | ||
import { App, PluginDescriptor, setupDevtoolsPlugin, DevtoolsPluginApi } from '@vue/devtools-api'; | ||
import { watch } from 'vue'; | ||
|
||
import { defaultConfig } from '@/config'; | ||
|
||
type AnyFunction = (...args: any[]) => any; | ||
|
||
type HookType< | ||
TApi extends DevtoolsPluginApi<unknown>, | ||
THookName extends keyof TApi['on'], | ||
T = TApi['on'][THookName], | ||
> = T extends AnyFunction ? Parameters<T>[0] : never; | ||
|
||
const inspectorId = 'swr-vue-inspector'; | ||
|
||
export function setupDevtools(app: App) { | ||
const setupPluginSettings: PluginDescriptor = { | ||
id: 'swr-vue-devtools-plugin', | ||
label: 'Stale-While-Revalidate Vue', | ||
packageName: 'swr-vue', | ||
homepage: 'https://edumudu.github.io/swr-vue/', | ||
app, | ||
}; | ||
|
||
const isSwrInspector = (id: string) => id === inspectorId; | ||
|
||
setupDevtoolsPlugin(setupPluginSettings, (api) => { | ||
const handleGetInspectorTree: HookType<typeof api, 'getInspectorTree'> = (payload) => { | ||
if (!isSwrInspector(inspectorId)) return; | ||
|
||
payload.rootNodes = [ | ||
{ | ||
id: 'root', | ||
label: 'Global scope', | ||
}, | ||
]; | ||
}; | ||
|
||
const handleGetInspectorState: HookType<typeof api, 'getInspectorState'> = (payload) => { | ||
if (!isSwrInspector(inspectorId) || payload.nodeId !== 'root') return; | ||
|
||
const entries = Array.from(defaultConfig.cacheProvider.entries(), ([key, value]) => ({ | ||
key, | ||
value, | ||
})); | ||
|
||
payload.state = { | ||
'Global cache': entries, | ||
}; | ||
}; | ||
|
||
api.addInspector({ | ||
id: inspectorId, | ||
label: 'SWR', | ||
icon: 'archive', | ||
}); | ||
|
||
api.on.getInspectorTree(handleGetInspectorTree); | ||
api.on.getInspectorState(handleGetInspectorState); | ||
|
||
watch(defaultConfig.cacheProvider, () => api.sendInspectorState(inspectorId), { deep: true }); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './composables'; | ||
export * from './devtools'; |
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
Oops, something went wrong.