| 1 | /* Shared handle tables. One `makeState()` per `createWorker` so multiple workers don't share connections. */ |
| 2 | |
| 3 | export const makeState = () => { |
| 4 | const requests = []; // AbortController per in-flight fetch; nulled on completion or abort. |
| 5 | const sockets = []; // WebSocket per ws_open; nulled on close. |
| 6 | const sseSources = []; // EventSource per sse_open; nulled on close. |
| 7 | |
| 8 | const allocSocket = (ws) => { sockets.push(ws); return sockets.length - 1; }; |
| 9 | const socket = (h) => { |
| 10 | if (h < 0 || h >= sockets.length || sockets[h] === null) { |
| 11 | throw new Error('invalid socket handle: ' + h); |
| 12 | } |
| 13 | return sockets[h]; |
| 14 | }; |
| 15 | |
| 16 | const allocSse = (es) => { sseSources.push(es); return sseSources.length - 1; }; |
| 17 | const sse = (h) => { |
| 18 | if (h < 0 || h >= sseSources.length || sseSources[h] === null) { |
| 19 | throw new Error('invalid sse handle: ' + h); |
| 20 | } |
| 21 | return sseSources[h]; |
| 22 | }; |
| 23 | |
| 24 | return { requests, sockets, sseSources, allocSocket, socket, allocSse, sse }; |
| 25 | }; |