Skip to content

Commit

Permalink
refactor!: orangize internal host client interface (nuxt#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored and Dev1436 committed Aug 12, 2023
1 parent 77c3057 commit c7ff1b9
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 94 deletions.
68 changes: 41 additions & 27 deletions packages/devtools-kit/src/_types/client-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,24 @@ export interface NuxtDevtoolsClientHooks {
* Triggers reactivity manually, since Vue won't be reactive across frames)
*/
'host:update:reactivity': () => void
/**
* Host action to control the DevTools navigation
*/
'host:action:navigate': (path: string) => void
/**
* Host action to reload the DevTools
*/
'host:action:reload': () => void
}

/**
* Host client from the App
*/
export interface NuxtDevtoolsHostClient {
nuxt: NuxtApp
appConfig: AppConfig
hooks: Hookable<NuxtDevtoolsClientHooks>

colorMode: Ref<'dark' | 'light'>
getIframe(): HTMLIFrameElement | undefined

inspector?: {
instance?: VueInspectorClient
Expand All @@ -62,38 +69,45 @@ export interface NuxtDevtoolsHostClient {
isEnabled: Ref<boolean>
}

loadingTimeMetrics: LoadingTimeMetric
getClientHooksMetrics(): HookInfo[]

clientPluginMetrics: PluginMetric[] | undefined
clientTimelineMetrics: TimelineMetrics | undefined

reloadPage(): void
devtools: {
close(): void
open(): void
toggle(): void
reload(): void
navigate(path: string): void

/**
* Popup the DevTools frame into Picture-in-Picture mode
*
* Requires Chrome 111 with experimental flag enabled.
*
* Function is undefined when not supported.
*
* @see https://developer.chrome.com/docs/web-platform/document-picture-in-picture/
*/
popup?(): any
}

close(): void
open(): void
toggle(): void
app: {
reload(): void
navigate(path: string, hard?: boolean): void
appConfig: AppConfig
colorMode: Ref<'dark' | 'light'>
frameState: Ref<DevToolsFrameState>
}

/**
* Popup the DevTools frame into Picture-in-Picture mode
*
* Requires Chrome 111 with experimental flag enabled.
*
* Function is undefined when not supported.
*
* @see https://developer.chrome.com/docs/web-platform/document-picture-in-picture/
*/
popup?(): any
metrics: {
clientHooks(): HookInfo[]
clientPlugins(): PluginMetric[] | undefined
clientTimeline(): TimelineMetrics | undefined
loading(): LoadingTimeMetric
}

/**
* Update client
* @internal
*/
updateClient(): NuxtDevtoolsHostClient

getIframe(): HTMLIFrameElement | undefined

frameState: Ref<DevToolsFrameState>
syncClient(): NuxtDevtoolsHostClient
}

export interface NuxtDevtoolsClient {
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools/client/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const isUtilityView = computed(() => route.path.startsWith('/__'))
const waiting = computed(() => !client.value && !showConnectionWarning.value)
watch(
() => client.value?.colorMode.value,
() => client.value?.app.colorMode.value,
(mode) => {
if (mode)
colorMode.value = mode
Expand All @@ -46,7 +46,7 @@ watch(
addEventListener('keydown', (e) => {
if (e.code === 'KeyD' && e.altKey) {
client.value?.close()
client.value?.devtools.close()
e.preventDefault()
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/components/RestartDialogs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ nuxt.hook('devtools:terminal:exit', ({ id, code }) => {
if (result) {
rpc.restartNuxt()
setTimeout(() => {
client.value?.reloadPage()
client.value?.app.reload()
}, 500)
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/components/TimelineView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const client = useClient()
const view = ref<'table' | 'list'>('table')
const selected = ref<TimelineEvent | undefined>()
const metrics = computed(() => client.value?.clientTimelineMetrics)
const metrics = computed(() => client.value?.metrics.clientTimeline())
function clear() {
if (metrics.value)
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/pages/modules/hooks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ definePageMeta({
const serverHooks = useServerHooks()
const client = useClient()
const clientHooks = computed(() => client.value?.getClientHooksMetrics())
const clientHooks = computed(() => client.value?.metrics.clientHooks())
</script>

<template>
Expand Down
15 changes: 8 additions & 7 deletions packages/devtools/client/pages/modules/overview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function goIntro() {
}
const vueVersion = computed(() => client.value?.nuxt.vueApp.version)
const metricsLoading = computed(() => client.value?.metrics.loading())
</script>

<template>
Expand Down Expand Up @@ -87,35 +88,35 @@ const vueVersion = computed(() => client.value?.nuxt.vueApp.version)
<div carbon-plug text-3xl />
<div>{{ config.plugins.length }} plugins</div>
</NuxtLink>
<div v-if="client?.loadingTimeMetrics" pointer-events-none min-w-40 p4 theme-card-lime flex="~ auto gap-6">
<div v-if="metricsLoading" pointer-events-none min-w-40 p4 theme-card-lime flex="~ auto gap-6">
<div i-carbon-time-plot flex-none text-3xl />
<div grid="~ cols-[auto_auto] gap-x-5 items-center">
<template v-if="client.loadingTimeMetrics.ssrStart">
<template v-if="metricsLoading.ssrStart">
<div text-sm>
SSR to full load
</div>
<div text-right>
{{ formatDuration(client.loadingTimeMetrics.appLoad! - client.loadingTimeMetrics.ssrStart!) }}
{{ formatDuration(metricsLoading.appLoad! - metricsLoading.ssrStart!) }}
</div>
</template>
<div text-sm>
Page load
</div>
<div text-right>
{{ formatDuration(client.loadingTimeMetrics.appLoad! - client.loadingTimeMetrics.appInit!) }}
{{ formatDuration(metricsLoading.appLoad! - metricsLoading.appInit!) }}
</div>
<div text-sm>
Navigation
</div>
<div text-right>
{{ formatDuration(client.loadingTimeMetrics.pageEnd! - client.loadingTimeMetrics.pageStart!) }}
{{ formatDuration(metricsLoading.pageEnd! - metricsLoading.pageStart!) }}
</div>
<template v-if="client.loadingTimeMetrics.hmrStart">
<template v-if="metricsLoading.hmrStart">
<div text-sm>
HMR
</div>
<div text-right>
{{ formatDuration(client.loadingTimeMetrics.hmrEnd! - client.loadingTimeMetrics.hmrStart!) }}
{{ formatDuration(metricsLoading.hmrEnd! - metricsLoading.hmrStart!) }}
</div>
</template>
</div>
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools/client/pages/modules/plugins.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const client = useClient()
const plugins = computed((): PluginInfoWithMetic[] => {
const plugins = app.value?.plugins || []
const metics = client.value?.clientPluginMetrics || []
const metics = client.value?.metrics.clientPlugins() || []
return plugins.map((plugin) => {
const p = typeof plugin === 'string' ? { src: plugin } : plugin
Expand All @@ -24,7 +24,7 @@ const plugins = computed((): PluginInfoWithMetic[] => {
})
const totalTime = computed(() => {
const metics = client.value?.clientPluginMetrics || []
const metics = client.value?.metrics.clientPlugins() || []
const minStart = Math.min(...metics.map(m => m.start))
const maxEnd = Math.max(...metics.map(m => m.end))
return maxEnd - minStart
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/pages/modules/runtime-configs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const privateConfig = computed(() => {
text="App Config"
:padding="false"
>
<StateEditor :state="client.appConfig" />
<StateEditor :state="client.app.appConfig" />
</NSectionBlock>

<NSectionBlock
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools/client/pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ function pinMove(name: string, delta: number) {
// sync devtools options with frame state
watchEffect(() => {
client.value.frameState.value.closeOnOutsideClick = interactionCloseOnOutsideClick.value
client.value.app.frameState.value.closeOnOutsideClick = interactionCloseOnOutsideClick.value
})
watchEffect(() => {
client.value.frameState.value.minimizePanelInactive = minimizePanelInactive.value
client.value.app.frameState.value.minimizePanelInactive = minimizePanelInactive.value
})
</script>

Expand Down
2 changes: 2 additions & 0 deletions packages/devtools/client/plugins/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default defineNuxtPlugin(() => {
_client.hooks.hook('host:update:reactivity', onUpdateReactivity)
_client.hooks.hook('host:inspector:update', onInspectorUpdate)
_client.hooks.hook('host:inspector:click', onInspectorClick)
_client.hooks.hook('host:action:reload', () => location.reload())
_client.hooks.hook('host:action:navigate', (path: string) => router.push(path))

// eslint-disable-next-line no-console
console.log('[nuxt-devtools] Client connected', _client)
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/src/runtime/plugins/view/FrameBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ useEventListener(window, 'keydown', (e: KeyboardEvent) => {
if (e.key === 'Escape' && props.client.inspector?.isEnabled.value) {
e.preventDefault()
props.client.inspector?.disable()
props.client.close()
props.client.devtools.close()
}
})
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools/src/runtime/plugins/view/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ watchEffect(() => {
const SNAP_THRESHOLD = 2
const vars = computed(() => {
const dark = props.client.colorMode.value === 'dark'
const dark = props.client.app.colorMode.value === 'dark'
return {
'--nuxt-devtools-widget-bg': dark ? '#111' : '#ffffff',
'--nuxt-devtools-widget-fg': dark ? '#F5F5F5' : '#111',
Expand Down Expand Up @@ -285,7 +285,7 @@ const iframeStyle = computed(() => {
const time = computed(() => {
let type = ''
const metric = props.client.loadingTimeMetrics
const metric = props.client.metrics.loading()
let time = -1
if (metric.pageEnd && metric.pageStart) {
time = metric.pageEnd - metric.pageStart
Expand Down Expand Up @@ -328,7 +328,7 @@ onMounted(() => {
class="nuxt-devtools-icon-button nuxt-devtools-nuxt-button"
title="Toggle Nuxt DevTools"
:style="state.open ? '' : 'filter:saturate(0)'"
@click="client.toggle()"
@click="client.devtools.toggle()"
>
<svg
viewBox="0 0 324 324" fill="none" xmlns="http://www.w3.org/2000/svg"
Expand Down
Loading

0 comments on commit c7ff1b9

Please sign in to comment.