Skip to content

Commit

Permalink
fix: dispose plugin registry
Browse files Browse the repository at this point in the history
vincentdchan committed Dec 5, 2023
1 parent dba653a commit fc49893
Showing 5 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/blocky-core/package.json
Original file line number Diff line number Diff line change
@@ -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",
36 changes: 33 additions & 3 deletions packages/blocky-core/src/registry/pluginRegistry.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
11 changes: 11 additions & 0 deletions packages/blocky-core/src/registry/pluginRegistry.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
22 changes: 22 additions & 0 deletions packages/blocky-core/src/view/editor.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 3 additions & 0 deletions packages/blocky-core/src/view/editor.ts
Original file line number Diff line number Diff line change
@@ -1890,6 +1890,9 @@ export class Editor {
"selectionchange",
this.#selectionChangedHandler
);

this.controller.pluginRegistry.dispose();

flattenDisposable(this.disposables).dispose();
}
}

1 comment on commit fc49893

@vercel
Copy link

@vercel vercel bot commented on fc49893 Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.