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 2 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
34 changes: 29 additions & 5 deletions dist/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ const warnAboutMemoryLeak = (eventName, count) =>
);

export default class EventEmitter {
globalEventName = '*';

tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
constructor() {
this.events = new Map();
this.events.set(this.globalEventName, new Set());
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
this.maxListenersCount = 10;
}

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

tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
getMaxListeners() {
return this.maxListenersCount;
}
Expand Down Expand Up @@ -42,10 +49,21 @@ export default class EventEmitter {
}

emit(name, ...args) {
if (this.isGlobalEvent(name))
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
`Cannot emit '${this.globalEventName}'. It is reserved for global listeners.`,
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
);
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(this.globalEventName);
if (globalListeners.size) {
for (const fn of globalListeners.values()) {
fn(name, ...args);
}
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
}
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -58,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);
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
if (!name) {
this.events.clear();
globalListeners.clear();
return;
}
if (this.isGlobalEvent(name)) globalListeners.clear();
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
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