feat: promisification

This commit is contained in:
cha0s 2020-12-09 18:54:20 -06:00
parent 22b58b37b9
commit 119835fb0d
3 changed files with 31 additions and 22 deletions

View File

@ -1,3 +1,5 @@
import {promisify} from 'util';
import D from 'debug'; import D from 'debug';
import io from 'socket.io-client'; import io from 'socket.io-client';
@ -42,6 +44,8 @@ export default class SocketClient extends decorate(Class) {
this.socket = io(this.address, { this.socket = io(this.address, {
...this.options, ...this.options,
}); });
const precalc = this.socket.binary(true);
this.socket.emitPromise = promisify(precalc.emit.bind(precalc));
this.socket.on('connect', () => { this.socket.on('connect', () => {
debug('connect'); debug('connect');
this.isConnected = true; this.isConnected = true;
@ -90,24 +94,22 @@ export default class SocketClient extends decorate(Class) {
return this.socket ? this.socket.id : undefined; return this.socket ? this.socket.id : undefined;
} }
static send(latus, socket, packetOrDehydrated, fn) { static send(latus, socket, packetOrDehydrated) {
const packet = Array.isArray(packetOrDehydrated) const packet = Array.isArray(packetOrDehydrated)
? this.hydrate(latus, packetOrDehydrated) ? this.hydrate(latus, packetOrDehydrated)
: packetOrDehydrated; : packetOrDehydrated;
debug('sending packet %o', packet); debug('sending packet %o', packet);
const {id} = packet.constructor; const {id} = packet.constructor;
socket.binary(true).emit(id, packet.data, fn); return socket.emitPromise(id, packet.data);
} }
send(packet, fn) { send(packet) {
this.constructor.send(this.latus, this.socket, packet, fn); return this.constructor.send(this.latus, this.socket, packet);
} }
to(channel) { to(room) {
return { return {
send: (packet, fn) => { send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet),
this.constructor.send(this.latus, this.socket.to(channel), packet, fn);
},
}; };
} }

View File

@ -1,3 +1,5 @@
import {promisify} from 'util';
import {Class, compose, EventEmitter} from '@latus/core'; import {Class, compose, EventEmitter} from '@latus/core';
import D from 'debug'; import D from 'debug';
@ -15,6 +17,10 @@ export default class ServerSocket extends decorate(Class) {
super(); super();
this.latus = latus; this.latus = latus;
this.socket = socket; this.socket = socket;
const precalc = this.socket.binary(true);
this.socket.emitPromise = promisify(precalc.emit.bind(precalc));
this.socket.joinPromise = promisify(this.socket.join.bind(this.socket));
this.socket.leavePromise = promisify(this.socket.leave.bind(this.socket));
const Packets = Object.entries(all(latus)); const Packets = Object.entries(all(latus));
for (let i = 0; i < Packets.length; i++) { for (let i = 0; i < Packets.length; i++) {
const Packet = Packets[i][1]; const Packet = Packets[i][1];
@ -22,7 +28,7 @@ export default class ServerSocket extends decorate(Class) {
debug('Registering packet %s(id: %s)', Packet.name, id); debug('Registering packet %s(id: %s)', Packet.name, id);
this.socket.on(id, (data, fn) => { this.socket.on(id, (data, fn) => {
const packet = new Packet(data); const packet = new Packet(data);
debug('recieved packet %o', packet); debug('recieved packet %s(%o)', Packet.name, data);
this.emit('packet', packet, fn); this.emit('packet', packet, fn);
}); });
} }
@ -50,12 +56,12 @@ export default class ServerSocket extends decorate(Class) {
return this.socket.id; return this.socket.id;
} }
leave(channel, fn) { leave(channel) {
this.socket.leave(channel, fn); return this.socket.leavePromise(channel);
} }
join(channel) { join(channel) {
this.socket.join(channel); return this.socket.joinPromise(channel);
} }
get req() { get req() {
@ -66,22 +72,23 @@ export default class ServerSocket extends decorate(Class) {
return this.socket.rooms; return this.socket.rooms;
} }
static send(latus, socket, packetOrDehydrated, fn) { static send(latus, socket, packetOrDehydrated) {
const packet = Array.isArray(packetOrDehydrated) const packet = Array.isArray(packetOrDehydrated)
? this.hydrate(latus, packetOrDehydrated) ? this.hydrate(latus, packetOrDehydrated)
: packetOrDehydrated; : packetOrDehydrated;
debug('sending packet %o', packet); debug('sending packet %s(%o)', packet.constructor.name, packet.data);
const {id} = packet.constructor; const {id} = packet.constructor;
socket.binary(true).emit(id, packet.data, fn); return socket.emitPromise(id, packet.data);
} }
send(packet, fn) { send(packet) {
this.constructor.send(this.latus, this.socket, packet, fn); return this.constructor.send(this.latus, this.socket, packet);
} }
to(room, packet, fn) { to(room) {
debug('broadcasting to %s', room); return {
this.constructor.send(this.latus, this.socket.to(room), packet, fn); send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet),
};
} }
} }

View File

@ -50,7 +50,7 @@ export default {
debug('@latus/socket/authenticate: passport.initialize()'); debug('@latus/socket/authenticate: passport.initialize()');
passport.initialize()(socket.handshake, undefined, () => { passport.initialize()(socket.handshake, undefined, () => {
debug('@latus/socket/authenticate: passport.session()'); debug('@latus/socket/authenticate: passport.session()');
passport.session()(socket.handshake, undefined, () => { passport.session()(socket.handshake, undefined, async () => {
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
socket.handshake.login = LogOps.logIn; socket.handshake.login = LogOps.logIn;
socket.handshake.logIn = LogOps.logIn; socket.handshake.logIn = LogOps.logIn;
@ -60,7 +60,7 @@ export default {
socket.handshake.isUnauthenticated = LogOps.isUnauthenticated; socket.handshake.isUnauthenticated = LogOps.isUnauthenticated;
socket.handshake.userId = socket.handshake.user ? socket.handshake.user.id : 0; socket.handshake.userId = socket.handshake.user ? socket.handshake.user.id : 0;
/* eslint-enable no-param-reassign */ /* eslint-enable no-param-reassign */
socket.join(`/u/${socket.handshake.userId}`); await socket.join(`/u/${socket.handshake.userId}`);
next(); next();
}); });
}); });