You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: agent-docs/design/cross-module-communication.md
+15-6Lines changed: 15 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,16 +7,25 @@ coordination happens through the FireflyRuntime API.
7
7
8
8
## Event Bus
9
9
10
-
Pub/sub messaging for decoupled communication between modules:
10
+
Pub/sub messaging for decoupled communication between modules. Events are type-safe via module augmentation of the `EventMap` interface (same pattern as `EnvironmentVariables` and `FeatureFlags`). All Squide native events are pre-augmented; consumer apps add their own events:
The event bus is type-safe via module augmentation of the `EventMap` interface (same pattern as `EnvironmentVariables` and `FeatureFlags`). Events must be declared in `EventMap` before they can be dispatched or listened to.
226
+
227
+
### TypeScript Augmentation (EventMap Interface)
228
+
229
+
```ts
230
+
// types/event-map.d.ts
231
+
import"@squide/firefly";
232
+
233
+
declaremodule"@squide/firefly" {
234
+
interfaceEventMap {
235
+
"tenant-changed": { tenantId:string };
236
+
"theme-updated": { mode:"light"|"dark" };
237
+
}
238
+
}
239
+
```
240
+
241
+
All Squide native events (bootstrapping lifecycle, data fetching, AppRouter state) are pre-augmented and already type-safe.
@@ -48,8 +48,72 @@ import { useEventBusDispatcher } from "@squide/firefly";
48
48
49
49
const dispatch =useEventBusDispatcher();
50
50
51
-
// Dispatch a "foo" event with a "bar" payload.
52
-
dispatch("foo", "bar");
51
+
// Dispatch a "show-toast" event with a string payload.
52
+
dispatch("show-toast", "Hello!");
53
53
```
54
54
55
+
## Setup the typings
55
56
57
+
Before dispatching or listening to events, modules should [augment](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) the [EventMap](../reference/messaging/EventMap.md) interface with the events they intend to use to ensure type safety and autocompletion.
58
+
59
+
First, create a types folder in the project:
60
+
61
+
```!#7-8
62
+
project
63
+
├── src
64
+
├────── register.tsx
65
+
├────── Page.tsx
66
+
├────── index.tsx
67
+
├────── App.tsx
68
+
├── types
69
+
├────── event-map.d.ts
70
+
```
71
+
72
+
Then create an `event-map.d.ts` file:
73
+
74
+
```ts !#6 project/types/event-map.d.ts
75
+
import"@squide/firefly";
76
+
77
+
declaremodule"@squide/firefly" {
78
+
interfaceEventMap {
79
+
// Each entry maps an event name to its payload type.
80
+
"show-toast":string;
81
+
}
82
+
}
83
+
```
84
+
85
+
Finally, update the project `tsconfig.json` to include the `types` folder:
The `EventMap` interface defines the shape and types of the event names and payload values that modules dispatch and listen to through the [FireflyRuntime](../runtime/FireflyRuntime.md) instance event bus.
10
+
11
+
Consumer applications are expected to [augment](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) this interface to declare the events they intend to use, providing a fully type-safe experience when working with the event bus.
12
+
13
+
All Squide native events (bootstrapping lifecycle, data fetching, AppRouter state transitions) are pre-augmented and already type-safe. You only need to augment `EventMap` for your own application events.
14
+
15
+
## Augment the interface
16
+
17
+
First, create a types folder in the project:
18
+
19
+
```!#7-8
20
+
project
21
+
├── src
22
+
├────── register.tsx
23
+
├────── Page.tsx
24
+
├────── index.tsx
25
+
├────── App.tsx
26
+
├── types
27
+
├────── event-map.d.ts
28
+
```
29
+
30
+
Then create an `event-map.d.ts` file:
31
+
32
+
```ts !#6 project/types/event-map.d.ts
33
+
import"@squide/firefly";
34
+
35
+
declaremodule"@squide/firefly" {
36
+
interfaceEventMap {
37
+
// Each entry maps an event name to its payload type.
38
+
"show-toast":string;
39
+
}
40
+
}
41
+
```
42
+
43
+
Finally, update the project `tsconfig.json` to include the `types` folder:
0 commit comments