Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(events): introduce EventType enum in separate module #8530

Merged
merged 2 commits into from
Aug 20, 2024

Conversation

cpcallen
Copy link
Contributor

The basics

The details

Proposed Changes

Introduce an enum for the event .type values.

Reason for Changes

Although we can't actually use it as the type of the .type property on the Abstract event class, because we want to allow developers to add their own custom event types inheriting from this type, this change means we can be reasonably sure that all of our own event subclasses have distinct .type values—plus consistent use of enum syntax (EventType.TYPE_NAME) is probably good for readability overall.

Additionally, by putting it in a separate module from the rest of events/utils.ts, it moves us a step towards being able to write type narrowing checks like

if (event instanceof SomeEventType) 

in utils.ts, which is not currently possible due to load ordering problems that arise when there are (non-type) circular imports between utils.ts and the other modules in events/.

(A few of the event classes also depend on utils.ts for fire() or other helper functions; this will be harder to untangle, but at least this commit is a step forward in terms of reducing the circularity of of our dependencies, making most of the event subclass modules dependent on type.ts, which has no imports, rather than on utils.ts which has multiple imports.)

Test Coverage

Passes npm test; no changes to manual testing expected.

Documentation

No; API-exported symbols remain unchanged.

Additional Information

I've also restructured events/events.ts to use expor … from … for readability and conciseness.

Introduce an enum for the event .type values.  We can't actually
use it as the type of the .type property on Abstract events,
because we want to allow developers to add their own custom
event types inheriting from this type, but at least this way we
can be reasonably sure that all of our own event subclasses have
distinct .type values—plus consistent use of enum syntax
(EventType.TYPE_NAME) is probably good for readability overall.

Put it in a separate module from the rest of events/utils.ts
because it would be helpful if event utils could use

    event instanceof SomeEventType

for type narrowing but but at the moment most events are in
modules that depend on events/utils.ts for their .type
constant, and although circular ESM dependencies should work
in principle there are various restrictions and this
particular circularity causes issues at the moment.

A few of the event classes also depend on utils.ts for fire()
or other functions, which will be harder to deal with, but at
least this commit is win in terms of reducing the complexity
of our dependencies, making most of the Abstract event subclass
module dependent on type.ts, which has no imports, rather than
on utils.ts which has multiple imports.
@cpcallen cpcallen merged commit 7ccdcc5 into google:develop Aug 20, 2024
11 checks passed
@cpcallen cpcallen deleted the refactor/event-type branch August 20, 2024 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants