-
Notifications
You must be signed in to change notification settings - Fork 10
feat(module-event): add EventModuleConfigurator, waitForEvent/watchEvents, and operators subpath #5241
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
Merged
Merged
feat(module-event): add EventModuleConfigurator, waitForEvent/watchEvents, and operators subpath #5241
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a078524
feat(module-event): add EventModuleConfigurator, waitForEvent/watchEv…
odinr c011377
docs(vue-press): align event module docs with docs/ folder, move Reac…
odinr 09955d3
style(module-event): apply Biome formatting fixes flagged by reviewdog
odinr e584b84
fix(module-event): fix waitForEvent TDZ on sync completion, correct d…
odinr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@equinor/fusion-framework-docs": patch | ||
| --- | ||
|
|
||
| Align the event module's vue-press documentation with its `docs/` folder: `README.md` and the | ||
| new `docs/{configuration,observable-patterns,lifecycle,testing}.md` pages now `@include` the | ||
| package's own docs instead of duplicating content, matching the `http`/`module` pattern. | ||
|
|
||
| The event module's React bindings page moves from `event/react.md` to `react/event/README.md`, | ||
| alongside `react/router/`, and is now an `@include` of `@equinor/fusion-framework-react-module-event`'s | ||
| README. The sidebar is updated to match. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| "@equinor/fusion-framework-module-event": minor | ||
| --- | ||
|
|
||
| Add `EventModuleConfigurator`, a `BaseConfigBuilder`-based configurator with fluent | ||
| `setOnDispatch`/`setOnBubble` setters, replacing direct property assignment on the config | ||
| object. | ||
|
|
||
| ```ts | ||
| // Before (still works, but deprecated) | ||
| config.event.onDispatch = (event) => { ... }; | ||
| delete config.event.onBubble; | ||
|
|
||
| // After | ||
| configurator.setOnDispatch((event) => { ... }); | ||
| configurator.setOnBubble(undefined); | ||
| ``` | ||
|
|
||
| `IEventModuleConfigurator` is renamed to `EventModuleConfig` and converted from an `interface` | ||
| to a `type`. `IEventModuleConfigurator` is kept as a deprecated type alias for backward | ||
| compatibility. | ||
|
|
||
| Also narrows the `dispatchEvent` return type for registered {@link FrameworkEventMap} keys and | ||
| pre-constructed event instances, so callers get back the specific event type instead of the | ||
| generic `FrameworkEvent`. | ||
|
|
||
| **Deprecated (since 6.1.0), no migration required yet:** | ||
| - `EventModuleConfigurator#onDispatch`/`#onBubble` property assignment — use `setOnDispatch`/`setOnBubble`. | ||
| - `IEventModuleConfigurator` type — use `EventModuleConfig`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --- | ||
| "@equinor/fusion-framework-module-event": minor | ||
| --- | ||
|
|
||
| Add `waitForEvent` and `watchEvents` helper utilities and `./operators` subpath. | ||
|
|
||
| **New subpath exports:** | ||
|
|
||
| ```ts | ||
| import { filterEvent } from '@equinor/fusion-framework-module-event/operators'; | ||
| import { waitForEvent, watchEvents } from '@equinor/fusion-framework-module-event/utils'; | ||
| ``` | ||
|
|
||
| ### `waitForEvent(provider, matcher, options?)` | ||
|
|
||
| Resolves with the next event matching `matcher`. Accepts a single event type string (uses the type-scoped `filterEvent` path and preserves type narrowing), an array of type strings, or a predicate function. Supports an optional `timeout` (ms) and `AbortSignal` so a test cannot hang indefinitely. | ||
|
|
||
| ```ts | ||
| // Single type — typed result | ||
| const event = await waitForEvent(provider, 'onModulesLoaded'); | ||
|
|
||
| // Array of types | ||
| const event = await waitForEvent(provider, ['myFeature.saved', 'myFeature.updated']); | ||
|
|
||
| // Predicate matching on payload | ||
| const event = await waitForEvent(provider, (e) => e.detail?.id === 1); | ||
|
|
||
| // With timeout | ||
| const event = await waitForEvent(provider, 'myFeature.saved', { timeout: 1000 }); | ||
| ``` | ||
|
|
||
| ### `watchEvents(provider, matcher)` | ||
|
|
||
| Collects all events matching `matcher` into an array. Only matching events are ever stored — a high volume of non-matching dispatches does not cause unbounded memory growth. Returns a handle with `events`, `lastEvent(type?)`, and `dispose()`. | ||
|
|
||
| ```ts | ||
| const handle = watchEvents(provider, ['myFeature.saved', 'myFeature.deleted']); | ||
| // ... run code under test ... | ||
| expect(handle.lastEvent('myFeature.saved')?.detail).toEqual({ id: 1 }); | ||
| handle.dispose(); | ||
| ``` | ||
|
|
||
| ### `./operators` subpath | ||
|
|
||
| `filterEvent` is now also exported from `@equinor/fusion-framework-module-event/operators`. The existing root export is preserved — no migration required for current consumers. | ||
|
|
||
| Resolves [equinor/fusion-core-tasks#1656](https://github.com/equinor/fusion-core-tasks/issues/1656). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # Configuration | ||
|
|
||
| ## Dispatch hooks | ||
|
|
||
| The event module is configured through `EventModuleConfigurator`, a `BaseConfigBuilder` with | ||
| fluent `setOnDispatch`/`setOnBubble` setters: | ||
|
|
||
| ```ts | ||
| import { EventModuleConfigurator } from '@equinor/fusion-framework-module-event'; | ||
|
|
||
| const doNotHandleEvents = ['onMyEvent']; | ||
| const doNotPropagateEvents = ['myOtherEvent']; | ||
|
|
||
| const configurator = new EventModuleConfigurator(); | ||
|
|
||
| // Inspect or cancel events before listeners run | ||
| configurator.setOnDispatch((event) => { | ||
| if (doNotHandleEvents.includes(event.type)) { | ||
| event.preventDefault(); | ||
| } | ||
| if (doNotPropagateEvents.includes(event.type)) { | ||
| event.stopPropagation(); | ||
| } | ||
| }); | ||
|
|
||
| // Disable bubbling to parent providers | ||
| configurator.setOnBubble(undefined); | ||
| ``` | ||
|
|
||
| > **Deprecated:** assigning `configurator.onDispatch`/`configurator.onBubble` directly still | ||
| > works but is deprecated since `6.1.0` — use `setOnDispatch`/`setOnBubble` instead. | ||
|
|
||
| ### `onDispatch` | ||
|
|
||
| Called **before** registered listeners. Use it to log, validate, or cancel events globally. | ||
|
|
||
| ### `onBubble` | ||
|
|
||
| Called **after** all listeners if the event still bubbles. By default, the framework wires this to forward events to the parent provider. Pass `undefined` to isolate events to the current scope. | ||
|
|
||
| ## Registering custom event types | ||
|
|
||
| Extend `FrameworkEventMap` via TypeScript declaration merging to get type-safe `addEventListener` and `dispatchEvent` calls. Declaring the map entry adds type hinting only — it does not add any runtime behavior: | ||
|
|
||
| ```ts | ||
| import type { | ||
| FrameworkEvent, | ||
| FrameworkEventInit, | ||
| } from '@equinor/fusion-framework-module-event'; | ||
|
|
||
| interface MyPayload { | ||
| id: string; | ||
| value: number; | ||
| } | ||
|
|
||
| declare module '@equinor/fusion-framework-module-event' { | ||
| interface FrameworkEventMap { | ||
| 'myFeature': FrameworkEvent<FrameworkEventInit<MyPayload>>; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| After registration, both the event name and payload are type-checked: | ||
|
|
||
| ```ts | ||
| modules.event.addEventListener('myFeature', (event) => { | ||
| // event.detail is typed as MyPayload | ||
| console.log(event.detail.id); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Custom event classes | ||
|
|
||
| For behavior beyond a typed `detail`, subclass `FrameworkEvent` directly: | ||
|
|
||
| ```ts | ||
| class MyEvent extends FrameworkEvent<MyPayload, MySource> { | ||
| constructor(readonly obj: MyObj, init: FrameworkEventInit<MyPayload, MySource>) { | ||
| super('onMyEvent', init); | ||
| } | ||
| } | ||
|
|
||
| // add type hinting | ||
| declare module '@equinor/fusion-framework-module-event' { | ||
| interface FrameworkEventMap { | ||
| onMyEvent: MyEvent; | ||
| } | ||
| } | ||
|
|
||
| modules.event.dispatchEvent(new MyEvent(someObj, { detail, source })); | ||
|
|
||
| modules.event.addEventListener('onMyEvent', (event) => { | ||
| console.log('is my custom event:', event instanceof MyEvent); | ||
| console.log('my custom obj', event.obj); | ||
| }); | ||
| ``` | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.