From cca92d71afb4fd58c20a5911f5ca7ec796649562 Mon Sep 17 00:00:00 2001 From: Oleksii Date: Thu, 11 Aug 2022 23:21:47 +0300 Subject: [PATCH 1/6] client (browser) emitting events to server --- dist/events.js | 16 +++++++++++++++- dist/metacom.js | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/dist/events.js b/dist/events.js index 6552a285..1e66789b 100644 --- a/dist/events.js +++ b/dist/events.js @@ -9,6 +9,7 @@ const warnAboutMemoryLeak = (eventName, count) => export default class EventEmitter { constructor() { this.events = new Map(); + this.globalListeners = new Set(); this.maxListenersCount = 10; } @@ -33,6 +34,16 @@ 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); @@ -43,10 +54,13 @@ export default class EventEmitter { emit(name, ...args) { const event = this.events.get(name); - if (!event) return; + if (!event && !this.globalListeners.size) return; for (const fn of event.values()) { fn(...args); } + for (const fn of this.globalListeners.values()) { + fn(name, ...args); + } } remove(name, fn) { diff --git a/dist/metacom.js b/dist/metacom.js index f0e84388..d3b06c23 100644 --- a/dist/metacom.js +++ b/dist/metacom.js @@ -36,6 +36,7 @@ export class Metacom extends EventEmitter { this.calls = new Map(); this.streams = new Map(); this.streamId = 0; + this.eventId = 0; this.active = false; this.connected = false; this.opening = null; @@ -157,6 +158,11 @@ export class Metacom extends EventEmitter { for (const methodName of methodNames) { methods[methodName] = request(methodName); } + methods.onAny((eventName, data) => { + const target = `${interfaceName}/${eventName}`; + const packet = { event: ++this.eventId, [target]: data }; + this.send(JSON.stringify(packet)); + }); this.api[interfaceName] = methods; } } From 1b1aacc6f0a38cc1d856cd207adfb43f8132cfb0 Mon Sep 17 00:00:00 2001 From: Oleksii Date: Thu, 18 Aug 2022 00:42:48 +0300 Subject: [PATCH 2/6] global listeners: .on('*', listener) --- dist/events.js | 46 ++++++++++++++++++++++++++++------------------ dist/metacom.js | 2 +- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/dist/events.js b/dist/events.js index 1e66789b..7198a687 100644 --- a/dist/events.js +++ b/dist/events.js @@ -7,12 +7,18 @@ const warnAboutMemoryLeak = (eventName, count) => ); export default class EventEmitter { + globalEventName = '*'; + constructor() { this.events = new Map(); - this.globalListeners = new Set(); + this.events.set(this.globalEventName, new Set()); this.maxListenersCount = 10; } + isGlobalEvent(name) { + return name === this.globalEventName; + } + getMaxListeners() { return this.maxListenersCount; } @@ -34,16 +40,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); @@ -53,13 +49,21 @@ export default class EventEmitter { } emit(name, ...args) { + if (this.isGlobalEvent(name)) + throw new Error( + `Cannot emit '${this.globalEventName}'. It 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); + const globalListeners = this.events.get(this.globalEventName); + if (globalListeners.size) { + for (const fn of globalListeners.values()) { + fn(name, ...args); + } } } @@ -72,7 +76,13 @@ export default class EventEmitter { } clear(name) { - if (name) this.events.delete(name); - else this.events.clear(); + const globalListeners = this.events.get(this.globalEventName); + if (!name) { + this.events.clear(); + globalListeners.clear(); + return; + } + if (this.isGlobalEvent(name)) globalListeners.clear(); + else this.events.delete(name); } } diff --git a/dist/metacom.js b/dist/metacom.js index d3b06c23..155930c5 100644 --- a/dist/metacom.js +++ b/dist/metacom.js @@ -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)); From 4537dcae248b46801ea1745c33509f91589b8978 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Wed, 24 Aug 2022 21:14:50 +0300 Subject: [PATCH 3/6] Apply suggestions from code review --- dist/events.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/dist/events.js b/dist/events.js index 7198a687..05f08d61 100644 --- a/dist/events.js +++ b/dist/events.js @@ -7,18 +7,11 @@ const warnAboutMemoryLeak = (eventName, count) => ); export default class EventEmitter { - globalEventName = '*'; - constructor() { this.events = new Map(); - this.events.set(this.globalEventName, new Set()); + this.events.set('*', new Set()); this.maxListenersCount = 10; } - - isGlobalEvent(name) { - return name === this.globalEventName; - } - getMaxListeners() { return this.maxListenersCount; } @@ -49,9 +42,9 @@ export default class EventEmitter { } emit(name, ...args) { - if (this.isGlobalEvent(name)) + if (name === '*') throw new Error( - `Cannot emit '${this.globalEventName}'. It is reserved for global listeners.`, + 'Cannot emit "*"; it is reserved for global listeners.', ); const event = this.events.get(name); if (event) { @@ -59,7 +52,7 @@ export default class EventEmitter { fn(...args); } } - const globalListeners = this.events.get(this.globalEventName); + const globalListeners = this.events.get('*'); if (globalListeners.size) { for (const fn of globalListeners.values()) { fn(name, ...args); @@ -82,7 +75,7 @@ export default class EventEmitter { globalListeners.clear(); return; } - if (this.isGlobalEvent(name)) globalListeners.clear(); + if (name === '*') globalListeners.clear(); else this.events.delete(name); } } From 3f5edf4822437050eef3b5e63dec8c54751ba632 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Wed, 24 Aug 2022 21:17:25 +0300 Subject: [PATCH 4/6] Apply suggestions from code review --- dist/events.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dist/events.js b/dist/events.js index 05f08d61..20e45309 100644 --- a/dist/events.js +++ b/dist/events.js @@ -42,10 +42,9 @@ export default class EventEmitter { } emit(name, ...args) { - if (name === '*') - throw new Error( - 'Cannot emit "*"; it is reserved for global listeners.', - ); + if (name === '*') { + throw new Error('Cannot emit reserved "*" global listeners.'); + } const event = this.events.get(name); if (event) { for (const fn of event.values()) { From 775c0278a02efd1ee5076f6fec636c2a018f1e56 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Wed, 24 Aug 2022 21:19:02 +0300 Subject: [PATCH 5/6] Update dist/events.js --- dist/events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/events.js b/dist/events.js index 20e45309..5e1d949d 100644 --- a/dist/events.js +++ b/dist/events.js @@ -68,7 +68,7 @@ export default class EventEmitter { } clear(name) { - const globalListeners = this.events.get(this.globalEventName); + const globalListeners = this.events.get('*'); if (!name) { this.events.clear(); globalListeners.clear(); From 9d5e364635845c890763b1ae7a8feb96dc93de47 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Wed, 24 Aug 2022 21:25:38 +0300 Subject: [PATCH 6/6] Apply suggestions from code review --- dist/events.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/dist/events.js b/dist/events.js index 5e1d949d..4b305ad9 100644 --- a/dist/events.js +++ b/dist/events.js @@ -9,7 +9,6 @@ const warnAboutMemoryLeak = (eventName, count) => export default class EventEmitter { constructor() { this.events = new Map(); - this.events.set('*', new Set()); this.maxListenersCount = 10; } getMaxListeners() { @@ -52,10 +51,9 @@ export default class EventEmitter { } } const globalListeners = this.events.get('*'); - if (globalListeners.size) { - for (const fn of globalListeners.values()) { - fn(name, ...args); - } + if (!globalListeners) return; + for (const fn of globalListeners.values()) { + fn(name, ...args); } }