From 841fd76002b3eb82871659ab57fe5ace66cd64d8 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 2 Jul 2024 17:57:00 +0200 Subject: [PATCH] fix: capture for circular reference in state editor --- .../client/components/StateEditor.vue | 59 +++++++++++++------ 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/packages/devtools/client/components/StateEditor.vue b/packages/devtools/client/components/StateEditor.vue index bdd23a68f..c2e01c284 100644 --- a/packages/devtools/client/components/StateEditor.vue +++ b/packages/devtools/client/components/StateEditor.vue @@ -14,24 +14,41 @@ const emit = defineEmits<{ const isOpen = useVModel(props, 'open', emit, { passive: true }) const colorMode = useColorMode() -const proxy = ref() +const proxy = shallowRef() +const error = shallowRef() const state = useState(props.name) -if (props.state) - proxy.value = JSON.parse(JSON.stringify(props.state)) -else if (typeof props.state === 'number' || typeof props.state !== 'string') - proxy.value = props.state -const watcher = watchPausable( - proxy, - (value) => { - if (typeof value !== 'number' && typeof value !== 'string') - deepSync(value, props.state) - else - state.value = value - }, - { deep: true }, -) +function clone() { + error.value = undefined + try { + if (props.state) + proxy.value = JSON.parse(JSON.stringify(props.state)) + else if (typeof props.state === 'number' || typeof props.state !== 'string') + proxy.value = props.state + } + catch (e) { + console.error(e) + error.value = e + } +} + +let watcher: ReturnType | undefined + +onMounted(() => { + clone() + + watcher = watchPausable( + proxy, + (value) => { + if (typeof value !== 'number' && typeof value !== 'string') + deepSync(value, props.state) + else + state.value = value + }, + { deep: true }, + ) +}) function deepSync(from: any, to: any) { for (const key in from) { @@ -47,10 +64,10 @@ function deepSync(from: any, to: any) { } async function refresh() { - watcher.pause() - proxy.value = JSON.parse(JSON.stringify(props.state)) + watcher?.pause() + clone() await nextTick() - watcher.resume() + watcher?.resume() } @@ -75,13 +92,17 @@ async function refresh() {