Skip to content

Commit

Permalink
fix: follow kafkajs convention for event listener removal
Browse files Browse the repository at this point in the history
  • Loading branch information
brianphillips committed Jul 13, 2022
1 parent a088bac commit 6037dd3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/index.test.ts
Expand Up @@ -301,6 +301,21 @@ describe("eachMessage", () => {
});
expect(mockEventHandler).toBeCalledTimes(1);
});
it('allows event listeners to be removed easily', async () => {
expect.assertions(3);
const mock = jest.fn<AsyncCallback>().mockRejectedValue(new Error("test"));
const helper = subject();
const mockEventHandler = jest.fn();
const remove = helper.on("retry", mockEventHandler);
const wrapped = helper.eachMessage(mock);
const payload = messagePayload();
await wrapped(payload);
expect(mock).toBeCalledTimes(1);
expect(mockEventHandler).toBeCalledTimes(1);
remove();
await wrapped(payload);
expect(mockEventHandler).toBeCalledTimes(1);
});
it("handles weird error scenarios appropriately", async () => {
expect.assertions(3);
const mock = jest
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -274,9 +274,9 @@ export default class AsyncRetryHelper {
public on(
event: AsyncRetryEvent | `${AsyncRetryEvent}`,
listener: (payload: EventPayload) => void
): this {
): () => void {
this.eventEmitter.on(event, listener);
return this;
return () => { this.eventEmitter.removeListener(event, listener) };
}

/**
Expand Down

0 comments on commit 6037dd3

Please sign in to comment.