| 40 | }); |
| 41 | |
| 42 | const waitFor = (executionId: string): Effect.Effect<BrowserApprovalDecision | null> => |
| 43 | Effect.gen(function* () { |
| 44 | const existing = yield* take(executionId); |
| 45 | if (existing) return existing; |
| 46 | |
| 47 | const waiter = waiters.get(executionId) ?? (yield* Deferred.make<BrowserApprovalDecision>()); |
| 48 | waiters.set(executionId, waiter); |
| 49 | const decision = yield* Deferred.await(waiter).pipe( |
| 50 | Effect.ensuring( |
| 51 | Effect.sync(() => { |
| 52 | if (waiters.get(executionId) === waiter) waiters.delete(executionId); |
| 53 | }), |
| 54 | ), |
| 55 | ); |
| 56 | yield* take(executionId); |
| 57 | return decision; |
| 58 | }); |
| 59 | |
| 60 | return { |
| 61 | store: { takeResponse: take, waitForResponse: waitFor }, |