(
handleId: string,
options: {
timeoutMs?: number;
abortSignal?: AbortSignal;
requestingWorkspaceId: string;
backgroundOnMessageQueued?: boolean;
}
)
| 4882 | } |
| 4883 | |
| 4884 | async waitForWorkspaceTurn( |
| 4885 | handleId: string, |
| 4886 | options: { |
| 4887 | timeoutMs?: number; |
| 4888 | abortSignal?: AbortSignal; |
| 4889 | requestingWorkspaceId: string; |
| 4890 | backgroundOnMessageQueued?: boolean; |
| 4891 | } |
| 4892 | ): Promise<WorkspaceTurnWaitResult> { |
| 4893 | assert(handleId.length > 0, "waitForWorkspaceTurn: handleId must be non-empty"); |
| 4894 | assert( |
| 4895 | options.requestingWorkspaceId.length > 0, |
| 4896 | "waitForWorkspaceTurn: requestingWorkspaceId must be non-empty" |
| 4897 | ); |
| 4898 | const timeoutMs = options.timeoutMs ?? 120_000; |
| 4899 | assert(Number.isFinite(timeoutMs) && timeoutMs > 0, "waitForWorkspaceTurn: timeoutMs invalid"); |
| 4900 | |
| 4901 | this.markTaskForegroundRelevant(handleId); |
| 4902 | |
| 4903 | return await new Promise<WorkspaceTurnWaitResult>((resolve, reject) => { |
| 4904 | let settled = false; |
| 4905 | let timer: ReturnType<typeof setTimeout> | null = null; |
| 4906 | let abortListener: (() => void) | null = null; |
| 4907 | let stopBlockingRequester: (() => void) | null = this.startForegroundAwait( |
| 4908 | options.requestingWorkspaceId |
| 4909 | ); |
| 4910 | const shouldBackgroundOnQueuedMessage = options.backgroundOnMessageQueued ?? true; |
| 4911 | |
| 4912 | const cleanup = () => { |
| 4913 | if (settled) return; |
| 4914 | settled = true; |
| 4915 | if (timer) { |
| 4916 | clearTimeout(timer); |
| 4917 | timer = null; |
| 4918 | } |
| 4919 | if (abortListener) { |
| 4920 | options.abortSignal?.removeEventListener("abort", abortListener); |
| 4921 | abortListener = null; |
| 4922 | } |
| 4923 | if (waiterEntry.backgroundOnMessageQueued && waiterEntry.requestingWorkspaceId) { |
| 4924 | this.unregisterBackgroundableForegroundWaiter( |
| 4925 | waiterEntry.requestingWorkspaceId, |
| 4926 | waiterEntry |
| 4927 | ); |
| 4928 | } |
| 4929 | const waiters = this.pendingWorkspaceTurnWaitersByHandleId.get(handleId) ?? []; |
| 4930 | const nextWaiters = waiters.filter((waiter) => waiter !== waiterEntry); |
| 4931 | if (nextWaiters.length === 0) { |
| 4932 | this.pendingWorkspaceTurnWaitersByHandleId.delete(handleId); |
| 4933 | } else { |
| 4934 | this.pendingWorkspaceTurnWaitersByHandleId.set(handleId, nextWaiters); |
| 4935 | } |
| 4936 | if (stopBlockingRequester) { |
| 4937 | try { |
| 4938 | stopBlockingRequester(); |
| 4939 | } finally { |
| 4940 | stopBlockingRequester = null; |
| 4941 | } |
no test coverage detected