From 62b8445b28e20c6b12e0eda7b3711d83a2be365c Mon Sep 17 00:00:00 2001 From: William Cai Date: Tue, 27 Sep 2022 22:40:21 +0800 Subject: [PATCH] feat(StoryboardContext): move logic of track into resolveFreeVariableValue to support tracking state in useBrick --- .../brick-kit/src/core/StoryboardContext.ts | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/packages/brick-kit/src/core/StoryboardContext.ts b/packages/brick-kit/src/core/StoryboardContext.ts index e4f30e29f7..a3663fdf45 100644 --- a/packages/brick-kit/src/core/StoryboardContext.ts +++ b/packages/brick-kit/src/core/StoryboardContext.ts @@ -296,36 +296,6 @@ async function resolveNormalStoryboardContext( // Or if the resolve is ignored or lazy, use its `value` as a fallback. value = computeRealValue(contextConf.value, mergedContext, true); } - - if (contextConf.track) { - // Track its dependencies and auto update when each of them changed. - const deps = (isTemplateState ? trackUsedState : trackUsedContext)( - load ? contextConf.resolve : contextConf.value - ); - for (const dep of deps) { - const ctx = storyboardContextWrapper.get().get(dep); - ( - ctx as StoryboardContextItemFreeVariable - )?.eventTarget?.addEventListener( - isTemplateState ? "state.change" : "context.change", - () => { - if (load) { - storyboardContextWrapper.updateValue( - contextConf.name, - { cache: "default" }, - "refresh" - ); - } else { - storyboardContextWrapper.updateValue( - contextConf.name, - computeRealValue(contextConf.value, mergedContext, true), - "replace" - ); - } - } - ); - } - } } resolveFreeVariableValue( @@ -413,5 +383,35 @@ function resolveFreeVariableValue( ); } } + + if (contextConf.track) { + const isTemplateState = !!storyboardContextWrapper.tplContextId; + // Track its dependencies and auto update when each of them changed. + const deps = (isTemplateState ? trackUsedState : trackUsedContext)( + load ? contextConf.resolve : contextConf.value + ); + for (const dep of deps) { + const ctx = storyboardContextWrapper.get().get(dep); + (ctx as StoryboardContextItemFreeVariable)?.eventTarget?.addEventListener( + isTemplateState ? "state.change" : "context.change", + () => { + if (load) { + storyboardContextWrapper.updateValue( + contextConf.name, + { cache: "default" }, + "refresh" + ); + } else { + storyboardContextWrapper.updateValue( + contextConf.name, + computeRealValue(contextConf.value, mergedContext, true), + "replace" + ); + } + } + ); + } + } + storyboardContextWrapper.set(contextConf.name, newContext); }