Skip to content

Commit 2e5b8da

Browse files
ffMathygismya
authored andcommitted
feat: Use type discriminators for event payload. (ftrackhq#89)
1 parent 28bba17 commit 2e5b8da

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

source/event_hub.ts

+66-25
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,65 @@ import {
1111
} from "./error";
1212
import { Data } from "./types";
1313

14-
export interface EventPayload {
14+
interface BaseActionData {
15+
selection: Array<{
16+
entityId: string;
17+
entityType: string;
18+
}>;
19+
}
20+
21+
interface BaseEventPayload {
1522
target: string;
16-
inReplyToEvent: string;
17-
topic: string;
1823
source: EventSource;
19-
data: EventData;
2024
id: string;
25+
inReplyToEvent?: string;
26+
sent?: boolean;
27+
}
28+
29+
export interface ActionDiscoverEventPayload extends BaseEventPayload {
30+
topic: "ftrack.action.discover";
31+
data: BaseActionData;
32+
}
33+
34+
export interface ActionLaunchEventData extends BaseActionData {
35+
actionIdentifier: string;
36+
description?: string;
37+
label?: string;
38+
applicationIdentifier?: string;
39+
}
40+
41+
export interface ActionLaunchEventPayload extends BaseEventPayload {
42+
topic: "ftrack.action.launch";
43+
data: ActionLaunchEventData;
2144
}
2245

46+
export interface UpdateEventData {
47+
entities?: EventEntity[];
48+
pushToken?: string;
49+
parents?: string[];
50+
user?: {
51+
userid: string;
52+
name: string;
53+
};
54+
clientToken?: string;
55+
}
56+
57+
export interface UpdateEventPayload extends BaseEventPayload {
58+
topic: "ftrack.update";
59+
data: UpdateEventData;
60+
}
61+
62+
export interface UnknownEventPayload extends BaseEventPayload {
63+
topic: unknown;
64+
data: unknown;
65+
}
66+
67+
export type EventPayload =
68+
| ActionLaunchEventPayload
69+
| ActionDiscoverEventPayload
70+
| UpdateEventPayload
71+
| UnknownEventPayload;
72+
2373
export interface EventSource {
2474
clientToken: string;
2575
applicationId: string;
@@ -30,32 +80,21 @@ export interface EventSource {
3080
id: string;
3181
}
3282

33-
export interface EventData {
34-
entities: EventEntity[];
35-
pushToken: string;
36-
parents: string[];
37-
user: {
38-
userid: string;
39-
name: string;
40-
};
41-
clientToken: string;
42-
}
43-
4483
export interface EventEntity {
45-
entity_type: string;
46-
keys: string[];
47-
objectTypeId: string;
48-
entityType: string;
49-
parents: {
84+
entity_type?: string;
85+
keys?: string[];
86+
objectTypeId?: string;
87+
entityType?: string;
88+
parents?: {
5089
entityId: string;
5190
entityType: string;
5291
entity_type: string;
5392
parentId?: string;
5493
}[];
55-
parentId: string;
56-
action: string;
57-
entityId: string;
58-
changes: Data;
94+
parentId?: string;
95+
action?: string;
96+
entityId?: string;
97+
changes?: Data;
5998
}
6099

61100
export interface SubscriberMetadata {
@@ -541,7 +580,9 @@ export class EventHub {
541580
*/
542581
_handleReply(eventPayload: EventPayload) {
543582
this.logger.debug("Reply received", eventPayload);
544-
const onReplyCallback = this._replyCallbacks[eventPayload.inReplyToEvent];
583+
const onReplyCallback = !eventPayload.inReplyToEvent
584+
? null
585+
: this._replyCallbacks[eventPayload.inReplyToEvent];
545586
if (onReplyCallback) {
546587
onReplyCallback(eventPayload);
547588
}

0 commit comments

Comments
 (0)