feat: promisification
This commit is contained in:
parent
22b58b37b9
commit
119835fb0d
|
@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user