Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client (browser) emitting events to server #346

Merged
merged 6 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 22 additions & 6 deletions dist/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const warnAboutMemoryLeak = (eventName, count) =>
export default class EventEmitter {
constructor() {
this.events = new Map();
this.events.set('*', new Set());
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
this.maxListenersCount = 10;
}

getMaxListeners() {
return this.maxListenersCount;
}
Expand Down Expand Up @@ -42,10 +42,20 @@ export default class EventEmitter {
}

emit(name, ...args) {
if (name === '*') {
throw new Error('Cannot emit reserved "*" global listeners.');
}
const event = this.events.get(name);
if (!event) return;
for (const fn of event.values()) {
fn(...args);
if (event) {
for (const fn of event.values()) {
fn(...args);
}
}
const globalListeners = this.events.get('*');
if (globalListeners.size) {
for (const fn of globalListeners.values()) {
fn(name, ...args);
}
}
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -58,7 +68,13 @@ export default class EventEmitter {
}

clear(name) {
if (name) this.events.delete(name);
else this.events.clear();
const globalListeners = this.events.get(this.globalEventName);
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
if (!name) {
this.events.clear();
globalListeners.clear();
return;
}
if (name === '*') globalListeners.clear();
else this.events.delete(name);
}
}
6 changes: 6 additions & 0 deletions dist/metacom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,6 +158,11 @@ export class Metacom extends EventEmitter {
for (const methodName of methodNames) {
methods[methodName] = request(methodName);
}
methods.on('*', (eventName, data) => {
const target = `${interfaceName}/${eventName}`;
const packet = { event: ++this.eventId, [target]: data };
this.send(JSON.stringify(packet));
});
this.api[interfaceName] = methods;
}
}
Expand Down