2021-05-12 06:35:00 +00:00
|
|
|
import { Call } from '@grpc/grpc-js';
|
2020-11-10 00:30:27 +00:00
|
|
|
|
|
|
|
// A call can also emit 'metadata' and 'status' events
|
2021-05-12 06:35:00 +00:00
|
|
|
let _calls: Record<string, Call> = {};
|
2020-11-10 00:30:27 +00:00
|
|
|
|
2020-12-17 11:38:21 +00:00
|
|
|
const activeCount = () => Object.keys(_calls).length;
|
|
|
|
|
2020-11-10 00:30:27 +00:00
|
|
|
const get = (requestId: string): Call | undefined => {
|
|
|
|
const call: Call = _calls[requestId];
|
|
|
|
|
|
|
|
if (!call) {
|
|
|
|
console.log(`[gRPC] client call for req=${requestId} not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return call;
|
|
|
|
};
|
|
|
|
|
|
|
|
const set = (requestId: string, call: Call): void => {
|
|
|
|
_calls[requestId] = call;
|
|
|
|
};
|
|
|
|
|
|
|
|
const _tryCloseChannel = (requestId: string) => {
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION channel not found in call
|
2020-11-10 00:30:27 +00:00
|
|
|
const channel = get(requestId)?.call?.call.channel;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-11-10 00:30:27 +00:00
|
|
|
if (channel) {
|
|
|
|
channel.close();
|
|
|
|
} else {
|
|
|
|
console.log(`[gRPC] failed to close channel for req=${requestId} because it was not found`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const clear = (requestId: string): void => {
|
|
|
|
_tryCloseChannel(requestId);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-11-10 00:30:27 +00:00
|
|
|
delete _calls[requestId];
|
|
|
|
};
|
|
|
|
|
|
|
|
const reset = (): void => {
|
|
|
|
_calls = {};
|
|
|
|
};
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
const callCache = {
|
|
|
|
activeCount,
|
|
|
|
get,
|
|
|
|
set,
|
|
|
|
clear,
|
|
|
|
reset,
|
|
|
|
};
|
2020-11-10 00:30:27 +00:00
|
|
|
export default callCache;
|