Skip to content

Commit

Permalink
fix: once event is not only firing once (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasgriffintn authored May 6, 2023
1 parent 1dec5b9 commit 528b4fd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class TypedEventEmitter extends EventEmitter {
event: E,
listener: (...args: Events[E]) => void
): this {
return super.on(event, listener);
return super.once(event, listener);
}
/**
* Emits an event with the provided arguments
Expand Down
38 changes: 38 additions & 0 deletions test/tests/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,44 @@ describe('Consumer', () => {
});
});

describe('event listeners', () => {
it('fires the event multiple times', async () => {
sqs.send.withArgs(mockReceiveMessage).resolves({});

const handleEmpty = sandbox.stub().returns(null);

consumer.on('empty', handleEmpty);

consumer.start();

await clock.tickAsync(0);

consumer.stop();

await clock.runAllAsync();

sandbox.assert.calledTwice(handleEmpty);
});

it('fires the events only once', async () => {
sqs.send.withArgs(mockReceiveMessage).resolves({});

const handleEmpty = sandbox.stub().returns(null);

consumer.once('empty', handleEmpty);

consumer.start();

await clock.tickAsync(0);

consumer.stop();

await clock.runAllAsync();

sandbox.assert.calledOnce(handleEmpty);
});
});

describe('.stop', () => {
it('stops the consumer polling for messages', async () => {
const handleStop = sandbox.stub().returns(null);
Expand Down

0 comments on commit 528b4fd

Please sign in to comment.