Skip to content

feat: Use type discriminators for event payload. #89

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 10 commits into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 66 additions & 25 deletions source/event_hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,65 @@ import {
} from "./error";
import { Data } from "./types";

export interface EventPayload {
interface BaseActionData {
selection: Array<{
entityId: string;
entityType: string;
}>;
}

interface BaseEventPayload {
target: string;
inReplyToEvent: string;
topic: string;
source: EventSource;
data: EventData;
id: string;
inReplyToEvent?: string;
sent?: boolean;
}

export interface ActionDiscoverEventPayload extends BaseEventPayload {
topic: "ftrack.action.discover";
data: BaseActionData;
}

export interface ActionLaunchEventData extends BaseActionData {
actionIdentifier: string;
description?: string;
label?: string;
applicationIdentifier?: string;
}

export interface ActionLaunchEventPayload extends BaseEventPayload {
topic: "ftrack.action.launch";
data: ActionLaunchEventData;
}

export interface UpdateEventData {
entities?: EventEntity[];
pushToken?: string;
parents?: string[];
user?: {
userid: string;
name: string;
};
clientToken?: string;
}

export interface UpdateEventPayload extends BaseEventPayload {
topic: "ftrack.update";
data: UpdateEventData;
}

export interface UnknownEventPayload extends BaseEventPayload {
topic: unknown;
data: unknown;
}

export type EventPayload =
| ActionLaunchEventPayload
| ActionDiscoverEventPayload
| UpdateEventPayload
| UnknownEventPayload;

export interface EventSource {
clientToken: string;
applicationId: string;
Expand All @@ -30,32 +80,21 @@ export interface EventSource {
id: string;
}

export interface EventData {
entities: EventEntity[];
pushToken: string;
parents: string[];
user: {
userid: string;
name: string;
};
clientToken: string;
}

export interface EventEntity {
entity_type: string;
keys: string[];
objectTypeId: string;
entityType: string;
parents: {
entity_type?: string;
keys?: string[];
objectTypeId?: string;
entityType?: string;
parents?: {
entityId: string;
entityType: string;
entity_type: string;
parentId?: string;
}[];
parentId: string;
action: string;
entityId: string;
changes: Data;
parentId?: string;
action?: string;
entityId?: string;
changes?: Data;
}

export interface SubscriberMetadata {
Expand Down Expand Up @@ -539,7 +578,9 @@ export class EventHub {
*/
_handleReply(eventPayload: EventPayload) {
this.logger.debug("Reply received", eventPayload);
const onReplyCallback = this._replyCallbacks[eventPayload.inReplyToEvent];
const onReplyCallback = !eventPayload.inReplyToEvent
? null
: this._replyCallbacks[eventPayload.inReplyToEvent];
if (onReplyCallback) {
onReplyCallback(eventPayload);
}
Expand Down