import { Subject } from 'rxjs'; import type { RequestData, ResponseData } from './trpc'; export const clientFunctionCallEventSubjectMap = new Map>(); /** * Clear client function call event subjects. * If socketId is not undefined or 0, clear all the client function call event subjects. * Otherwise, clear the client function call event subjects by socketId. * @param socketId The id of socket. */ export const clearClientFunctionCallEventSubjects = (socketId?: number) => { if (socketId) { clientFunctionCallEventSubjectMap.get(socketId)?.unsubscribe(); clientFunctionCallEventSubjectMap.delete(socketId); } else { // Clear all the client function call event subjects. for (const subject of clientFunctionCallEventSubjectMap.values()) { subject.unsubscribe(); } clientFunctionCallEventSubjectMap.clear(); } }; /** * Get a client function call event subject by socketId. * If there is no cached subject which key is socketId, create an new subject, cache it with socketId and return it. * @param socketId The id of socket. * @returns The cached subject which key is socketId. */ export const getClientFunctionCallEventSubject = (socketId: number) => { let subject = clientFunctionCallEventSubjectMap.get(socketId); if (subject) { return subject; } subject = new Subject(); clientFunctionCallEventSubjectMap.set(socketId, subject); return subject; }; export const clientFunctionReturnEventSubject = new Subject();