fix(vue-runtime-hlep): 页面片销毁后需要取消app的事件监听

This commit is contained in:
roymondchen 2025-11-12 19:49:55 +08:00
parent 8809351537
commit e36da82d29
2 changed files with 21 additions and 11 deletions

View File

@ -1,5 +1,5 @@
{ {
"version": "1.2.2", "version": "1.2.3",
"name": "@tmagic/vue-runtime-help", "name": "@tmagic/vue-runtime-help",
"type": "module", "type": "module",
"sideEffects": false, "sideEffects": false,

View File

@ -4,38 +4,48 @@ import type TMagicApp from '@tmagic/core';
import type { ChangeEvent, Id, MNode } from '@tmagic/core'; import type { ChangeEvent, Id, MNode } from '@tmagic/core';
import { isPage, isPageFragment, replaceChildNode } from '@tmagic/core'; import { isPage, isPageFragment, replaceChildNode } from '@tmagic/core';
export const useDsl = (app = inject<TMagicApp>('app'), pageFragmentConstainerId?: Id) => { export const useDsl = (app = inject<TMagicApp>('app'), pageFragmentContainerId?: Id) => {
if (!app) { if (!app) {
throw new Error('useDsl must be used after MagicApp is created'); throw new Error('useDsl must be used after MagicApp is created');
} }
const pageFragment = pageFragmentConstainerId ? app.pageFragments.get(pageFragmentConstainerId) : null; const pageFragment = pageFragmentContainerId ? app.pageFragments.get(pageFragmentContainerId) : null;
const pageConfig = ref<MNode | undefined>(pageFragmentConstainerId ? pageFragment?.data : app.page?.data); const pageConfig = ref<MNode | undefined>(pageFragmentContainerId ? pageFragment?.data : app.page?.data);
if (pageFragmentConstainerId) { if (pageFragmentContainerId) {
app.on('dsl-change', () => { const setPageConfig = () => {
pageConfig.value = pageFragment?.data; pageConfig.value = pageFragment?.data;
};
app.on('dsl-change', setPageConfig);
onBeforeUnmount(() => {
app.off('dsl-change', setPageConfig);
}); });
} else { } else {
app.on('page-change', () => { const setPageConfig = () => {
pageConfig.value = app.page?.data; pageConfig.value = app.page?.data;
};
app.on('page-change', setPageConfig);
onBeforeUnmount(() => {
app.off('page-change', setPageConfig);
}); });
} }
const updateDataHandler = (nodes: MNode[], sourceId: string, changeEvent: ChangeEvent, pageId: Id) => { const updateDataHandler = (nodes: MNode[], sourceId: string, changeEvent: ChangeEvent, pageId: Id) => {
if ( if (
!nodes.length || !nodes.length ||
(pageFragmentConstainerId && pageFragment?.data.id !== pageId) || (pageFragmentContainerId && pageFragment?.data.id !== pageId) ||
(!pageFragmentConstainerId && app.page?.data.id !== pageId) (!pageFragmentContainerId && app.page?.data.id !== pageId)
) { ) {
return; return;
} }
for (const node of nodes) { for (const node of nodes) {
if ( if (
(isPage(node) && !pageFragmentConstainerId && node.id === pageId) || (isPage(node) && !pageFragmentContainerId && node.id === pageId) ||
(isPageFragment(node) && pageFragmentConstainerId) (isPageFragment(node) && pageFragmentContainerId)
) { ) {
pageConfig.value = node; pageConfig.value = node;
} else { } else {