JSON.parse の回数を削減する (#93)

This commit is contained in:
riku6460 2023-07-02 12:17:55 +09:00 committed by GitHub
parent 65a75ecd9a
commit 59b1064fad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -93,6 +93,13 @@ export class StreamingApiServerService {
});
});
const globalEv = new EventEmitter();
this.redisForSub.on('message', (_: string, data: string) => {
const parsed = JSON.parse(data);
globalEv.emit('message', parsed);
});
this.#wss.on('connection', async (connection: WebSocket.WebSocket, request: http.IncomingMessage, ctx: {
stream: MainStreamConnection,
user: LocalUser | null;
@ -102,12 +109,11 @@ export class StreamingApiServerService {
const ev = new EventEmitter();
async function onRedisMessage(_: string, data: string): Promise<void> {
const parsed = JSON.parse(data);
ev.emit(parsed.channel, parsed.message);
function onRedisMessage(data: any): void {
ev.emit(data.channel, data.message);
}
this.redisForSub.on('message', onRedisMessage);
globalEv.on('message', onRedisMessage);
await stream.listen(ev, connection);
@ -127,7 +133,7 @@ export class StreamingApiServerService {
connection.once('close', () => {
ev.removeAllListeners();
stream.dispose();
this.redisForSub.off('message', onRedisMessage);
globalEv.off('message', onRedisMessage);
this.#connections.delete(connection);
if (userUpdateIntervalId) clearInterval(userUpdateIntervalId);
});