diff --git a/src/renderer/src/utils/abortController.ts b/src/renderer/src/utils/abortController.ts index 6593b1c162..614b7b068f 100644 --- a/src/renderer/src/utils/abortController.ts +++ b/src/renderer/src/utils/abortController.ts @@ -49,3 +49,25 @@ export function createAbortPromise(signal: AbortSignal, finallyPromise: Promi }) }) } + +/** + * 创建一个新的 AbortController 并将其注册到全局的 abort 映射中 + * @param key - 用于标识此 AbortController 的唯一键值 + * @returns AbortSignal - 返回 AbortController 的信号 + * @example + * ```typescript + * const signal = readyToAbort('uniqueKey'); + * fetch('https://api.example.com/data', { signal }) + * .then(response => response.json()) + * .catch(error => { + * if (error.name === 'AbortError') { + * console.log('Fetch aborted'); + * } + * }); + * ``` + */ +export function readyToAbort(key: string) { + const controller = new AbortController() + addAbortController(key, controller.abort) + return controller.signal +}