Skip to content

feat: Support for async subscriber callbacks #132

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
Aug 29, 2023
Merged
Show file tree
Hide file tree
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
21 changes: 12 additions & 9 deletions source/event_hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,18 +557,25 @@ export class EventHub {
*/
private _handle(eventPayload: EventPayload) {
this.logger.debug("Event received", eventPayload);

const promises: Promise<any>[] = [];
for (const subscriber of this._subscribers) {
// TODO: Parse event target and check that it matches subscriber.

// TODO: Support full expression format as used in Python.
if (!this._IsSubscriberInterestedIn(subscriber, eventPayload)) {
continue;
}

let response = null;
try {
response = subscriber.callback(eventPayload);
const responsePromise = Promise.resolve(
subscriber.callback(eventPayload)
);
promises.push(responsePromise);
responsePromise.then((response) => {
// Publish reply if response isn't null or undefined.
if (response != null) {
this.publishReply(eventPayload, response, subscriber.metadata);
}
});
} catch (error) {
this.logger.error(
"Error calling subscriber for event.",
Expand All @@ -577,12 +584,8 @@ export class EventHub {
eventPayload
);
}

// Publish reply if response isn't null or undefined.
if (response != null) {
this.publishReply(eventPayload, response, subscriber.metadata);
}
}
return promises;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion source/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ export class Session {
if (reason instanceof Error) {
throw this.getErrorFromResponse({
exception: "NetworkError",
content: (reason.cause as string) || reason.message,
content: (reason["cause"] as string) || reason.message,
});
}
throw new Error("Unknown error");
Expand Down
56 changes: 56 additions & 0 deletions test/event_hub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,60 @@ describe("EventHub", () => {

expect(callback).not.toHaveBeenCalledWith(testEvent);
});

test("should handle sync callback and return correct data", async () => {
const callback = vi.fn(() => "someData");
const testEvent = {
topic: "ftrack.test",
data: {},
id: "eventId",
source: { id: "sourceId" },
};

const publishReplySpy = vi
.spyOn(eventHub, "publishReply")
.mockImplementation((_, data) => data);

eventHub.subscribe("topic=ftrack.test", callback);
const promises = eventHub._handle(testEvent);
await Promise.all(promises);
expect(callback).toHaveBeenCalledWith(testEvent);
expect(publishReplySpy).toHaveBeenCalledWith(
expect.anything(),
"someData",
expect.anything()
);
publishReplySpy.mockRestore();
});

test("should not handle async callback with a promise", async () => {
const asyncCallback = vi.fn(async () => "someData");
const testEvent = {
topic: "ftrack.test",
data: {},
id: "eventId",
source: { id: "sourceId" },
};

const publishReplySpy = vi
.spyOn(eventHub, "publishReply")
.mockImplementation((_, data) => data);

eventHub.subscribe("topic=ftrack.test", asyncCallback);
const promises = eventHub._handle(testEvent);
await Promise.all(promises ?? []);
expect(asyncCallback).toHaveBeenCalledWith(testEvent);
expect(publishReplySpy).toHaveBeenCalled();
expect(publishReplySpy).not.toHaveBeenCalledWith(
expect.anything(),
expect.any(Promise),
expect.anything()
);
expect(publishReplySpy).toHaveBeenCalledWith(
expect.anything(),
"someData",
expect.anything()
);
publishReplySpy.mockRestore();
});
});