From 119835fb0d8c63a686ed706c05c3f7e21f19fe9b Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 9 Dec 2020 18:54:20 -0600 Subject: [PATCH] feat: promisification --- packages/socket/src/client/socket.js | 18 +++++++++------- packages/socket/src/socket.js | 31 +++++++++++++++++----------- packages/user/src/passport/index.js | 4 ++-- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/packages/socket/src/client/socket.js b/packages/socket/src/client/socket.js index 9275f3c..1aca4aa 100644 --- a/packages/socket/src/client/socket.js +++ b/packages/socket/src/client/socket.js @@ -1,3 +1,5 @@ +import {promisify} from 'util'; + import D from 'debug'; import io from 'socket.io-client'; @@ -42,6 +44,8 @@ export default class SocketClient extends decorate(Class) { this.socket = io(this.address, { ...this.options, }); + const precalc = this.socket.binary(true); + this.socket.emitPromise = promisify(precalc.emit.bind(precalc)); this.socket.on('connect', () => { debug('connect'); this.isConnected = true; @@ -90,24 +94,22 @@ export default class SocketClient extends decorate(Class) { return this.socket ? this.socket.id : undefined; } - static send(latus, socket, packetOrDehydrated, fn) { + static send(latus, socket, packetOrDehydrated) { const packet = Array.isArray(packetOrDehydrated) ? this.hydrate(latus, packetOrDehydrated) : packetOrDehydrated; debug('sending packet %o', packet); const {id} = packet.constructor; - socket.binary(true).emit(id, packet.data, fn); + return socket.emitPromise(id, packet.data); } - send(packet, fn) { - this.constructor.send(this.latus, this.socket, packet, fn); + send(packet) { + return this.constructor.send(this.latus, this.socket, packet); } - to(channel) { + to(room) { return { - send: (packet, fn) => { - this.constructor.send(this.latus, this.socket.to(channel), packet, fn); - }, + send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet), }; } diff --git a/packages/socket/src/socket.js b/packages/socket/src/socket.js index 28d9c80..4795529 100644 --- a/packages/socket/src/socket.js +++ b/packages/socket/src/socket.js @@ -1,3 +1,5 @@ +import {promisify} from 'util'; + import {Class, compose, EventEmitter} from '@latus/core'; import D from 'debug'; @@ -15,6 +17,10 @@ export default class ServerSocket extends decorate(Class) { super(); this.latus = latus; 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)); for (let i = 0; i < Packets.length; i++) { 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); this.socket.on(id, (data, fn) => { const packet = new Packet(data); - debug('recieved packet %o', packet); + debug('recieved packet %s(%o)', Packet.name, data); this.emit('packet', packet, fn); }); } @@ -50,12 +56,12 @@ export default class ServerSocket extends decorate(Class) { return this.socket.id; } - leave(channel, fn) { - this.socket.leave(channel, fn); + leave(channel) { + return this.socket.leavePromise(channel); } join(channel) { - this.socket.join(channel); + return this.socket.joinPromise(channel); } get req() { @@ -66,22 +72,23 @@ export default class ServerSocket extends decorate(Class) { return this.socket.rooms; } - static send(latus, socket, packetOrDehydrated, fn) { + static send(latus, socket, packetOrDehydrated) { const packet = Array.isArray(packetOrDehydrated) ? this.hydrate(latus, packetOrDehydrated) : packetOrDehydrated; - debug('sending packet %o', packet); + debug('sending packet %s(%o)', packet.constructor.name, packet.data); const {id} = packet.constructor; - socket.binary(true).emit(id, packet.data, fn); + return socket.emitPromise(id, packet.data); } - send(packet, fn) { - this.constructor.send(this.latus, this.socket, packet, fn); + send(packet) { + return this.constructor.send(this.latus, this.socket, packet); } - to(room, packet, fn) { - debug('broadcasting to %s', room); - this.constructor.send(this.latus, this.socket.to(room), packet, fn); + to(room) { + return { + send: (packet) => this.constructor.send(this.latus, this.socket.to(room), packet), + }; } } diff --git a/packages/user/src/passport/index.js b/packages/user/src/passport/index.js index 5f410f8..af13dba 100644 --- a/packages/user/src/passport/index.js +++ b/packages/user/src/passport/index.js @@ -50,7 +50,7 @@ export default { debug('@latus/socket/authenticate: passport.initialize()'); passport.initialize()(socket.handshake, undefined, () => { debug('@latus/socket/authenticate: passport.session()'); - passport.session()(socket.handshake, undefined, () => { + passport.session()(socket.handshake, undefined, async () => { /* eslint-disable no-param-reassign */ socket.handshake.login = LogOps.logIn; socket.handshake.logIn = LogOps.logIn; @@ -60,7 +60,7 @@ export default { socket.handshake.isUnauthenticated = LogOps.isUnauthenticated; socket.handshake.userId = socket.handshake.user ? socket.handshake.user.id : 0; /* eslint-enable no-param-reassign */ - socket.join(`/u/${socket.handshake.userId}`); + await socket.join(`/u/${socket.handshake.userId}`); next(); }); });