(
options: {
readonly size: number
readonly concurrency?: number | undefined
readonly targetUtilization?: number | undefined
} | {
readonly minSize: number
readonly maxSize: number
readonly concurrency?: number | undefined
readonly targetUtilization?: number | undefined
readonly timeToLive: Duration.Input
}
)
| 1219 | * @since 4.0.0 |
| 1220 | */ |
| 1221 | export const makeProtocolWorker = ( |
| 1222 | options: { |
| 1223 | readonly size: number |
| 1224 | readonly concurrency?: number | undefined |
| 1225 | readonly targetUtilization?: number | undefined |
| 1226 | } | { |
| 1227 | readonly minSize: number |
| 1228 | readonly maxSize: number |
| 1229 | readonly concurrency?: number | undefined |
| 1230 | readonly targetUtilization?: number | undefined |
| 1231 | readonly timeToLive: Duration.Input |
| 1232 | } |
| 1233 | ): Effect.Effect< |
| 1234 | Protocol["Service"], |
| 1235 | WorkerError, |
| 1236 | Scope.Scope | Worker.WorkerPlatform | Worker.Spawner |
| 1237 | > => |
| 1238 | Protocol.make(Effect.fnUntraced(function*(writeResponse, clientIds) { |
| 1239 | const worker = yield* Worker.WorkerPlatform |
| 1240 | const scope = yield* Effect.scope |
| 1241 | let workerId = 0 |
| 1242 | const initialMessage = yield* Effect.serviceOption(RpcWorker.InitialMessage) |
| 1243 | const hooks = yield* Effect.serviceOption(ConnectionHooks) |
| 1244 | |
| 1245 | const entries = new Map<string | number, { |
| 1246 | readonly clientId: number |
| 1247 | readonly worker: Worker.Worker<FromServerEncoded, FromClientEncoded | RpcWorker.InitialMessage.Encoded> |
| 1248 | readonly latch: Latch.Latch |
| 1249 | }>() |
| 1250 | |
| 1251 | const broadcast = (response: FromServerEncoded) => |
| 1252 | Effect.forEach(clientIds, (clientId) => writeResponse(clientId, response)) |
| 1253 | |
| 1254 | const acquire = Effect.gen(function*() { |
| 1255 | const id = workerId++ |
| 1256 | const backing = yield* worker.spawn<FromServerEncoded, FromClientEncoded | RpcWorker.InitialMessage.Encoded>(id) |
| 1257 | |
| 1258 | yield* backing.run((response) => { |
| 1259 | if (response._tag === "Exit") { |
| 1260 | const entry = entries.get(response.requestId) |
| 1261 | if (entry) { |
| 1262 | entries.delete(response.requestId) |
| 1263 | entry.latch.openUnsafe() |
| 1264 | return writeResponse(entry.clientId, response) |
| 1265 | } |
| 1266 | } else if (response._tag === "Defect") { |
| 1267 | for (const [requestId, entry] of entries) { |
| 1268 | entries.delete(requestId) |
| 1269 | entry.latch.openUnsafe() |
| 1270 | } |
| 1271 | return broadcast(response) |
| 1272 | } else if ("requestId" in response) { |
| 1273 | const entry = entries.get(response.requestId) |
| 1274 | if (entry) { |
| 1275 | return writeResponse(entry.clientId, response) |
| 1276 | } |
| 1277 | } |
| 1278 | return broadcast(response) |
nothing calls this directly
no test coverage detected