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