MCPcopy Create free account
hub / github.com/Bistutu/FluentRead / enqueueTranslation

Function enqueueTranslation

entrypoints/utils/translateQueue.ts:25–55  ·  view source on GitHub ↗
(translationTask: () => Promise<T>)

Source from the content-addressed store, hash-verified

23 * @returns 返回一个Promise,当任务执行完成时resolve
24 */
25export function enqueueTranslation<T>(translationTask: () => Promise<T>): Promise<T> {
26 return new Promise((resolve, reject) => {
27 // 创建任务包装器,在任务完成后处理队列状态
28 const taskWrapper = async () => {
29
30 try {
31 // 执行实际的翻译任务
32 const result = await translationTask();
33 resolve(result);
34 return result;
35 } catch (error) {
36 reject(error);
37 throw error;
38 } finally {
39 // 无论成功失败,都需要减少活跃任务计数并处理队列
40 activeTranslations--;
41 processQueue();
42
43 }
44 };
45
46 // 将任务添加到队列
47 if (activeTranslations < getMaxConcurrentTranslations()) {
48 // 直接执行任务
49 activeTranslations++;
50 taskWrapper();
51 } else {
52 pendingTranslations.push(taskWrapper);
53 }
54 });
55}
56
57/**
58 * 处理队列中的下一个任务

Callers 1

translateTextFunction · 0.90

Calls 2

taskWrapperFunction · 0.85

Tested by

no test coverage detected