Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(mind_map): unsubscribe from the bus a listener was added to
Elian Dorana5bcd6dc97fd
diff --git a/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.spec.tsx b/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.spec.tsxindex 053c493703..5a6d032d2a 100644--- a/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.spec.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.spec.tsx@@ -1,5 +1,6 @@import i18next from "i18next";import type { MindElixirInstance, NodeObj, Topic } from "mind-elixir";+import { render } from "preact";import { act } from "preact/test-utils";import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";@@ -153,4 +154,17 @@ describe("MapContextMenu", () => {// Given up all the same: the click was spent, and a hint left standing would be a lie.expect(hint(container)).toBeNull();});++ it("stops listening to the map's bus after the map is destroyed", () => {+ const { mind } = buildMind();+ const { bus } = mind;+ const removeListener = vi.spyOn(bus, "removeListener");+ const container = renderMenu(mind);++ // `MindElixirInstance.destroy()` clears `bus` before the overlay drawn over the map unmounts.+ (mind as { bus?: unknown }).bus = undefined;+ expect(() => act(() => render(null, container))).not.toThrow();++ expect(removeListener).toHaveBeenCalledWith("showContextMenu", expect.any(Function));+ });});diff --git a/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.tsx b/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.tsxindex dcf1cbf5c4..54021d648e 100644--- a/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/MapContextMenu.tsx@@ -46,8 +46,10 @@ function useNodeMenu(mind: MindElixirInstance, requestArrow: (request: ArrowRequ});};- mind.bus.addListener("showContextMenu", show);- return () => mind.bus.removeListener("showContextMenu", show);+ const { bus } = mind;+ bus.addListener("showContextMenu", show);+ // `MindElixirInstance.destroy()` clears `bus` before this cleanup runs.+ return () => bus.removeListener("showContextMenu", show);}, [ mind, requestArrow ]);}diff --git a/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.spec.tsx b/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.spec.tsxindex c3e47b7e2c..39c1508158 100644--- a/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.spec.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.spec.tsx@@ -1,4 +1,4 @@-import type { ComponentChild } from "preact";+import { type ComponentChild, render } from "preact";import type { MindElixirInstance } from "mind-elixir";import { act } from "preact/test-utils";import { beforeEach, describe, expect, it, vi } from "vitest";@@ -261,6 +261,22 @@ describe("MapToolbar", () => {});});+describe("map listeners", () => {+ it("stop listening to the map's bus after the map is destroyed", () => {+ const mind = buildMind();+ const { bus } = mind;+ const removeListener = vi.spyOn(bus, "removeListener");+ const container = renderBar(<><MapToolbar mind={mind} /><DirectionToolbar mind={mind} /></>);++ // `MindElixirInstance.destroy()` clears `bus` before the overlay drawn over the map unmounts.+ (mind as { bus?: unknown }).bus = undefined;+ expect(() => act(() => render(null, container))).not.toThrow();++ expect(removeListener).toHaveBeenCalledWith("scale", expect.any(Function));+ expect(removeListener).toHaveBeenCalledWith("linkDiv", expect.any(Function));+ });+});+describe("DirectionToolbar", () => {// The three the map's own bar offered, and the downward one Mind Elixir added without ever// putting it in that bar.diff --git a/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsx b/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsxindex c970fdf0e1..a9625339e6 100644--- a/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsx@@ -150,8 +150,10 @@ function useMapScale(mind: MindElixirInstance) {// The map may have been moved between being built and being listened to.setScale(mind.scaleVal);- mind.bus.addListener("scale", setScale);- return () => mind.bus.removeListener("scale", setScale);+ const { bus } = mind;+ bus.addListener("scale", setScale);+ // `MindElixirInstance.destroy()` clears `bus` before this cleanup runs.+ return () => bus.removeListener("scale", setScale);}, [ mind ]);return scale;@@ -187,8 +189,10 @@ function useMapState<T>(mind: MindElixirInstance, read: (mind: MindElixirInstancconst report = () => setValue(() => readRef.current(mind));report();- mind.bus.addListener("linkDiv", report);- return () => mind.bus.removeListener("linkDiv", report);+ const { bus } = mind;+ bus.addListener("linkDiv", report);+ // `MindElixirInstance.destroy()` clears `bus` before this cleanup runs.+ return () => bus.removeListener("linkDiv", report);}, [ mind ]);return value;diff --git a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsxindex 17fa4f3ef3..d2e14cef8c 100644--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx@@ -311,9 +311,11 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro};dressNodes();- mind.bus.addListener("linkDiv", dressNodes);+ const { bus } = mind;+ bus.addListener("linkDiv", dressNodes);- return () => mind.bus.removeListener("linkDiv", dressNodes);+ // The instance is destroyed by the effect above, whose cleanup runs first and clears `bus`.+ return () => bus.removeListener("linkDiv", dressNodes);}, [ instanceVersion ]);// Selection listener. The events only carry the nodes that were added to or removed from the@@ -325,16 +327,17 @@ function MindElixir({ children, containerRef: externalContainerRef, containerProconst reportSelection = () => onSelectionChange(mind.currentNodes.map(({ nodeObj }) => nodeObj));reportSelection();- mind.bus.addListener("selectNodes", reportSelection);- mind.bus.addListener("unselectNodes", reportSelection);+ const { bus } = mind;+ bus.addListener("selectNodes", reportSelection);+ bus.addListener("unselectNodes", reportSelection);// Operations edit the node objects in place, leaving the selection pointing at the same// ones. Report it again so that what is shown of a selected node follows its edits.- mind.bus.addListener("operation", reportSelection);+ bus.addListener("operation", reportSelection);return () => {- mind.bus.removeListener("selectNodes", reportSelection);- mind.bus.removeListener("unselectNodes", reportSelection);- mind.bus.removeListener("operation", reportSelection);+ bus.removeListener("selectNodes", reportSelection);+ bus.removeListener("unselectNodes", reportSelection);+ bus.removeListener("operation", reportSelection);};}, [ onSelectionChange, instanceVersion ]);