Skip to content

Commit

Permalink
fix: introduce client.revision to trigger state editor update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 2, 2024
1 parent 841fd76 commit 418a22e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 11 deletions.
5 changes: 5 additions & 0 deletions packages/devtools-kit/src/_types/client-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export interface NuxtDevtoolsHostClient {
loading: () => LoadingTimeMetric
}

/**
* A counter to trigger reactivity updates
*/
revision: Ref<number>

/**
* Update client
* @internal
Expand Down
4 changes: 3 additions & 1 deletion packages/devtools-kit/src/runtime/iframe-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export function useDevtoolsClient() {
}

function onUpdateReactivity() {
triggerRef(clientRef!)
if (clientRef) {
triggerRef(clientRef)
}
}

function setup(client: NuxtDevtoolsIframeClient) {
Expand Down
9 changes: 4 additions & 5 deletions packages/devtools/client/components/StateEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import JsonEditorVue from 'json-editor-vue'
const props = defineProps<{
name?: string
open?: boolean
revision?: number
state?: any
readonly?: boolean
}>()
Expand All @@ -17,8 +18,6 @@ const colorMode = useColorMode()
const proxy = shallowRef()
const error = shallowRef()
const state = useState(props.name)
function clone() {
error.value = undefined
try {
Expand All @@ -38,13 +37,13 @@ let watcher: ReturnType<typeof watchPausable> | undefined
onMounted(() => {
clone()
watcher = watchPausable(
proxy,
watch(
() => [props.revision, props.state],
(value) => {
if (typeof value !== 'number' && typeof value !== 'string')
deepSync(value, props.state)

This comment has been minimized.

Copy link
@hichem-dahi

hichem-dahi Jul 2, 2024

  1. This watcher seems to trigger an endless loop.
  2. Passing value seems to trigger an error. Maybe replace value with value[1]?
else
state.value = value
proxy.value = props.state
},
{ deep: true },
)
Expand Down
2 changes: 2 additions & 0 deletions packages/devtools/client/components/StateGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
withDefaults(
defineProps<{
state?: Record<string, any>
revision?: number
prefix?: string
}>(),
{
Expand All @@ -16,6 +17,7 @@ withDefaults(
<StateEditor
v-for="value, key of state"
:key="key"
:revision="revision"
:state="value"
:name="key.startsWith(prefix) ? key.slice(prefix.length) : key"
>
Expand Down
1 change: 1 addition & 0 deletions packages/devtools/client/composables/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export function refreshData() {

nuxt.hooks.callHookParallel('app:data:refresh', Object.keys(nuxt.payload.data))
triggerRef(client)
client.value.revision.value += 1
}

export function reloadPage() {
Expand Down
11 changes: 9 additions & 2 deletions packages/devtools/client/pages/modules/payload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ definePageMeta({
const client = useClient()
const payload = computed(() => client.value?.nuxt.payload)
const revision = computed(() => client.value?.revision.value)
async function refreshData(keys?: string[]) {
await client.value?.nuxt.hooks.callHookParallel('app:data:refresh', keys)
Expand All @@ -27,7 +28,9 @@ async function refreshData(keys?: string[]) {
:padding="false"
>
<StateGroup
:state="payload.state" prefix="$s"
:state="payload.state"
:revision="revision"
prefix="$s"
/>
</NSectionBlock>
<NSectionBlock
Expand All @@ -45,7 +48,10 @@ async function refreshData(keys?: string[]) {
Re-fetch all data
</NButton>
</template>
<StateGroup :state="payload.data">
<StateGroup
:state="payload.data"
:revision="revision"
>
<template #actions="{ isOpen, name }">
<NButton
v-if="isOpen && name"
Expand All @@ -67,6 +73,7 @@ async function refreshData(keys?: string[]) {
<StateEditor
ml--6
:state="payload.functions"
:revision="revision"
/>
</NSectionBlock>
</div>
Expand Down
17 changes: 14 additions & 3 deletions packages/devtools/client/pages/modules/runtime-configs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ definePageMeta({
const client = useClient()
const runtimeConfig = useServerRuntimeConfig()
const payload = computed(() => client.value?.nuxt.payload)
const revision = computed(() => client.value?.revision.value)
const privateConfig = computed(() => {
const clone = {
Expand All @@ -31,15 +32,21 @@ const privateConfig = computed(() => {
text="App Config"
:padding="false"
>
<StateEditor :state="client.app.appConfig" />
<StateEditor
:state="client.app.appConfig"
:revision="revision"
/>
</NSectionBlock>

<NSectionBlock
icon="carbon-settings"
text="Public Runtime Config"
:padding="false"
>
<StateEditor :state="payload.config?.public" />
<StateEditor
:state="payload.config?.public"
:revision="revision"
/>
</NSectionBlock>

<NSectionBlock
Expand All @@ -49,7 +56,11 @@ const privateConfig = computed(() => {
:padding="false"
description="These values are not exposed to the client. Readonly in the DevTools."
>
<StateEditor :state="privateConfig" readonly />
<StateEditor
:state="privateConfig"
:revision="revision"
readonly
/>
</NSectionBlock>
</div>

Expand Down
1 change: 1 addition & 0 deletions packages/devtools/client/plugins/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineNuxtPlugin(() => {

function onUpdateReactivity() {
triggerRef(client)
client.value.revision.value += 1
}

function onInspectorUpdate(data: any) {
Expand Down
2 changes: 2 additions & 0 deletions packages/devtools/src/runtime/plugins/view/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export async function setupDevToolsClient({
clientTimeline: () => timeline,
loading: () => timeMetric,
},

revision: ref(0),
})

window.__NUXT_DEVTOOLS_HOST__ = client
Expand Down

0 comments on commit 418a22e

Please sign in to comment.