Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Nov 10, 2023
1 parent f560bcf commit af5a460
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .changeset/stale-seals-impress.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@segment/analytics-generic-utils': minor
---

Add Emitter. Log warning if a specific event type exceeds 10
Add Emitter library. Log default warning if a listeners exceeds 10 for a specific event type (configurable)
8 changes: 4 additions & 4 deletions packages/generic-utils/src/emitter/__tests__/emitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ describe(Emitter, () => {

it('has a default max listeners of 10', () => {
const em = new Emitter()
expect(em.maxListenersPerEvent).toBe(10)
expect(em.maxListeners).toBe(10)
})

it('should warn if possible memory leak', () => {
const fn = jest.fn()
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
const em = new Emitter({ maxListenersPerEvent: 3 })
const em = new Emitter({ maxListeners: 3 })
em.on('test', fn)
em.on('test', fn)
em.on('test', fn)
Expand All @@ -96,8 +96,8 @@ describe(Emitter, () => {
it('has no warning if maxListenersPerEvent is 0', () => {
const fn = jest.fn()
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
const em = new Emitter({ maxListenersPerEvent: 0 })
expect(em.maxListenersPerEvent).toBe(0)
const em = new Emitter({ maxListeners: 0 })
expect(em.maxListeners).toBe(0)
em.on('test', fn)
expect(warnSpy).not.toHaveBeenCalled()
em.on('test', fn)
Expand Down
12 changes: 6 additions & 6 deletions packages/generic-utils/src/emitter/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface EmitterOptions {
/** How many event listeners for a particular event before emitting a warning (0 = disabled)
* @default 10
**/
maxListenersPerEvent?: number
maxListeners?: number
}

/**
Expand All @@ -23,9 +23,9 @@ export interface EmitterOptions {
* ```
*/
export class Emitter<Contract extends EmitterContract = EmitterContract> {
maxListenersPerEvent: number
maxListeners: number
constructor(options?: EmitterOptions) {
this.maxListenersPerEvent = options?.maxListenersPerEvent ?? 10
this.maxListeners = options?.maxListeners ?? 10
}
private callbacks: Partial<Contract> = {}
private warned = false
Expand All @@ -37,13 +37,13 @@ export class Emitter<Contract extends EmitterContract = EmitterContract> {
return
}
if (
this.maxListenersPerEvent &&
this.callbacks[event]!.length > this.maxListenersPerEvent
this.maxListeners &&
this.callbacks[event]!.length > this.maxListeners
) {
console.warn(
`Event Emitter: Possible memory leak detected; ${String(
event
)} has exceeded ${this.maxListenersPerEvent} callbacks. `
)} has exceeded ${this.maxListeners} callbacks.`
)
this.warned = true
}
Expand Down

0 comments on commit af5a460

Please sign in to comment.