feat: promises

This commit is contained in:
cha0s 2020-12-09 22:07:00 -06:00
parent d4b8af601d
commit a1ba60d9c6
2 changed files with 8 additions and 8 deletions

View File

@ -94,22 +94,22 @@ export default class SocketClient extends decorate(Class) {
return this.socket ? this.socket.id : undefined;
}
static send(latus, socket, packetOrDehydrated) {
static send(latus, socket, packetOrDehydrated, method) {
const packet = Array.isArray(packetOrDehydrated)
? this.hydrate(latus, packetOrDehydrated)
: packetOrDehydrated;
debug('sending packet %o', packet);
const {id} = packet.constructor;
return socket.emitPromise(id, packet.data);
return socket[method](id, packet.data);
}
send(packet) {
return this.constructor.send(this.latus, this.socket, packet);
return this.constructor.send(this.latus, this.socket, packet, 'emitPromise');
}
to(room) {
return {
send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet),
send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet, 'emit'),
};
}

View File

@ -72,22 +72,22 @@ export default class ServerSocket extends decorate(Class) {
return this.socket.rooms;
}
static send(latus, socket, packetOrDehydrated) {
static send(latus, socket, packetOrDehydrated, method) {
const packet = Array.isArray(packetOrDehydrated)
? this.hydrate(latus, packetOrDehydrated)
: packetOrDehydrated;
debug('sending packet %s(%o)', packet.constructor.name, packet.data);
const {id} = packet.constructor;
return socket.emitPromise(id, packet.data);
return socket[method](id, packet.data);
}
send(packet) {
return this.constructor.send(this.latus, this.socket, packet);
return this.constructor.send(this.latus, this.socket, packet, 'emitPromise');
}
to(room) {
return {
send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet),
send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet, 'emit'),
};
}