Skip to content

Commit

Permalink
reshape eventsRoutingData, add emitterId:eventName emitting
Browse files Browse the repository at this point in the history
Adds feature where all events with EmitterId are also emitted
in another event named emitterId:eventName. These are not
shown in the event routing data, as they are already implicit
in the data. If something listens for them they are shown.

Rework the response format for eventsRoutingData, so that
the data is under events property in array format.
  • Loading branch information
tkurki committed Dec 8, 2023
1 parent 2060a2e commit b098ee9
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ export type EventsActorId = EmitterId & ListenerId

export interface WrappedEmitter {
getEmittedCount: () => number
getEventRoutingData: () => {
getEventRoutingData: () => {events: {
event: string
emitters: any
listeners: any
}[]
}

emit: (this: any, eventName: string, ...args: any[]) => boolean
Expand All @@ -120,14 +122,14 @@ export function wrappedEmitter(targetEmitter: EventEmitter): WrappedEmitter {
const targetAddListener = targetEmitter.addListener.bind(targetEmitter)

const eventDebugs: { [key: string]: Debugger } = {}
const emitterStats: {
const eventsData: {
[eventName: EventName]: {
[emitterId: EmitterId]: number
}
} = {}
const listeners: {
[eventName: EventName]: {
[listenerId: ListenerId]: boolean
emitters: {
[emitterId: EmitterId]: number
}
listeners: {
[listenerId: ListenerId]: boolean
}
}
} = {}

Expand Down Expand Up @@ -200,12 +202,15 @@ export function wrappedEmitter(targetEmitter: EventEmitter): WrappedEmitter {
eventName: EventName,
...args: any[]
) {
const emittersForEvent =
emitterStats[eventName] ?? (emitterStats[eventName] = {})
const emittersForEvent = (
eventsData[eventName] ??
(eventsData[eventName] = { emitters: {}, listeners: {} })
).emitters
if (!emittersForEvent[emitterId]) {
emittersForEvent[emitterId] = 0
}
emittersForEvent[emitterId]++
safeEmit(`${emitterId}:${eventName}`, ...args)
return safeEmit(eventName, ...args)
}

Expand All @@ -214,8 +219,10 @@ export function wrappedEmitter(targetEmitter: EventEmitter): WrappedEmitter {
eventName: EventName,
listener: (...args: any[]) => void
) {
const listenersForEvent =
listeners[eventName] ?? (listeners[eventName] = {})
const listenersForEvent = (
eventsData[eventName] ??
(eventsData[eventName] = { emitters: {}, listeners: {} })
).listeners
if (!listenersForEvent[listenerId]) {
listenersForEvent[listenerId] = true
}
Expand All @@ -225,8 +232,7 @@ export function wrappedEmitter(targetEmitter: EventEmitter): WrappedEmitter {
return {
getEmittedCount: () => emittedCount,
getEventRoutingData: () => ({
emitters: JSON.parse(JSON.stringify(emitterStats)),
listeners: JSON.parse(JSON.stringify(listeners))
events: Object.entries(eventsData).map(([event, data]) => ({event, ...data}))
}),

emit: function (this: any, eventName: string, ...args: any[]): boolean {
Expand Down

0 comments on commit b098ee9

Please sign in to comment.