* @private
(id: number, ack: (...args: any[]) => void)
| 462 | * @private |
| 463 | */ |
| 464 | private _registerAckCallback(id: number, ack: (...args: any[]) => void) { |
| 465 | const timeout = this.flags.timeout ?? this._opts.ackTimeout; |
| 466 | if (timeout === undefined) { |
| 467 | this.acks[id] = ack; |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | // @ts-ignore |
| 472 | const timer = this.io.setTimeoutFn(() => { |
| 473 | delete this.acks[id]; |
| 474 | for (let i = 0; i < this.sendBuffer.length; i++) { |
| 475 | if (this.sendBuffer[i].id === id) { |
| 476 | debug("removing packet with ack id %d from the buffer", id); |
| 477 | this.sendBuffer.splice(i, 1); |
| 478 | } |
| 479 | } |
| 480 | debug("event with ack id %d has timed out after %d ms", id, timeout); |
| 481 | ack.call(this, new Error("operation has timed out")); |
| 482 | }, timeout); |
| 483 | |
| 484 | const fn = (...args: any[]) => { |
| 485 | // @ts-ignore |
| 486 | this.io.clearTimeoutFn(timer); |
| 487 | ack.apply(this, args); |
| 488 | }; |
| 489 | fn.withError = true; |
| 490 | |
| 491 | this.acks[id] = fn; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Emits an event and waits for an acknowledgement |
no test coverage detected