Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/event",
"version": "5.7.0",
"version": "5.8.0",
"description": "The Athenna events handler with queue store. Based on Emittery syntax.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
6 changes: 5 additions & 1 deletion src/events/EventImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export class EventImpl extends Macroable {
let listener = null

if (Is.String(closure)) {
listener = new Listener(event, ctx => ioc.safeUse(closure).handle(ctx))
listener = new Listener(
event,
ctx => ioc.safeUse(closure).handle(ctx),
closure
)
} else {
listener = new Listener(event, closure)
}
Expand Down
15 changes: 13 additions & 2 deletions src/events/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ export class Listener {
public readonly id: string
public readonly event: string
public readonly closure: EventClosure
public readonly key?: string

public constructor(event: string, closure: EventClosure) {
/**
* The optional `key` uniquely identifies the listener when its closure
* source alone cannot. Listeners registered by name (`Event.on(event,
* 'MyListener')`) all share the same wrapper closure, so without the name
* as a key every named listener on the same event would hash to the same
* id and silently overwrite the previous one in the records map.
*/
public constructor(event: string, closure: EventClosure, key?: string) {
this.event = event
this.closure = closure
this.key = key
this.id = this.createId()
}

Expand All @@ -37,8 +46,10 @@ export class Listener {
}

private createId() {
const identity = this.key ?? this.closure.toString()

return createHash('sha256')
.update(`${this.event ?? '*'}|${this.closure.toString()}`)
.update(`${this.event ?? '*'}|${identity}`)
.digest('hex')
}
}
41 changes: 41 additions & 0 deletions tests/unit/events/EventImplTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,47 @@ export class EventImplTest {
assert.equal(event.listenerCount('other:event'), 1)
}

@Test()
public async shouldRegisterAndEmitToMultipleNamedListenersOnTheSameEvent({ assert }: Context) {
const event = Event.store('memoryA')

let webhookCalls = 0
let profileCalls = 0

ioc.bind(
'WebhookNamedListener',
class {
public async handle() {
webhookCalls++
}
}
)
ioc.bind(
'ProfileNamedListener',
class {
public async handle() {
profileCalls++
}
}
)

event.on('named:event', 'WebhookNamedListener')
event.on('named:event', 'ProfileNamedListener')

/**
* Named listeners share the same wrapper closure, so their ids must be
* derived from the listener name. Otherwise the second registration
* overwrites the first and only one of them ever runs.
*/
assert.equal(event.listenerCount('named:event'), 2)

await event.emit('named:event', { ok: true })
await Sleep.for(40).milliseconds().wait()

assert.equal(webhookCalls, 1)
assert.equal(profileCalls, 1)
}

@Test()
public async shouldEmitForAnyListenersAndSpecificListeners({ assert }: Context) {
const event = Event.store('memoryA')
Expand Down
Loading