* Add the packet to the queue. * @param args * @private
(args: unknown[])
| 527 | * @private |
| 528 | */ |
| 529 | private _addToQueue(args: unknown[]) { |
| 530 | let ack; |
| 531 | if (typeof args[args.length - 1] === "function") { |
| 532 | ack = args.pop(); |
| 533 | } |
| 534 | |
| 535 | const packet = { |
| 536 | id: this._queueSeq++, |
| 537 | tryCount: 0, |
| 538 | pending: false, |
| 539 | args, |
| 540 | flags: Object.assign({ fromQueue: true }, this.flags), |
| 541 | }; |
| 542 | |
| 543 | args.push((err, ...responseArgs) => { |
| 544 | if (packet !== this._queue[0]) { |
| 545 | return debug("packet [%d] already acknowledged", packet.id); |
| 546 | } |
| 547 | const hasError = err !== null; |
| 548 | if (hasError) { |
| 549 | if (packet.tryCount > this._opts.retries) { |
| 550 | debug( |
| 551 | "packet [%d] is discarded after %d tries", |
| 552 | packet.id, |
| 553 | packet.tryCount, |
| 554 | ); |
| 555 | this._queue.shift(); |
| 556 | if (ack) { |
| 557 | ack(err); |
| 558 | } |
| 559 | } |
| 560 | } else { |
| 561 | debug("packet [%d] was successfully sent", packet.id); |
| 562 | this._queue.shift(); |
| 563 | if (ack) { |
| 564 | ack(null, ...responseArgs); |
| 565 | } |
| 566 | } |
| 567 | packet.pending = false; |
| 568 | return this._drainQueue(); |
| 569 | }); |
| 570 | |
| 571 | this._queue.push(packet); |
| 572 | this._drainQueue(); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Send the first packet of the queue, and wait for an acknowledgement from the server. |
no test coverage detected