Skip to content

Commit c47b7b2

Browse files
committed
Reworking addEventListener to avoid union types in .NET, events now inherit from a common ancestor ApiEvent
1 parent e72eabf commit c47b7b2

6 files changed

Lines changed: 84 additions & 66 deletions

File tree

docs/api/ref/DesktopAgent.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface DesktopAgent {
4141
leaveCurrentChannel() : Promise<void>;
4242

4343
// non-context events
44-
addEventListener(type: FDC3EventType | null, handler: EventHandler): Promise<Listener>;
44+
addEventListener(type: FDC3EventTypes | null, handler: EventHandler): Promise<Listener>;
4545

4646
//implementation info
4747
getInfo(): Promise<ImplementationMetadata>;
@@ -96,7 +96,7 @@ const contactListener = await fdc3.addContextListener('fdc3.contact', (contact,
9696
9797
### `addEventListener`
9898
```ts
99-
addEventListener(type: FDC3EventType | null, handler: EventHandler): Promise<Listener>;
99+
addEventListener(type: FDC3EventTypes | null, handler: EventHandler): Promise<Listener>;
100100
```
101101
102102
Registers a handler for non-context and non-intent events from the Desktop Agent. If the consumer is only interested in an event of a particular type, they can specify that type. If the consumer is able to receive events of any type or will inspect types received, then they can pass `null` as the `type` parameter to receive all event types.
@@ -110,13 +110,17 @@ Whenever the handler function is called it will be passed an event object with d
110110
const listener = await fdc3.addEventListener(null, event => { ... });
111111
112112
// listener for a specific event type that logs its details
113-
const userChannelChangedListener = await fdc3.addEventListener(FDC3EventType.USER_CHANNEL_CHANGED, event => {
113+
const userChannelChangedListener = await fdc3.addEventListener("USER_CHANNEL_CHANGED", event => {
114114
console.log(`Received event ${event.type}\n\tDetails: ${event.details}`);
115115
//do something else with the event
116116
});
117-
````
117+
```
118118
119+
**See also:**
119120
121+
- [`FDC3EventTypes`](./Events#fdc3eventtypes)
122+
- [`FDC3Event`](./Events#fdc3event)
123+
- [`EventHandler`](./Events#eventhandler)
120124
121125
### `addIntentListener`
122126

docs/api/ref/Events.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@ title: Events
44

55
In addition to intent and context events, the FDC3 API and PrivateChannel API may be used to listen for other types of events via their `addEventListener()` functions.
66

7+
## `ApiEvent`
8+
9+
Type defining a basic event object that may be emitted by an FDC3 API interface such as DesktopAgent or PrivateChannel. There are more specific event types defined for each interface.
10+
11+
```ts
12+
interface ApiEvent {
13+
readonly type: string;
14+
readonly details: any;
15+
}
16+
```
17+
18+
**See also:**
19+
20+
- [`FDC3Event`](#fdc3event)
21+
- [`PrivateChannelEvent`](#privatechannelevent)
22+
723
## `EventHandler`
824

925
```ts
10-
type EventHandler = (event: FDC3Event | PrivateChannelEvent) => void;
26+
type EventHandler = (event: ApiEvent) => void;
1127
```
1228

1329
Describes a callback that handles non-context and non-intent events. Provides the details of the event.
@@ -16,28 +32,27 @@ Used when attaching listeners to events.
1632

1733
**See also:**
1834

19-
- [`DesktopAgent.addEventListener`](DesktopAgent#addEventListener)
20-
- [`FDC3Event`](#fdc3event)
35+
- [`DesktopAgent.addEventListener`](DesktopAgent#addeventlistener)
36+
- [`PrivateChannel.addEventListener`](PrivateChannel#addeventlistener)
37+
- [`ApiEvent`](#apievent)
2138

22-
## `FDC3EventType`
39+
## `FDC3EventTypes`
2340

2441
```ts
25-
enum FDC3EventType {
26-
USER_CHANNEL_CHANGED = "USER_CHANNEL_CHANGED"
27-
}
42+
type FDC3EventTypes = "USER_CHANNEL_CHANGED";
2843
```
2944

30-
Enumeration defining the types of (non-context and non-intent) events that may be received via the FDC3 API's `addEventListener` function.
45+
Type defining valid type strings for DesktopAgent interface events.
3146

3247
**See also:**
3348

34-
- [`DesktopAgent.addEventListener`](DesktopAgent#addEventListener)
49+
- [`DesktopAgent.addEventListener`](DesktopAgent#addeventlistener)
3550

3651
## `FDC3Event`
3752

3853
```ts
39-
interface FDC3Event {
40-
readonly type: FDC3EventType;
54+
interface FDC3Event extends ApiEvent{
55+
readonly type: FDC3EventTypes;
4156
readonly details: any;
4257
}
4358
```
@@ -48,14 +63,14 @@ Events will always include both `type` and `details` properties, which describe
4863

4964
**See also:**
5065

51-
- [`DesktopAgent.addEventListener`](DesktopAgent#addEventListener)
52-
- [`FDC3EventType`](#fdc3eventtype)
66+
- [`DesktopAgent.addEventListener`](DesktopAgent#addeventlistener)
67+
- [`FDC3EventTypes`](#fdc3eventtypes)
5368

5469
### `FDC3ChannelChangedEvent`
5570

5671
```ts
5772
interface FDC3ChannelChangedEvent extends FDC3Event {
58-
readonly type: FDC3EventType.USER_CHANNEL_CHANGED;
73+
readonly type: "USER_CHANNEL_CHANGED";
5974
readonly details: {
6075
currentChannelId: string | null
6176
};
@@ -66,27 +81,23 @@ Type representing the format of USER_CHANNEL_CHANGED events.
6681

6782
The identity of the channel joined is provided as `details.currentChannelId`, which will be `null` if the app is no longer joined to any channel.
6883

69-
## `PrivateChannelEventType`
84+
## `PrivateChannelEventTypes`
7085

7186
```ts
72-
enum PrivateChannelEventType {
73-
ADD_CONTEXT_LISTENER = "addContextListener",
74-
UNSUBSCRIBE = "unsubscribe",
75-
DISCONNECT = "disconnect"
76-
}
87+
type PrivateChannelEventTypes = "addContextListener" | "unsubscribe" | "disconnect";
7788
```
7889

79-
Enumeration defining the types of (non-context and non-intent) events that may be received via a PrivateChannel's `addEventListener` function.
90+
Type defining valid type strings for Private Channel events.
8091

8192
**See also:**
8293

83-
- [`PrivateChannel.addEventListener`](PrivateChannel#addEventListener)
94+
- [`PrivateChannel.addEventListener`](PrivateChannel#addeventlistener)
8495

8596
## `PrivateChannelEvent`
8697

8798
```ts
88-
interface PrivateChannelEvent {
89-
readonly type: PrivateChannelEventType;
99+
interface PrivateChannelEvent extends ApiEvent {
100+
readonly type: PrivateChannelEventTypes;
90101
readonly details: any;
91102
}
92103
```
@@ -95,14 +106,14 @@ Type defining the format of event objects that may be received via a PrivateChan
95106

96107
**See also:**
97108

98-
- [`PrivateChannel.addEventListener`](PrivateChannel#addEventListener)
99-
- [`PrivateChannelEventType`](#privatechanneleventtype)
109+
- [`PrivateChannel.addEventListener`](PrivateChannel#addeventlistener)
110+
- [`PrivateChannelEventTypes`](#privatechanneleventtypes)
100111

101112
### `PrivateChannelAddContextListenerEvent`
102113

103114
```ts
104115
interface PrivateChannelAddContextListenerEvent extends PrivateChannelEvent {
105-
readonly type: PrivateChannelEventType.ADD_CONTEXT_LISTENER;
116+
readonly type: "addContextListener";
106117
readonly details: {
107118
contextType: string | null
108119
};
@@ -117,7 +128,7 @@ The context type of the listener added is provided as `details.contextType`, whi
117128

118129
```ts
119130
interface PrivateChannelUnsubscribeEvent extends PrivateChannelEvent {
120-
readonly type: PrivateChannelEventType.UNSUBSCRIBE;
131+
readonly type: "unsubscribe";
121132
readonly details: {
122133
contextType: string | null
123134
};
@@ -132,7 +143,7 @@ The context type of the listener removed is provided as `details.contextType`,
132143

133144
```ts
134145
export interface PrivateChannelDisconnectEvent extends PrivateChannelEvent {
135-
readonly type: PrivateChannelEventType.DISCONNECT;
146+
readonly type: "disconnect";
136147
readonly details: null | undefined;
137148
}
138149
```

docs/api/ref/PrivateChannel.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It is intended that Desktop Agent implementations:
1717
```ts
1818
interface PrivateChannel extends Channel {
1919
// functions
20-
addEventListener(type: PrivateChannelEventType | null, handler: EventHandler): Promise<Listener>;
20+
addEventListener(type: PrivateChannelEventTypes | null, handler: EventHandler): Promise<Listener>;
2121
disconnect(): Promise<void>;
2222

2323
//deprecated functions
@@ -120,7 +120,7 @@ try {
120120
### `addEventListener`
121121

122122
```ts
123-
addEventListener(type: PrivateChannelEventType | null, handler: EventHandler): Promise<Listener>;
123+
addEventListener(type: PrivateChannelEventTypes | null, handler: EventHandler): Promise<Listener>;
124124
```
125125

126126
Register a handler for events from the PrivateChannel. Whenever the handler function is called it will be passed an event object with details related to the event.
@@ -142,7 +142,8 @@ const channelChangedListener: Listener = await myPrivateChannel.addEventListener
142142

143143
**See also:**
144144

145-
- [`PrivateChannelEventType`](./Events#privatechanneleventtype)
145+
- [`PrivateChannelEventTypes`](./Events#privatechanneleventtypes)
146+
- [`PrivateChannelEvent`](./Events#privatechannelevent)
146147
- [`EventHandler`](./Events#eventhandler)
147148

148149
### `disconnect`

src/api/DesktopAgent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { AppIdentifier } from './AppIdentifier';
1515
import { AppMetadata } from './AppMetadata';
1616
import { Intent } from '../intents/Intents';
1717
import { ContextType } from '../context/ContextType';
18-
import { EventHandler, FDC3EventType } from './Events';
18+
import { EventHandler, FDC3EventTypes } from './Events';
1919

2020
/**
2121
* A Desktop Agent is a desktop component (or aggregate of components) that serves as a
@@ -395,7 +395,7 @@ export interface DesktopAgent {
395395
* @param {EventHandler} handler A function that events received will be passed to.
396396
*
397397
*/
398-
addEventListener(type: FDC3EventType | null, handler: EventHandler): Promise<Listener>;
398+
addEventListener(type: FDC3EventTypes | null, handler: EventHandler): Promise<Listener>;
399399

400400
/**
401401
* Retrieves a list of the User channels available for the app to join.

src/api/Events.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,61 @@
33
* Copyright FINOS FDC3 contributors - see NOTICE file
44
*/
55

6+
/**
7+
* Type defining a basic event object that may be emitted by an FDC3 API interface
8+
* such as DesktopAgent or PrivateChannel. There are more specific event types
9+
* defined for each interface.
10+
*/
11+
export interface ApiEvent {
12+
readonly type: string;
13+
readonly details: any;
14+
}
15+
616
/** Type representing a handler function for events from the Desktop Agent
717
* or a PrivateChannel.
8-
* @param event The handler function will be passed an `FDC3Event` or
9-
* `PrivateChannelEvent` Object providing details of the event (such as a
10-
* change of channel membership for the app, or type of context listener added)
18+
* @param event The handler function will be passed an `ApiEvent` (or more specifically
19+
* an `FDC3Event` or `PrivateChannelEvent`) Object providing details of the event (such
20+
* as a change of channel membership for the app, or type of context listener added)
1121
* as the only parameter.
1222
*/
13-
export type EventHandler = (event: FDC3Event | PrivateChannelEvent ) => void;
23+
export type EventHandler = (event: ApiEvent ) => void;
1424

1525
/**
16-
* Enumeration defining the types of (non-context and non-intent) events that may be received
17-
via the FDC3 API's `addEventListener` function.
26+
* Type defining valid type strings for DesktopAgent interface events.
1827
*/
19-
export enum FDC3EventType {
20-
USER_CHANNEL_CHANGED = "USER_CHANNEL_CHANGED"
21-
}
28+
export type FDC3EventTypes = "USER_CHANNEL_CHANGED";
29+
2230

2331
/**
2432
* Type defining the format of event objects that may be received
25-
via the FDC3 API's `addEventListener` function.
33+
* via the FDC3 API's `addEventListener` function.
2634
*/
27-
export interface FDC3Event {
28-
readonly type: FDC3EventType;
35+
export interface FDC3Event extends ApiEvent {
36+
readonly string: FDC3EventTypes;
2937
readonly details: any;
3038
}
3139

3240
/**
3341
* Type defining the format of event USER_CHANNEL_CHANGED objects
3442
*/
3543
export interface FDC3ChannelChangedEvent extends FDC3Event {
36-
readonly type: FDC3EventType.USER_CHANNEL_CHANGED;
44+
readonly type: "USER_CHANNEL_CHANGED";
3745
readonly details: {
3846
currentChannelId: string | null
3947
};
4048
}
4149

4250
/**
43-
* Enumeration defining the types of events that may be received
44-
via a PrivateChannel's `addEventListener` function.
51+
* Type defining valid type strings for Private Channel events.
4552
*/
46-
export enum PrivateChannelEventType {
47-
ADD_CONTEXT_LISTENER = "addContextListener",
48-
UNSUBSCRIBE = "unsubscribe",
49-
DISCONNECT = "disconnect"
50-
}
51-
52-
53+
export type PrivateChannelEventTypes = "addContextListener" | "unsubscribe" | "disconnect";
54+
5355
/**
5456
* Type defining the format of event objects that may be received
5557
* via a PrivateChannel's `addEventListener` function.
5658
*/
5759
export interface PrivateChannelEvent {
58-
readonly type: PrivateChannelEventType;
60+
readonly type: PrivateChannelEventTypes;
5961
readonly details: any;
6062
}
6163

@@ -69,7 +71,7 @@ export interface PrivateChannelEvent {
6971
* which will be `null` if all event types are being listened to.
7072
*/
7173
export interface PrivateChannelAddContextListenerEvent extends PrivateChannelEvent {
72-
readonly type: PrivateChannelEventType.ADD_CONTEXT_LISTENER;
74+
readonly type: "addContextListener";
7375
readonly details: {
7476
contextType: string | null
7577
};
@@ -84,7 +86,7 @@ export interface PrivateChannelAddContextListenerEvent extends PrivateChannelEve
8486
* which will be `null` if all event types were being listened to.
8587
*/
8688
export interface PrivateChannelUnsubscribeEvent extends PrivateChannelEvent {
87-
readonly type: PrivateChannelEventType.UNSUBSCRIBE;
89+
readonly type: "unsubscribe";
8890
readonly details: {
8991
contextType: string | null
9092
};
@@ -97,6 +99,6 @@ export interface PrivateChannelUnsubscribeEvent extends PrivateChannelEvent {
9799
* No details are provided.
98100
*/
99101
export interface PrivateChannelDisconnectEvent extends PrivateChannelEvent {
100-
readonly type: PrivateChannelEventType.DISCONNECT;
102+
readonly type: "disconnect";
101103
readonly details: null | undefined;
102104
}

src/api/PrivateChannel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { Listener } from './Listener';
77
import { Channel } from './Channel';
8-
import { EventHandler, PrivateChannelEventType } from './Events';
8+
import { EventHandler, PrivateChannelEventTypes } from './Events';
99

1010
/**
1111
* Object representing a private context channel, which is intended to support
@@ -43,7 +43,7 @@ export interface PrivateChannel extends Channel {
4343
* @param {EventHandler} handler A function that events received will be passed to.
4444
*
4545
*/
46-
addEventListener(type: PrivateChannelEventType | null, handler: EventHandler): Promise<Listener>;
46+
addEventListener(type: PrivateChannelEventTypes | null, handler: EventHandler): Promise<Listener>;
4747

4848
/**
4949
* May be called to indicate that a participant will no longer interact with this channel.

0 commit comments

Comments
 (0)