(
type: K,
payload: z.input<GetSocketMessageSchema<TEmitCatalog, K>>,
timeoutInMs?: number
)
| 287 | } |
| 288 | |
| 289 | public async sendWithAck<K extends GetSocketMessagesWithCallback<TEmitCatalog>>( |
| 290 | type: K, |
| 291 | payload: z.input<GetSocketMessageSchema<TEmitCatalog, K>>, |
| 292 | timeoutInMs?: number |
| 293 | ): Promise<z.infer<GetSocketCallbackSchema<TEmitCatalog, K>>> { |
| 294 | const currentId = this.#messageCounter++; |
| 295 | |
| 296 | return new Promise(async (resolve, reject) => { |
| 297 | const defaultTimeoutInMs = 2000; |
| 298 | |
| 299 | // Timeout if the ACK takes too long to get back to us |
| 300 | const timeout = setTimeout(() => { |
| 301 | reject( |
| 302 | JSON.stringify({ |
| 303 | reason: "sendWithAck() timeout", |
| 304 | timeoutInMs: timeoutInMs ?? defaultTimeoutInMs, |
| 305 | type, |
| 306 | payload, |
| 307 | }) |
| 308 | ); |
| 309 | }, timeoutInMs ?? defaultTimeoutInMs); |
| 310 | |
| 311 | this.#acks.set(currentId, { resolve, reject, timeout }); |
| 312 | |
| 313 | const schema = this.opts.emitSchema[type]["message"]; |
| 314 | |
| 315 | if (!schema) { |
| 316 | clearTimeout(timeout); |
| 317 | return reject(`Unknown message type: ${type as string}`); |
| 318 | } |
| 319 | |
| 320 | const parsedPayload = schema.safeParse(payload); |
| 321 | |
| 322 | if (!parsedPayload.success) { |
| 323 | clearTimeout(timeout); |
| 324 | return reject(`Failed to parse message payload: ${JSON.stringify(parsedPayload.error)}`); |
| 325 | } |
| 326 | |
| 327 | await this.#sendPacket({ |
| 328 | type: "EVENT", |
| 329 | message: { |
| 330 | type, |
| 331 | payload, |
| 332 | version: "v1", |
| 333 | }, |
| 334 | id: currentId, |
| 335 | }); |
| 336 | }); |
| 337 | } |
| 338 | } |
nothing calls this directly
no test coverage detected