| 681 | } |
| 682 | |
| 683 | private async waitForConnected(timeoutMs: number): Promise<boolean> { |
| 684 | if (this.socket.connected) { |
| 685 | return true |
| 686 | } |
| 687 | |
| 688 | this.socket.connect() |
| 689 | |
| 690 | return await new Promise<boolean>((resolve) => { |
| 691 | let settled = false |
| 692 | |
| 693 | const cleanup = () => { |
| 694 | this.socket.off('connect', onConnect) |
| 695 | clearTimeout(timeout) |
| 696 | } |
| 697 | |
| 698 | const onConnect = () => { |
| 699 | if (settled) return |
| 700 | settled = true |
| 701 | cleanup() |
| 702 | resolve(true) |
| 703 | } |
| 704 | |
| 705 | const timeout = setTimeout(() => { |
| 706 | if (settled) return |
| 707 | settled = true |
| 708 | cleanup() |
| 709 | resolve(false) |
| 710 | }, Math.max(0, timeoutMs)) |
| 711 | |
| 712 | this.socket.on('connect', onConnect) |
| 713 | }) |
| 714 | } |
| 715 | |
| 716 | private async drainLock(lock: AsyncLock, timeoutMs: number): Promise<boolean> { |
| 717 | if (timeoutMs <= 0) { |