From 528d1beaee99cbd1b642e4e949a1d91a0021c9d9 Mon Sep 17 00:00:00 2001 From: lichunn <269031597@qq.com> Date: Thu, 31 Jul 2025 15:06:59 +0800 Subject: [PATCH 1/3] fix:Application state getters cannot be rendered --- .../src/application-function/global-state.ts | 57 ++++++++++++++----- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/packages/canvas/render/src/application-function/global-state.ts b/packages/canvas/render/src/application-function/global-state.ts index 9e8f84ae82..155e354046 100644 --- a/packages/canvas/render/src/application-function/global-state.ts +++ b/packages/canvas/render/src/application-function/global-state.ts @@ -1,31 +1,58 @@ -import { ref, shallowReactive, watchEffect } from 'vue' +import { ref, shallowReactive, watchEffect, reactive, computed } from 'vue' import { reset } from '../data-utils' -const Func = Function - export function useGlobalState() { - const globalState = ref([]) + const globalState = ref([]) - const setGlobalState = (data = []) => { + const setGlobalState = (data: any[] = []) => { globalState.value = data } - const stores = shallowReactive({}) + + const stores = shallowReactive>({}) + watchEffect(() => { reset(stores) + globalState.value.forEach(({ id, state = {}, getters = {} }) => { - const computedGetters = Object.keys(getters).reduce( - (acc, key) => ({ - ...acc, - [key]: new Func('return ' + getters[key])().call(acc, state) // parseData(getters[key], null, acc)?.call?.(acc, state) //理论上不应该走parseData, unibuy代码遗留 - }), - {} - ) - stores[id] = { ...state, ...computedGetters } + try { + const reactiveState = reactive({ ...state }) + const store = reactive({ ...reactiveState }) + + if (getters && typeof getters === 'object') { + Object.entries(getters).forEach(([key, getterDef]: [string, any]) => { + try { + if (getterDef?.type === 'JSFunction' && getterDef.value) { + const getterFn = new Function(`return (${getterDef.value})`)() as Function + const computedGetter = computed(() => { + try { + return getterFn.call(store, reactiveState) + } catch (error) { + console.error(`[useGlobalState] Error in getter "${key}" (store ${id}):`, error) + return null + } + }) + + Object.defineProperty(store, key, { + get: () => computedGetter.value, + enumerable: true, + }) + } + } catch (parseError) { + console.error(`[useGlobalState] Invalid getter "${key}" in store ${id}:`, parseError) + } + }) + } + + stores[id] = store + } catch (storeError) { + console.error(`[useGlobalState] Failed to create store "${id}":`, storeError) + } }) }) + return { globalState, setGlobalState, stores } -} +} \ No newline at end of file From ac6db97a98606d068fc46b76b4a9e4ee63250386 Mon Sep 17 00:00:00 2001 From: lichunn <269031597@qq.com> Date: Thu, 31 Jul 2025 15:48:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=E5=8E=BB=E6=8E=89function=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/canvas/render/src/application-function/global-state.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/canvas/render/src/application-function/global-state.ts b/packages/canvas/render/src/application-function/global-state.ts index 155e354046..65a42b8561 100644 --- a/packages/canvas/render/src/application-function/global-state.ts +++ b/packages/canvas/render/src/application-function/global-state.ts @@ -22,7 +22,7 @@ export function useGlobalState() { Object.entries(getters).forEach(([key, getterDef]: [string, any]) => { try { if (getterDef?.type === 'JSFunction' && getterDef.value) { - const getterFn = new Function(`return (${getterDef.value})`)() as Function + const getterFn = new Function(`return (${getterDef.value})`)() const computedGetter = computed(() => { try { return getterFn.call(store, reactiveState) From 54d21a1707a941d7a0867fa0295b33692bdb8452 Mon Sep 17 00:00:00 2001 From: lichunn <269031597@qq.com> Date: Thu, 31 Jul 2025 15:52:02 +0800 Subject: [PATCH 3/3] fix:fix review --- .../render/src/application-function/global-state.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/canvas/render/src/application-function/global-state.ts b/packages/canvas/render/src/application-function/global-state.ts index 65a42b8561..e2eddebcb7 100644 --- a/packages/canvas/render/src/application-function/global-state.ts +++ b/packages/canvas/render/src/application-function/global-state.ts @@ -12,12 +12,12 @@ export function useGlobalState() { watchEffect(() => { reset(stores) - + globalState.value.forEach(({ id, state = {}, getters = {} }) => { try { const reactiveState = reactive({ ...state }) const store = reactive({ ...reactiveState }) - + if (getters && typeof getters === 'object') { Object.entries(getters).forEach(([key, getterDef]: [string, any]) => { try { @@ -27,24 +27,25 @@ export function useGlobalState() { try { return getterFn.call(store, reactiveState) } catch (error) { - console.error(`[useGlobalState] Error in getter "${key}" (store ${id}):`, error) return null } }) Object.defineProperty(store, key, { get: () => computedGetter.value, - enumerable: true, + enumerable: true }) } } catch (parseError) { + // eslint-disable-next-line no-console console.error(`[useGlobalState] Invalid getter "${key}" in store ${id}:`, parseError) } }) } - + stores[id] = store } catch (storeError) { + // eslint-disable-next-line no-console console.error(`[useGlobalState] Failed to create store "${id}":`, storeError) } }) @@ -55,4 +56,4 @@ export function useGlobalState() { setGlobalState, stores } -} \ No newline at end of file +}