JSON.parse の回数を削減する (#93)
This commit is contained in:
parent
65a75ecd9a
commit
59b1064fad
|
@ -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);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue