refactor: promisification

This commit is contained in:
cha0s 2020-07-16 23:02:30 -05:00
parent a2a485fbee
commit 3a86b1dd0e
2 changed files with 21 additions and 38 deletions

View File

@ -1,37 +1,27 @@
/* eslint-disable import/no-extraneous-dependencies */
import {promisify} from 'util';
import {joinChannel} from '~/common/channel';
import createRedisClient, {keys} from './redis';
const redisClient = createRedisClient();
const mget = promisify(redisClient.mget.bind(redisClient));
const channelState = async (req, channel) => {
const messageKeys = await keys(redisClient, `${channel}:messages:*`);
const messages = 0 === messageKeys.length
? []
: await new Promise((resolve, reject) => {
redisClient.mget(messageKeys, (error, replies) => (
error
? reject(error)
: resolve(replies
.map((reply, i) => ({
...JSON.parse(reply),
uuid: messageKeys[i].split(':')[2],
}))
.sort((l, r) => l.timestamp - r.timestamp))
));
});
: (await mget(messageKeys))
.map((reply, i) => ({
...JSON.parse(reply),
uuid: messageKeys[i].split(':')[2],
}))
.sort((l, r) => l.timestamp - r.timestamp);
const socketKeys = await keys(redisClient, `${channel}:users:*`);
const users = 0 === socketKeys.length
? []
: await new Promise((resolve, reject) => {
redisClient.mget(socketKeys, (error, replies) => (
error ? reject(error) : resolve(
Object
.keys(replies.reduce((r, k) => ({[k]: true, ...r}), {}))
.map((idStrings) => parseInt(idStrings, 10)),
)
));
});
: Object.keys((await mget(socketKeys)).reduce((r, k) => ({[k]: true, ...r}), {}))
.map((idStrings) => parseInt(idStrings, 10));
return {
messages,
users,

View File

@ -1,4 +1,6 @@
/* eslint-disable import/no-extraneous-dependencies */
import {promisify} from 'util';
import redisAdapter from 'socket.io-redis';
import {v4 as uuidv4} from 'uuid';
@ -17,6 +19,7 @@ import session from './session';
const pubClient = createRedisClient();
const subClient = createRedisClient();
const adapter = redisAdapter({pubClient, subClient});
const set = promisify(pubClient.set.bind(pubClient));
export function createSocketServer(httpServer) {
const socketServer = new SocketServer(httpServer, {
@ -35,25 +38,15 @@ export function createSocketServer(httpServer) {
next();
});
socketServer.io.use(async (socket, next) => {
const join = promisify(socket.join.bind(socket));
await Promise.all(
channelsToHydrate(socket.handshake)
.map((channel) => new Promise((resolve, reject) => {
socket.join(
joinChannel(channel),
(error) => (error ? reject(error) : resolve()),
);
})),
);
next();
});
socketServer.io.use(async (socket, next) => {
await Promise.all(
channelsToHydrate(socket.handshake)
.map((channel) => new Promise((resolve, reject) => {
const key = `${joinChannel(channel)}:users:${socket.id}`;
.map(async (channel) => {
const channelString = joinChannel(channel);
await join(channelString);
const {user} = socket.handshake;
pubClient.set(key, user ? user.id : 0, (error) => (error ? reject(error) : resolve()));
})),
await set(`${channelString}:users:${socket.id}`, user ? user.id : 0);
}),
);
next();
});