Repository navigation
refactor!: extract the event names from InternalEvent into EventNames - #1231
redfish4ktc wants to merge 3 commits into
Conversation
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.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
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. Comment |
| // 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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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.
|



Overview
InternalEventis documented as cross-browser DOM event support, but it also held the 96 names of the events fired by maxGraph throughEventSource, which are never DOM events. This PR moves them to a dedicatedEventNamesobject, with the same names and values.InternalEventkeeps its DOM helpers, the handle indexes andPINCH_THRESHOLD.Breaking change:
InternalEvent.CLICK,InternalEvent.CELLS_MOVEDand the other 94 names becomeEventNames.CLICK,EventNames.CELLS_MOVED... TypeScript reports every former access. In JavaScript, a former access returnsundefinedwithout error, so a listener registered with it never fires: the CHANGELOG entry says so and explains how to find them.Changes
EventNamesobject, declaredas const(built-in names read-only), typed by theEventNamesMapinterface derived from it, and theEventNameunion of the names.EventSource.addListenertakesEventName | (string & Record<never, never>): editors suggest the known names, any other string still compiles.mxEvent.Xreplaced byEventNames.Xin the JSDoc inherited from mxGraph.RESUMEandSAVEdescribed another event, two toolbar DOM listeners were typed with the instance type ofInternalEvent(any value accepted), and event references in the JSDoc ofGraphDataModel.Decisions worth reviewing
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.EventNamesMaprequires the added property to be optional, so every use of the name needs a!; asserting the type ofEventNamesinstead 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 requireEventName.MOUSE_UPbranch 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.mdis left unchanged although it mentionsInternalEvent.PAN_STARTandInternalEvent.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
evalindoEvalprevents the minifiers from working on the whole bundle, see #1226), but the shorter references already reduce every example: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
test-check, unit tests, circular dependencies,check:npm-package, lint.ts-support(TypeScript 3.9): read-only built-in names, unknown names rejected,EventNameunion, event name accepted byaddListener.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 (deadMOUSE_UPbranch), #1229 (handle indexes andPINCH_THRESHOLD), #1230 (requireEventNamein the listener API).