Skip to content

refactor!: extract the event names from InternalEvent into EventNames - #1231

Draft
redfish4ktc wants to merge 3 commits into
mainfrom
refactor/extract_event_names
Draft

redfish4ktc wants to merge 3 commits into
mainfrom
refactor/extract_event_names

Conversation

@redfish4ktc

Copy link
Copy Markdown
Member

Overview

InternalEvent is documented as cross-browser DOM event support, but it also held the 96 names of the events fired by maxGraph through EventSource, which are never DOM events. This PR moves them to a dedicated EventNames object, with the same names and values. InternalEvent keeps its DOM helpers, the handle indexes and PINCH_THRESHOLD.

Breaking change: InternalEvent.CLICK, InternalEvent.CELLS_MOVED and the other 94 names become EventNames.CLICK, EventNames.CELLS_MOVED... TypeScript reports every former access. In JavaScript, a former access returns undefined without error, so a listener registered with it never fires: the CHANGELOG entry says so and explains how to find them.

Changes

  • New EventNames object, declared as const (built-in names read-only), typed by the EventNamesMap interface derived from it, and the EventName union of the names.
  • EventSource.addListener takes EventName | (string & Record<never, never>): editors suggest the known names, any other string still compiles.
  • 371 references rewritten in the core, the tests and 17 stories; mxEvent.X replaced by EventNames.X in the JSDoc inherited from mxGraph.
  • Small fixes found on the way: the JSDoc of RESUME and SAVE described another event, two toolbar DOM listeners were typed with the instance type of InternalEvent (any value accepted), and event references in the JSDoc of GraphDataModel.
  • Size budgets of the examples lowered, see below.

Decisions worth reviewing

  • No deprecated aliases on InternalEvent. No bundler drops unused class statics, so aliases would keep all 96 names in every bundle and cancel the tree-shaking gain this change prepares.
  • Custom event names are not documented. Declaring them by augmenting EventNamesMap requires the added property to be optional, so every use of the name needs a !; asserting the type of EventNames instead would hide a forgotten runtime assignment. This is left to Check the event names passed to the listener API against the declared event names #1230, which will make the listener API require EventName.
  • The dead MOUSE_UP branch of the double-tap detection, rejected by tsc once the names have literal types, is kept with a cast and a comment pointing to Remove the double-tap branch of fireMouseEvent that can never run #1228.
  • migrate-from-mxgraph.md is left unchanged although it mentions InternalEvent.PAN_START and InternalEvent.PAN_END: its notice says it is not updated after 0.18.0 and sends readers to the CHANGELOG, which carries the migration.

Bundle size

Raw minified, kB. No bundler drops unused event names yet (the direct eval in doEval prevents the minifiers from working on the whole bundle, see #1226), but the shorter references already reduce every example:

Example Before After
js-example 466.88 463.99
js-example-selected-features 384.94 382.19
js-example-without-defaults 239.11 236.77
ts-example 428.73 426.93
ts-example-selected-features 361.53 359.78
ts-example-without-defaults 220.86 219.25

The measurements done while preparing the change (8 projects of maxgraph-integration-examples, with and without an indirect eval, Vite with terser) are in the task files of the first commit.

Tests

  • Core build, test-check, unit tests, circular dependencies, check:npm-package, lint.
  • ts-support (TypeScript 3.9): read-only built-in names, unknown names rejected, EventName union, event name accepted by addListener.
  • Storybook and website builds, examples built within their new budgets.
  • The 7 listeners registered with EventNames.* in the Monitor, Overlays and AutoLayout stories, triggered with real mouse input in a headless browser: all fire.

Notes

The first commit carries the task files of the work (exploration, plan, implementation notes, size prototypes) for the review; the second commit removes them so they do not reach main.

Follow-up issues: #1226 (indirect eval), #1227 (nativeDblClickEnabled), #1228 (dead MOUSE_UP branch), #1229 (handle indexes and PINCH_THRESHOLD), #1230 (require EventName in the listener API).

InternalEvent is documented as cross-browser DOM event support, but it also held the 96 names of the events fired by
maxGraph through EventSource, which are never DOM events. They now live in a dedicated EventNames object, with the same
names and values, while InternalEvent keeps the DOM helpers, the handle indexes and PINCH_THRESHOLD (their move is
tracked separately).

The object is declared `as const`, so the built-in names are read-only, and typed by the EventNamesMap interface derived
from it. That interface is empty by construction, hence the targeted disable of no-empty-object-type, and is kept an
interface so that a later change can rely on module augmentation. EventName, the union of the names, is built from
Required<EventNamesMap>, so that a property added as optional cannot bring undefined into it.

EventSource.addListener now types the event name as `EventName | (string & Record<never, never>)`, the pattern of
PluginId and StyleArrowValue: editors suggest the known names while any other string still compiles. Requiring
EventName, and documenting how applications declare their own names, is left to a later change. Declaring them by
augmenting EventNamesMap was tried and not documented: the added property has to be optional, otherwise the type check
of EventNames itself fails when maxGraph is compiled with the application, so every use of the name needs a non-null
assertion. Asserting the type of the initializer instead would remove it, but TypeScript would then trust that the
application assigned the name, and a forgotten assignment would only show at runtime as a listener that never fires.

The literal types make tsc reject the MOUSE_UP comparison nested in the MOUSE_DOWN branch of the double-tap detection,
which can never be true. It is kept as is with a cast, its removal is tracked separately.

Deprecated aliases on InternalEvent were considered and rejected: no bundler drops unused class statics, so they would
keep every name in every bundle and cancel the tree-shaking gain the extraction prepares. With the current direct eval
in doEval, no bundler drops unused names yet, but the shorter references already reduce the examples (kB):

| Example | Before | After |
| --- | --- | --- |
| js-example | 466.88 | 463.99 |
| js-example-selected-features | 384.94 | 382.19 |
| js-example-without-defaults | 239.11 | 236.77 |
| ts-example | 428.73 | 426.93 |
| ts-example-selected-features | 361.53 | 359.78 |
| ts-example-without-defaults | 220.86 | 219.25 |

The size budgets of the examples are lowered accordingly.

The JSDoc inherited from mxGraph now names the events with EventNames.X instead of mxEvent.X, the RESUME and SAVE
entries no longer describe another event, and the toolbar DOM listeners typed with the instance type of InternalEvent,
which accepted any value, now take an Event.

BREAKING CHANGE:
- The 96 event-name static properties of InternalEvent (CLICK, CHANGE, CELLS_MOVED...) no longer exist. Use the same
  names on EventNames instead. In JavaScript, the former accesses return undefined without any error, so a listener
  registered with them never fires.
The exploration, plan, implementation notes and size prototypes of the task stay readable in the history of the pull
request, but must not reach the main branch.
@redfish4ktc redfish4ktc added the chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...) label Oct 7, 2026
@coderabbitai

coderabbitai Bot commented Oct 7, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

// Checks the types of EventNames, of the EventName union and of the event name accepted by EventSource.addListener.
import { EventName, EventNames, EventSource } from '@maxgraph/core';

const builtInEventName: EventName = EventNames.CLICK;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Intended: ts-support only runs tsc, so these variables exist to make the compiler check the type of the assigned value, and undeclaredEventName and unknownEventName carry a @ts-expect-error that fails the build if the error disappears. module-augmentation.ts in the same package follows the same pattern.

// @ts-expect-error a built-in event name is read-only
EventNames.CLICK = 'anotherValue';
// @ts-expect-error 'AN_UNDECLARED_EVENT' does not exist in type 'EventNamesMap'
const undeclaredEventName = EventNames.AN_UNDECLARED_EVENT;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Intended: ts-support only runs tsc, so these variables exist to make the compiler check the type of the assigned value, and undeclaredEventName and unknownEventName carry a @ts-expect-error that fails the build if the error disappears. module-augmentation.ts in the same package follows the same pattern.

// @ts-expect-error 'AN_UNDECLARED_EVENT' does not exist in type 'EventNamesMap'
const undeclaredEventName = EventNames.AN_UNDECLARED_EVENT;
// @ts-expect-error an undeclared value is not an event name
const unknownEventName: EventName = 'anUndeclaredEvent';

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Intended: ts-support only runs tsc, so these variables exist to make the compiler check the type of the assigned value, and undeclaredEventName and unknownEventName carry a @ts-expect-error that fails the build if the error disappears. module-augmentation.ts in the same package follows the same pattern.

The only use of `constants` in the story is in a commented-out line, so the import was dead. It was reported on the
import line already modified to replace InternalEvent by EventNames.
@sonarqubecloud

sonarqubecloud Bot commented Oct 7, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant