GeezerVison is showing github.com — Release the graphs and the objects created by a story when the story is left · Issue #1325 · maxGraph/maxGraph
Release the graphs and the objects created by a story when the story is left #1325DescriptionIs your technical request related to a problem? Please describe.
No story of packages/html/stories releases what it creates. Each story builds its graph in render and returns a DOM element, and nothing runs when the user switches to another story: no graph.destroy(), no keyHandler.onDestroy(), no destroy() of the windows, toolbars, outlines or popup menus created by the story. Storybook keeps the preview iframe document when switching stories, so everything registered on document or window stays there, and the objects reachable from these listeners stay in memory.
Some examples:
new KeyHandler(graph) adds a keydown listener on document.documentElement (KeyHandler.ts). Each visit of a story using it leaves one more listener, which calls escape() on a graph no longer displayed. graph.destroy() would not help here, since KeyHandler is not a plugin (see Manage the key handling as a graph plugin #1323).
- The graph and its plugins register listeners on
document or window that are only removed by destroy(), for instance in GraphView, PanningHandler, SelectionHandler and CellEditorHandler. A graph that is never destroyed keeps its model, view and cell states alive through them.
- The global decorator of preview.ts resets the global configurations and, as a workaround introduced for MenuStyle Story: the context menu (MaxPopupMenu) is always displayed #400, removes the DOM elements of popup menus, tooltips and windows left by the previous story (
.mxPopupMenu, .mxTooltip, .mxWindow). The JavaScript objects behind them (MaxPopupMenu, TooltipHandler, MaxWindow) and their listeners are not released.
Besides the graph, several stories create other elements or add global listeners themselves: DynamicToolbar, OrgChart, Toolbar, Clipboard, ShowRegion, Window and Wires (from a search on new MaxWindow, new MaxPopupMenu, new MaxToolbar, new Outline, MaxLog and listeners on window or document).
Describe the solution you'd like
Each story should release what it created when the user leaves it.
Explore the possible solutions, compare them, then choose one. Releasing resources when a story is left is a generic need, so start with what Storybook and other projects already provide. The leads known so far:
beforeEach cleanup: Storybook calls the function returned by beforeEach (project, component or story level) when the story is left. Check that it runs in the Storybook UI when switching stories, not only in tests, and how it is ordered with the decorators.
- Storybook hooks:
useEffect from storybook/preview-api is available in decorators and stories of any renderer, its cleanup running when the story unmounts. Check that it works with @storybook/html-vite 10.
- Other projects using
@storybook/html or @storybook/web-components: how they release their resources (teardown helpers, beforeEach cleanup, channel events such as STORY_CHANGED, a registry of disposables).
- Framework renderers, as a reference: with React or Vue stories, the unmount of the story component (React
useEffect cleanup, Vue onUnmounted) destroys the graph. Migrating the stories is not an option, but it shows the expected behavior.
Other leads found on the way are welcome. The comparison should cover at least: stories with several graphs, the objects other than the graph (KeyHandler, Outline, MaxToolbar, MaxWindow, listeners added by the story), the amount of change in each story, and the behavior in the Storybook UI when switching stories and when changing the args.
An idea to start from, not the decided solution. It mainly shows that we are not blocked if nothing better is found: let each story register what it has to release, and let the Storybook preview release it when the story is left. Store cleanup functions rather than graphs: the graph is then one cleanup among others, which covers stories with several graphs, and objects that are not graphs (KeyHandler, Outline, MaxToolbar, MaxWindow, listeners added by the story). A sketch, names to be decided:
// packages/html/stories/shared/story-cleanup.ts
const cleanups: Array<() => void> = [];
export const registerStoryCleanup = (cleanup: () => void): void => {
cleanups.push(cleanup);
};
// convenience for the common case
export const registerGraphForCleanup = (graph: AbstractGraph, extraCleanup?: () => void): void => {
registerStoryCleanup(() => {
extraCleanup?.();
graph.destroy();
});
};
// called by .storybook/preview.ts
export const runStoryCleanups = (): void => {
// reverse order: what was created last is released first
while (cleanups.length > 0) {
try {
cleanups.pop()!();
} catch (error) {
console.error('Story cleanup failed', error);
}
}
};
In a story:
const graph = new Graph(container);
const keyHandler = new KeyHandler(graph);
registerGraphForCleanup(graph, () => keyHandler.onDestroy());
runStoryCleanups can be called:
- in the existing global decorator of
preview.ts, before resetting the configurations. It already runs before every render, so it needs no new Storybook feature, and it also runs when the args change, which re-renders the story with a new graph;
- or in the cleanup function returned by
beforeEach, depending on what the exploration finds.
Once every story registers its cleanups, the destroyUnreleasedElements workaround can become a safety net, or be removed.
Points to check, whatever the chosen solution:
- A global state, such as the list of the idea above, assumes that a single story is rendered at a time. This holds today: every story file exports a single
Default story, and the only MDX pages embedding a story (Boundary.mdx, Control.mdx) use a single <Canvas/>.
- Whether
graph.destroy() releases the popup menu and the tooltip of the PopupMenuHandler and TooltipHandler plugins.
- Whether Storybook ever reloads the iframe (docs mode, args change, HMR), which would change what leaks.
- How it fits with the
createGraphContainer options planned for KeyHandler does not work in Storybook stories #910. The container is created before the graph, so it cannot register the graph itself, which suggests a separate helper.
Describe alternatives you've considered
- A single global graph, as tried by a contributor in a fork (graph manager, wiring in preview.ts): a
graphManager object holds the container and the graph, a global decorator creates them from a setup function declared in the story parameters, and the cleanup returned by beforeEach removes the container and destroys the graph. It handles a single graph per story, does not pass the story args to the setup function, and does not release the other objects created by the story. The fork also restructures the stories and adds the Storybook Vitest addon, which is out of the scope of this issue.
Additional context
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|