diff --git a/packages/editor/src/components/Editor.tsx b/packages/editor/src/components/Editor.tsx index 625d33b4..e784a355 100644 --- a/packages/editor/src/components/Editor.tsx +++ b/packages/editor/src/components/Editor.tsx @@ -134,10 +134,9 @@ export const Editor: React.FC = observer( }, [activeDataSource, services, activeDataSourceType]); const onRefresh = useCallback(() => { - services.stateManager.clear(); setIsDisplayApp(false); onRefreshApp(); - }, [services.stateManager, onRefreshApp]); + }, [onRefreshApp]); useEffect(() => { // Wait until the app is completely unmounted before remounting it if (isDisplayApp === false) { diff --git a/packages/editor/src/init.tsx b/packages/editor/src/init.tsx index cfc8be57..f19b15bc 100644 --- a/packages/editor/src/init.tsx +++ b/packages/editor/src/init.tsx @@ -99,7 +99,15 @@ export function initSunmaoUIEditor(props: SunmaoUIEditorProps = {}) { const Editor: React.FC = () => { const [store, setStore] = useState(stateManager.store); const onRefresh = useCallback(() => { + // need to reregister all the traits to clear the trait states which like `HasInitializedMap` + const traits = registry.getAllTraits(); + + stateManager.clear(); setStore(stateManager.store); + registry.unregisterAllTraits(); + traits.forEach(trait => { + registry.registerTrait(trait); + }); }, []); return ( diff --git a/packages/runtime/src/components/_internal/ImplWrapper/ImplWrapperMain.tsx b/packages/runtime/src/components/_internal/ImplWrapper/ImplWrapperMain.tsx index aeb28da4..c1d3289a 100644 --- a/packages/runtime/src/components/_internal/ImplWrapper/ImplWrapperMain.tsx +++ b/packages/runtime/src/components/_internal/ImplWrapper/ImplWrapperMain.tsx @@ -24,7 +24,7 @@ export const ImplWrapperMain = React.forwardRef[]>( () => { - return c.traits.map(t => executeTrait(t, stateManager.deepEval(t.properties))); + return c.traits.map(t => executeTrait(t, stateManager.deepEval(t.properties, { fallbackWhenError: () => undefined }))); } ); @@ -49,7 +49,8 @@ export const ImplWrapperMain = React.forwardRef undefined } ); stops.push(stop); properties.push(result); diff --git a/packages/runtime/src/services/Registry.tsx b/packages/runtime/src/services/Registry.tsx index 457e1aeb..74b4e9fd 100644 --- a/packages/runtime/src/services/Registry.tsx +++ b/packages/runtime/src/services/Registry.tsx @@ -176,6 +176,10 @@ export class Registry { this.modules = new Map>(); } + unregisterAllTraits() { + this.traits = new Map>(); + } + registerUtilMethod(m: UtilMethod) { if (this.utilMethods.get(m.name)) { throw new Error(`Already has utilMethod ${m.name} in this registry.`); diff --git a/packages/runtime/src/types/trait.ts b/packages/runtime/src/types/trait.ts index fdc2c5b5..a88fae8f 100644 --- a/packages/runtime/src/types/trait.ts +++ b/packages/runtime/src/types/trait.ts @@ -29,7 +29,7 @@ export type ImplementedRuntimeTraitFactory = RuntimeTrait & { factory: TraitImplFactory; }; -export type ImplementedRuntimeTrait = RuntimeTrait & { +export type ImplementedRuntimeTrait = ImplementedRuntimeTraitFactory & { impl: TraitImpl; };