| 149 | }) |
| 150 | |
| 151 | const executeAcquire = < |
| 152 | Q extends Mailbox.Mailbox<O, E | WorkerError> | Deferred.Deferred<O, E | WorkerError> |
| 153 | >(request: I, makeMailbox: Effect.Effect<Q>) => |
| 154 | Effect.withFiberRuntime<{ |
| 155 | readonly id: number |
| 156 | readonly mailbox: Q |
| 157 | }>((fiber) => { |
| 158 | const context = fiber.getFiberRef(FiberRef.currentContext) |
| 159 | const span = Context.getOption(context, Tracer.ParentSpan).pipe( |
| 160 | Option.filter((span): span is Tracer.Span => span._tag === "Span") |
| 161 | ) |
| 162 | const id = requestIdCounter++ |
| 163 | return makeMailbox.pipe( |
| 164 | Effect.tap((mailbox) => { |
| 165 | requestMap.set(id, mailbox) |
| 166 | return wrappedEncode(request).pipe( |
| 167 | Effect.tap((payload) => |
| 168 | backing.send([ |
| 169 | id, |
| 170 | 0, |
| 171 | payload, |
| 172 | span._tag === "Some" ? [span.value.traceId, span.value.spanId, span.value.sampled] : undefined |
| 173 | ], collector.unsafeRead()) |
| 174 | ), |
| 175 | Effect.catchAllCause((cause) => |
| 176 | Mailbox.isMailbox<O, E | WorkerError>(mailbox) |
| 177 | ? mailbox.failCause(cause) |
| 178 | : Deferred.failCause(mailbox, cause) |
| 179 | ) |
| 180 | ) |
| 181 | }), |
| 182 | Effect.map((mailbox) => ({ id, mailbox })) |
| 183 | ) |
| 184 | }) |
| 185 | |
| 186 | const executeRelease = ({ id }: { readonly id: number }, exit: Exit.Exit<unknown, unknown>) => { |
| 187 | const release = Effect.sync(() => requestMap.delete(id)) |