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

Conversation

ffMathy
Copy link
Contributor

@ffMathy ffMathy commented Jul 21, 2023

Add support for async subscriber callbacks while retaining backwards compatibility.

  • I have added automatic tests where applicable
  • The PR title is suitable as a release note
  • The PR contains a description of what has been changed
  • The description contains manual test instructions

Changes

Made it possible to have an async subscriber callback.

Test

Check if subscribers are still called.

Add support for async subscriber callbacks while retaining backwards compatibility
@ffMathy ffMathy requested a review from a team as a code owner July 21, 2023 07:31
@gismya
Copy link
Contributor

gismya commented Jul 21, 2023

Looks great!

Will probably be a few weeks before this can be reviewed and merged. I won't have time to do it before going on summer vacations and the other maintainers are already on vacations.

Copy link
Contributor

@gismya gismya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Please add tests for handling both async and non async callbacks.

@ffMathy
Copy link
Contributor Author

ffMathy commented Aug 28, 2023

Okay. I looked at the existing tests, and I couldn't find enough inspiration to get started with this easily.

Do you have an idea of where I can start? The existing tests don't seem to have an example that tests the return value of the callback itself, and if that is being replied.

@gismya
Copy link
Contributor

gismya commented Aug 28, 2023

Something like this should work

  test("should handle async callback and return correct data", () => {
    const callback = vi.fn(async () => {
      return new Promise((resolve) => setTimeout(() => resolve("someData"), 1));
    });
    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);
    eventHub._handle(testEvent);

    setTimeout(() => {
      expect(callback).toHaveBeenCalledWith(testEvent);
      expect(publishReplySpy).toHaveBeenCalledWith(
        expect.anything(),
        "someData",
        expect.anything()
      );
    }, 10);
    publishReplySpy.mockRestore();
  });

  test("should handle sync callback and return correct data", () => {
    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);
    eventHub._handle(testEvent);

    setTimeout(() => {
      expect(callback).toHaveBeenCalledWith(testEvent);
      expect(publishReplySpy).toHaveBeenCalledWith(
        expect.anything(),
        "someData",
        expect.anything()
      );
    }, 10);
    publishReplySpy.mockRestore();
  });
});

Not the prettiest tests, but without changing the return values we have no way of awaiting everything, so there's a magic 10 ms timeout.

@ffMathy
Copy link
Contributor Author

ffMathy commented Aug 28, 2023

I added them now, and they pass. Thank you.

I also fixed formatting from not working on my machine (and not triggering for TS files either), since I didn't have certain CLIs installed. Did this by switching a precommit into using "NPX" instead. Let me know if that's too much, and I'll remove it again.

Copy link
Contributor

@gismya gismya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't actually test the test properly yesterday. It incorrectly passed even when a promise was sent as a response to publishReply, and didn't correctly check the value either. Adding the promises as return value for the function and awaiting (With some other changes) made the tests work as intended.

Good catch with .ts missing from lint-staged. I wonder why you need npx in the command though - could you check that you've done npm install (Or 'yarn' if that's what you're using) and that it's simply not missing the needed node modules? Otherwise I'll investigate if there might be any issues with using npx together with Yarn.

ffMathy and others added 4 commits August 29, 2023 09:13
Co-authored-by: Lars Johansson <gismya@gmail.com>
Co-authored-by: Lars Johansson <gismya@gmail.com>
Co-authored-by: Lars Johansson <gismya@gmail.com>
Co-authored-by: Lars Johansson <gismya@gmail.com>
@gismya gismya changed the title Add support for async subscriber callbacks while retaining backwards compatibility feat: Support for async subscriber callbacks Aug 29, 2023
Incorrectly copied the fix when suggesting line by line.
Copy link
Contributor

@gismya gismya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some mistakes when line by line suggesting the promise returns. I've commited the correct implementation.

You can remove the changes to package.json - we identified that the commit hooks have more issues and I'm posting a separate PR for that shortly. Thank you for pointing that out! Our IDE setups made us miss that it didn't run correctly.

@gismya gismya merged commit 2084035 into ftrackhq:main Aug 29, 2023
@ffMathy ffMathy deleted the patch-4 branch August 29, 2023 09:01
@ffMathy
Copy link
Contributor Author

ffMathy commented Aug 29, 2023

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants