|
| 1 | +import { |
| 2 | + enumerateConversationKeyForms, |
| 3 | + type IMessageApprovalConversationKey, |
| 4 | +} from "./approval-target-keys.js"; |
| 5 | + |
| 6 | +type BindingWindow = { |
| 7 | + done: Promise<void>; |
| 8 | + close: () => void; |
| 9 | +}; |
| 10 | + |
| 11 | +const pendingByConversation = new Map<string, Set<BindingWindow>>(); |
| 12 | + |
| 13 | +function bindingKeys(accountId: string, conversation: IMessageApprovalConversationKey): string[] { |
| 14 | + const account = accountId.trim(); |
| 15 | + return account |
| 16 | + ? enumerateConversationKeyForms(conversation).map((form) => `${account}:${form}`) |
| 17 | + : []; |
| 18 | +} |
| 19 | + |
| 20 | +/** Marks the send-to-binding interval during which a visible control is not yet resolvable. */ |
| 21 | +export function beginIMessageApprovalControlBinding(params: { |
| 22 | + accountId: string; |
| 23 | + conversation: IMessageApprovalConversationKey; |
| 24 | +}): { close: () => void } { |
| 25 | + const keys = bindingKeys(params.accountId, params.conversation); |
| 26 | + let resolveDone = () => {}; |
| 27 | + const window: BindingWindow = { |
| 28 | + done: new Promise<void>((resolve) => { |
| 29 | + resolveDone = resolve; |
| 30 | + }), |
| 31 | + close: () => {}, |
| 32 | + }; |
| 33 | + let closed = false; |
| 34 | + window.close = () => { |
| 35 | + if (closed) { |
| 36 | + return; |
| 37 | + } |
| 38 | + closed = true; |
| 39 | + for (const key of keys) { |
| 40 | + const windows = pendingByConversation.get(key); |
| 41 | + windows?.delete(window); |
| 42 | + if (windows?.size === 0) { |
| 43 | + pendingByConversation.delete(key); |
| 44 | + } |
| 45 | + } |
| 46 | + resolveDone(); |
| 47 | + }; |
| 48 | + for (const key of keys) { |
| 49 | + const windows = pendingByConversation.get(key) ?? new Set<BindingWindow>(); |
| 50 | + windows.add(window); |
| 51 | + pendingByConversation.set(key, windows); |
| 52 | + } |
| 53 | + return { close: window.close }; |
| 54 | +} |
| 55 | + |
| 56 | +/** Waits for one matching delivery to finish binding; callers recheck until none remain. */ |
| 57 | +export async function waitForIMessageApprovalControlBinding(params: { |
| 58 | + accountId: string; |
| 59 | + conversation: IMessageApprovalConversationKey; |
| 60 | + abortSignal?: AbortSignal; |
| 61 | +}): Promise<boolean> { |
| 62 | + const windows = new Set( |
| 63 | + bindingKeys(params.accountId, params.conversation).flatMap((key) => [ |
| 64 | + ...(pendingByConversation.get(key) ?? []), |
| 65 | + ]), |
| 66 | + ); |
| 67 | + if (windows.size === 0) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + if (params.abortSignal?.aborted) { |
| 71 | + throw params.abortSignal.reason; |
| 72 | + } |
| 73 | + let detachAbort = () => {}; |
| 74 | + const aborted = new Promise<never>((_resolve, reject) => { |
| 75 | + const onAbort = () => reject(params.abortSignal?.reason); |
| 76 | + params.abortSignal?.addEventListener("abort", onAbort, { once: true }); |
| 77 | + detachAbort = () => params.abortSignal?.removeEventListener("abort", onAbort); |
| 78 | + }); |
| 79 | + try { |
| 80 | + await Promise.race([Promise.race([...windows].map((window) => window.done)), aborted]); |
| 81 | + } finally { |
| 82 | + detachAbort(); |
| 83 | + } |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +export function clearIMessageApprovalControlBindingsForTest(): void { |
| 88 | + for (const windows of pendingByConversation.values()) { |
| 89 | + for (const window of windows) { |
| 90 | + window.close(); |
| 91 | + } |
| 92 | + } |
| 93 | + pendingByConversation.clear(); |
| 94 | +} |
0 commit comments