MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / stackAsyncTask

Function stackAsyncTask

src/pkg/utils/async_queue.ts:54–76  ·  view source on GitHub ↗
(key: string, task: TStackFn<T>)

Source from the content-addressed store, hash-verified

52 * 若该队列没有正在执行的任务(!stack.tail),则启动执行
53 */
54export const stackAsyncTask = <T>(key: string, task: TStackFn<T>): Promise<T> => {
55 return new Promise((resolve, reject) => {
56 // 获取或初始化对应 key 的队列
57 const stack: TStack<T> = stacks[key] || (stacks[key] = { head: null, tail: null });
58 const newNode: TNode<T> = { task, resolve, reject, next: null };
59
60 // 入队逻辑
61 if (!stack.tail) {
62 // 队列为空时,设为首尾节点
63 stack.head = newNode;
64 stack.tail = newNode;
65
66 // ⚠️ 注意:此时会启动任务执行
67 // 请勿在外部代码中同时多次调用 startAsync
68 // 当前逻辑依赖 “!stack.tail” 判断来避免并发执行
69 startAsync<T>(stack);
70 } else {
71 // 队列不为空时,追加到尾部
72 stack.tail.next = newNode;
73 stack.tail = newNode;
74 }
75 });
76};

Callers 15

setupBlockingTaskFunction · 0.90
runFunction · 0.90
BgGMXhrClass · 0.90
callbackMethod · 0.90
txMethod · 0.90
initMethod · 0.90
onDeterminingFilenameFunction · 0.90
onChangedListenerFunction · 0.90
startDownloadFunction · 0.90
syncOnceMethod · 0.90

Calls 1

startAsyncFunction · 0.85

Tested by 2

setupBlockingTaskFunction · 0.72
runFunction · 0.72