From 09581e0aedc80eb9233a385b2182ce5a0aeefdaf Mon Sep 17 00:00:00 2001 From: purocean Date: Wed, 1 Dec 2021 18:25:06 +0800 Subject: [PATCH] fix: memory leak --- src/renderer/plugins/markdown-macro.ts | 47 +++++++++++++---------- src/renderer/plugins/markdown-mind-map.ts | 1 + 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/renderer/plugins/markdown-macro.ts b/src/renderer/plugins/markdown-macro.ts index 9fb56d43..f4098f8b 100644 --- a/src/renderer/plugins/markdown-macro.ts +++ b/src/renderer/plugins/markdown-macro.ts @@ -1,7 +1,7 @@ import { omit } from 'lodash-es' import frontMatter from 'front-matter' import type { Plugin } from '@fe/context' -import type { Doc } from '@fe/types' +import type { Doc, RenderEnv } from '@fe/types' import type { MenuItem } from '@fe/services/status-bar' import { render } from '@fe/services/view' import { t } from '@fe/services/i18n' @@ -310,29 +310,34 @@ export default { }) }) - ctx.registerHook('VIEW_RENDERED', ({ renderEnv }) => { - ctx.statusBar.tapMenus(menus => { - const list = menus['status-bar-tool']?.list - if (list) { - const id = 'plugin.markdown-macro.copy-markdown' - const menu: MenuItem = { - id, - type: 'normal', - hidden: !renderEnv?.attributes?.enableMacro, - title: ctx.i18n.t('status-bar.tool.macro-copy-markdown'), - onClick: () => { - ctx.utils.copyText(renderEnv?.source) - } - } + let env: RenderEnv | null = null - const item = list.find(x => x.id === id) - if (item) { - Object.assign(item, menu) - } else { - list.push(menu) + ctx.statusBar.tapMenus(menus => { + const list = menus['status-bar-tool']?.list + if (list) { + const id = 'plugin.markdown-macro.copy-markdown' + const menu: MenuItem = { + id, + type: 'normal', + hidden: !env?.attributes?.enableMacro, + title: ctx.i18n.t('status-bar.tool.macro-copy-markdown'), + onClick: () => { + ctx.utils.copyText(env?.source) } } - }) + + const item = list.find(x => x.id === id) + if (item) { + Object.assign(item, menu) + } else { + list.push(menu) + } + } + }) + + ctx.registerHook('VIEW_RENDERED', ({ renderEnv }) => { + env = renderEnv + ctx.statusBar.refreshMenu() }) } } as Plugin diff --git a/src/renderer/plugins/markdown-mind-map.ts b/src/renderer/plugins/markdown-mind-map.ts index 3fb14f4e..467ef552 100644 --- a/src/renderer/plugins/markdown-mind-map.ts +++ b/src/renderer/plugins/markdown-mind-map.ts @@ -224,6 +224,7 @@ const MindMap = defineComponent({ registerHook('I18N_CHANGE_LANGUAGE', renderMindMap) onBeforeUnmount(() => { + km && km.destroy() removeHook('I18N_CHANGE_LANGUAGE', renderMindMap) })