Skip to content

Commit

Permalink
global listeners: .on('*', listener)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaitsev-oleksii committed Aug 18, 2022
1 parent cca92d7 commit 6ea2c2e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
54 changes: 34 additions & 20 deletions dist/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,34 @@ const warnAboutMemoryLeak = (eventName, count) =>
);

export default class EventEmitter {
globalEventName = '*';

constructor() {
this.events = new Map();
this.globalListeners = new Set();
this.maxListenersCount = 10;
}

isGlobalEvent(name) {
return name === this.globalEventName;
}

getMaxListeners() {
return this.maxListenersCount;
}

listenerCount(name) {
const event = this.events.get(name);
const event = this.isGlobalEvent(name)
? this.globalListeners
: this.events.get(name);
if (event) return event.size;
return 0;
}

on(name, fn) {
const event = this.events.get(name);
const event = this.isGlobalEvent(name)
? this.globalListeners
: this.events.get(name);
if (event) {
event.add(fn);
const tooManyListeners = event.size > this.maxListenersCount;
Expand All @@ -34,16 +44,6 @@ export default class EventEmitter {
}
}

onAny(fn) {
const tooManyListeners = this.globalListeners.size > this.maxListenersCount;
if (tooManyListeners) warnAboutMemoryLeak('*', this.globalListeners.size);
this.globalListeners.add(fn);
}

clearGlobalListeners() {
this.globalListeners.clear();
}

once(name, fn) {
const dispose = (...args) => {
this.remove(name, dispose);
Expand All @@ -53,26 +53,40 @@ export default class EventEmitter {
}

emit(name, ...args) {
if (this.isGlobalEvent(name))
throw new Error(
"Cannot emit '*'. Symbol is reserved for global listeners.",
);
const event = this.events.get(name);
if (!event && !this.globalListeners.size) return;
for (const fn of event.values()) {
fn(...args);
if (event) {
for (const fn of event.values()) {
fn(...args);
}
}
for (const fn of this.globalListeners.values()) {
fn(name, ...args);
if (this.globalListeners.size) {
for (const fn of this.globalListeners.values()) {
fn(name, ...args);
}
}
}

remove(name, fn) {
const event = this.events.get(name);
const event = this.isGlobalEvent(name)
? this.globalListeners
: this.events.get(name);
if (!event) return;
if (event.has(fn)) {
event.delete(fn);
}
}

clear(name) {
if (name) this.events.delete(name);
else this.events.clear();
if (!name) {
this.events.clear();
this.globalListeners.clear();
return;
}
if (this.isGlobalEvent(name)) this.globalListeners.clear();
else this.events.delete(name);
}
}
2 changes: 1 addition & 1 deletion dist/metacom.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class Metacom extends EventEmitter {
for (const methodName of methodNames) {
methods[methodName] = request(methodName);
}
methods.onAny((eventName, data) => {
methods.on('*', (eventName, data) => {
const target = `${interfaceName}/${eventName}`;
const packet = { event: ++this.eventId, [target]: data };
this.send(JSON.stringify(packet));
Expand Down

0 comments on commit 6ea2c2e

Please sign in to comment.