Skip to content
Open
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
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ export interface IEvent<T> {
subscribe(callback: EventCallback<T>): IDisposable
}

const eventsCache = new Map<string, EventEmitter>()

export class Event<T> implements IEvent<T> {

private _name: string
private _eventObject: EventEmitter = new EventEmitter()

constructor(name?: string) {
this._name = name || "default_event"
private _eventObject: EventEmitter

constructor(name: string) {
this._name = name
if (!eventsCache.has(name)) {
let emitter = new EventEmitter()
emitter.setMaxListeners(100)
eventsCache.set(name, emitter)
}
this._eventObject = eventsCache.get(name)!
}

public subscribe(callback: EventCallback<T>): IDisposable {
this._eventObject.addListener(this._name, callback)

const dispose = () => {
this._eventObject.removeListener(this._name, callback)
}
Expand Down