From fc49893af3d5ad765eda0f038d98787859845d75 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Tue, 5 Dec 2023 11:50:23 +0800 Subject: [PATCH] fix: dispose plugin registry --- packages/blocky-core/package.json | 2 +- .../src/registry/pluginRegistry.spec.ts | 36 +++++++++++++++++-- .../src/registry/pluginRegistry.ts | 11 ++++++ packages/blocky-core/src/view/editor.spec.ts | 22 ++++++++++++ packages/blocky-core/src/view/editor.ts | 3 ++ 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 packages/blocky-core/src/view/editor.spec.ts diff --git a/packages/blocky-core/package.json b/packages/blocky-core/package.json index 4156709..ba3c18e 100644 --- a/packages/blocky-core/package.json +++ b/packages/blocky-core/package.json @@ -1,6 +1,6 @@ { "name": "blocky-core", - "version": "3.7.0", + "version": "3.7.1", "description": "The core of the blocky editor", "main": "dist/index.js", "module": "dist/index.js", diff --git a/packages/blocky-core/src/registry/pluginRegistry.spec.ts b/packages/blocky-core/src/registry/pluginRegistry.spec.ts index cd248c7..5cbae05 100644 --- a/packages/blocky-core/src/registry/pluginRegistry.spec.ts +++ b/packages/blocky-core/src/registry/pluginRegistry.spec.ts @@ -7,16 +7,46 @@ describe("PluginRegistry", () => { const plugin: IPlugin = { name: "test", onInitialized() {}, + dispose() {}, }; const onInitSpy = vi.spyOn(plugin, "onInitialized"); + const disposeSpy = vi.spyOn(plugin, "dispose"); const pluginRegistry = new PluginRegistry([plugin]); - const editorController = new EditorController("user"); + const editorController = new EditorController("user", { + pluginRegistry, + }); const dom = document.createElement("div"); - const editor = Editor.fromController(dom, editorController); - pluginRegistry.initAllPlugins(editor); + Editor.fromController(dom, editorController); expect(onInitSpy).toBeCalledTimes(1); pluginRegistry.unload("test"); + expect(disposeSpy).toBeCalledTimes(1); + }); + + it("dispose", () => { + const plugin1: IPlugin = { + name: "test-1", + onInitialized() {}, + dispose() {}, + }; + const plugin2: IPlugin = { + name: "test-2", + onInitialized() {}, + dispose() {}, + }; + const dispose1 = vi.spyOn(plugin1, "dispose"); + const dispose2 = vi.spyOn(plugin2, "dispose"); + const pluginRegistry = new PluginRegistry([plugin1, plugin2]); + const editorController = new EditorController("user", { + pluginRegistry, + }); + const dom = document.createElement("div"); + Editor.fromController(dom, editorController); + + pluginRegistry.dispose(); + + expect(dispose1).toBeCalledTimes(1); + expect(dispose2).toBeCalledTimes(1); }); }); diff --git a/packages/blocky-core/src/registry/pluginRegistry.ts b/packages/blocky-core/src/registry/pluginRegistry.ts index c8ee4de..67f4609 100644 --- a/packages/blocky-core/src/registry/pluginRegistry.ts +++ b/packages/blocky-core/src/registry/pluginRegistry.ts @@ -45,6 +45,8 @@ export interface IPlugin { onInitialized?(context: PluginContext): void; onPaste?(evt: BlockyPasteEvent): void; + + dispose?(): void; } export class PluginContext { @@ -98,6 +100,8 @@ export class PluginRegistry { context.dispose(); this.contexts.delete(name); } + + plugin.dispose?.(); } initAllPlugins(editor: Editor) { @@ -114,4 +118,11 @@ export class PluginRegistry { plugin.onPaste?.({ ctx, raw: e }); } } + + dispose() { + const keys = Array.from(this.plugins.keys()); + for (const key of keys) { + this.unload(key); + } + } } diff --git a/packages/blocky-core/src/view/editor.spec.ts b/packages/blocky-core/src/view/editor.spec.ts new file mode 100644 index 0000000..1e4f4c4 --- /dev/null +++ b/packages/blocky-core/src/view/editor.spec.ts @@ -0,0 +1,22 @@ +import { describe, expect, it, vi } from "vitest"; +import { Editor } from "./editor"; +import { EditorController } from "./controller"; +import { PluginRegistry } from "@pkg/registry/pluginRegistry"; + +describe("Editor", () => { + it("dispose", () => { + const pluginRegistry = new PluginRegistry([]); + const editorController = new EditorController("user", { + pluginRegistry, + }); + const dom = document.createElement("div"); + const editor = Editor.fromController(dom, editorController); + pluginRegistry.initAllPlugins(editor); + + const disposeSpy = vi.spyOn(pluginRegistry, "dispose"); + + editor.dispose(); + + expect(disposeSpy).toBeCalledTimes(1); + }); +}); diff --git a/packages/blocky-core/src/view/editor.ts b/packages/blocky-core/src/view/editor.ts index 31dbc22..0896edd 100644 --- a/packages/blocky-core/src/view/editor.ts +++ b/packages/blocky-core/src/view/editor.ts @@ -1890,6 +1890,9 @@ export class Editor { "selectionchange", this.#selectionChangedHandler ); + + this.controller.pluginRegistry.dispose(); + flattenDisposable(this.disposables).dispose(); } }