refactor: events

This commit is contained in:
cha0s 2024-07-21 02:30:13 -05:00
parent 8bf26ef7cd
commit ede9614e2d
5 changed files with 44 additions and 69 deletions

View File

@ -1,33 +1,18 @@
import {CLIENT_LATENCY} from '@/util/constants.js'; import {CLIENT_LATENCY} from '@/util/constants.js';
import EventEmitter from '@/util/event-emitter.js';
export default class Client { export default class Client {
constructor() { constructor() {
this.listeners = {}; this.emitter = new EventEmitter();
} }
accept(packet) { accept(packet) {
const listeners = this.listeners[packet.type]; this.emitter.invoke(packet.type, packet.payload);
if (!listeners) {
return;
}
for (const i in listeners) {
listeners[i](packet.payload);
}
} }
addPacketListener(type, listener) { addPacketListener(type, listener) {
if (!this.listeners[type]) { this.emitter.addListener(type, listener);
this.listeners[type] = [];
}
this.listeners[type].push(listener);
} }
removePacketListener(type, listener) { removePacketListener(type, listener) {
const listeners = this.listeners[type]; this.emitter.removeListener(type, listener);
if (!listeners) {
return;
}
const index = listeners.indexOf(listener);
if (-1 !== index) {
listeners.splice(index, 1);
}
} }
send(packet) { send(packet) {
if (CLIENT_LATENCY > 0) { if (CLIENT_LATENCY > 0) {

View File

@ -1,38 +1,23 @@
import {SERVER_LATENCY} from '@/util/constants.js'; import {SERVER_LATENCY} from '@/util/constants.js';
import EventEmitter from '@/util/event-emitter.js';
const textDecoder = new TextDecoder(); const textDecoder = new TextDecoder();
export default class Server { export default class Server {
constructor() { constructor() {
this.listeners = {}; this.emitter = new EventEmitter();
} }
accept(connection, packet) { accept(connection, packet) {
const listeners = this.listeners[packet.type]; this.emitter.invoke(packet.type, connection, packet.payload);
if (!listeners) {
return;
}
for (const i in listeners) {
listeners[i](connection, packet.payload);
}
} }
addPacketListener(type, listener) { addPacketListener(type, listener) {
if (!this.listeners[type]) { this.emitter.addListener(type, listener);
this.listeners[type] = [];
}
this.listeners[type].push(listener);
} }
async readJson(path) { async readJson(path) {
return JSON.parse(textDecoder.decode(await this.readData(path))); return JSON.parse(textDecoder.decode(await this.readData(path)));
} }
removePacketListener(type, listener) { removePacketListener(type, listener) {
const listeners = this.listeners[type]; this.emitter.removeListener(type, listener);
if (!listeners) {
return;
}
const index = listeners.indexOf(listener);
if (-1 !== index) {
listeners.splice(index, 1);
}
} }
send(connection, packet) { send(connection, packet) {
if (SERVER_LATENCY > 0) { if (SERVER_LATENCY > 0) {

View File

@ -5,6 +5,7 @@ import {useDebug} from '@/react/context/debug.js';
import {useEcs, useEcsTick} from '@/react/context/ecs.js'; import {useEcs, useEcsTick} from '@/react/context/ecs.js';
import {useMainEntity} from '@/react/context/main-entity.js'; import {useMainEntity} from '@/react/context/main-entity.js';
import {RESOLUTION} from '@/util/constants.js'; import {RESOLUTION} from '@/util/constants.js';
import EventEmitter from '@/util/event-emitter.js';
import addKeyListener from './add-key-listener.js'; import addKeyListener from './add-key-listener.js';
import ClientEcs from './client-ecs.js'; import ClientEcs from './client-ecs.js';
@ -21,31 +22,7 @@ function emptySlots() {
return Array(10).fill(undefined); return Array(10).fill(undefined);
} }
const devEventsChannel = { const devEventsChannel = new EventEmitter();
$$listeners: {},
addListener(type, listener) {
if (!this.$$listeners[type]) {
this.$$listeners[type] = new Set();
}
this.$$listeners[type].add(listener);
},
invoke(type, payload) {
const listeners = this.$$listeners[type];
if (!listeners) {
return;
}
for (const listener of listeners) {
listener(payload);
}
},
removeListener(type, listener) {
const listeners = this.$$listeners[type];
if (!listeners) {
return;
}
listeners.delete(listener);
},
};
function Ui({disconnected}) { function Ui({disconnected}) {
// Key input. // Key input.
@ -261,9 +238,7 @@ function Ui({disconnected}) {
return; return;
} }
await ecs.apply(payload.ecs); await ecs.apply(payload.ecs);
for (const listener of client.listeners[':Ecs'] ?? []) { client.emitter.invoke(':Ecs', payload.ecs);
listener(payload.ecs);
}
}, [ecs]); }, [ecs]);
useEcsTick((payload) => { useEcsTick((payload) => {
let localMainEntity = mainEntity; let localMainEntity = mainEntity;

30
app/util/event-emitter.js Normal file
View File

@ -0,0 +1,30 @@
import fastCall from './fast-call.js';
export default class EventEmitter {
$$listeners = {};
addListener(type, listener) {
if (!this.$$listeners[type]) {
this.$$listeners[type] = new Set();
}
this.$$listeners[type].add(listener);
}
invoke(type, ...args) {
const listeners = this.$$listeners[type];
if (!listeners) {
return;
}
for (const listener of listeners) {
fastCall(listener, undefined, args);
}
}
removeListener(type, listener) {
const listeners = this.$$listeners[type];
if (!listeners) {
return;
}
listeners.delete(listener);
if (0 === listeners.size) {
delete this.$$listeners[type];
}
}
}

View File

@ -1,4 +1,4 @@
export default function(fn, holder, args) { export default function fastCall(fn, holder, args) {
if (holder) { if (holder) {
const {name} = fn; const {name} = fn;
if ('object' === typeof holder && name in holder && holder[name] === fn) { if ('object' === typeof holder && name in holder && holder[name] === fn) {