forked from XiaoMo/ChatGPT-Next-Web
38 lines
938 B
TypeScript
38 lines
938 B
TypeScript
|
// To store message streaming controller
|
||
|
export const ChatControllerPool = {
|
||
|
controllers: {} as Record<string, AbortController>,
|
||
|
|
||
|
addController(
|
||
|
sessionIndex: number,
|
||
|
messageId: number,
|
||
|
controller: AbortController,
|
||
|
) {
|
||
|
const key = this.key(sessionIndex, messageId);
|
||
|
this.controllers[key] = controller;
|
||
|
return key;
|
||
|
},
|
||
|
|
||
|
stop(sessionIndex: number, messageId: number) {
|
||
|
const key = this.key(sessionIndex, messageId);
|
||
|
const controller = this.controllers[key];
|
||
|
controller?.abort();
|
||
|
},
|
||
|
|
||
|
stopAll() {
|
||
|
Object.values(this.controllers).forEach((v) => v.abort());
|
||
|
},
|
||
|
|
||
|
hasPending() {
|
||
|
return Object.values(this.controllers).length > 0;
|
||
|
},
|
||
|
|
||
|
remove(sessionIndex: number, messageId: number) {
|
||
|
const key = this.key(sessionIndex, messageId);
|
||
|
delete this.controllers[key];
|
||
|
},
|
||
|
|
||
|
key(sessionIndex: number, messageIndex: number) {
|
||
|
return `${sessionIndex},${messageIndex}`;
|
||
|
},
|
||
|
};
|