Skip to content

Commit

Permalink
fix event-emitter potential recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
franzzua committed Dec 17, 2023
1 parent edda051 commit e39c01b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions core/event-emitter/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ export class EventEmitter<TEvents extends {

public on<TEventName extends keyof TEvents>(eventName: TEventName, listener: (data: TEvents[TEventName]) => void,
options: EventListenerOptions = DefaultListenerOptions) {
const arr = getOrAdd(this.listeners, eventName, () => {
if (!this.listeners.has(eventName)){
this.listeners.set(eventName, []);
this.subscribe(eventName);
return [];
});
}
const arr = this.listeners.get(eventName);
arr.push({listener, options: options});
arr.sort((a, b) => b.options.Priority - a.options.Priority);
return () => this.off(eventName, listener);
}

public off<TEventName extends keyof TEvents>(eventName: TEventName, listener: (data: TEvents[TEventName]) => void) {
const set = getOrAdd(this.listeners, eventName, () => []);
const set = this.listeners.get(eventName) ?? [];
removeAll(set, x => x.listener === listener);
if (set.length == 0) {
this.listeners.delete(eventName);
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cmmn/core",
"version": "2.1.0",
"version": "2.1.1",
"repository": {
"url": "https://github.com/franzzua/cmmn/tree/master/core"
},
Expand Down

0 comments on commit e39c01b

Please sign in to comment.