diff --git a/src/index.ts b/src/index.ts index 6b6dbb4..71b038a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,10 +2,10 @@ // Use of this source code is governed by an MIT-style license that can be // found in the LICENSE file. +import { ConnectError, UnknownChipFamilyError } from "./errors"; +import { Reader } from "./reader"; import { ESP32, Stub } from "./stubs"; import { sleep, toByteArray, toHex, Uint8Buffer, Uint8BufferSlipEncode } from "./util"; -import { UnknownChipFamilyError, ConnectError } from "./errors"; -import { Reader } from "./reader"; export enum ChipFamily { ESP32 = "esp32", diff --git a/src/reader.ts b/src/reader.ts index d7b4b3b..41f26bd 100644 --- a/src/reader.ts +++ b/src/reader.ts @@ -2,14 +2,14 @@ // Use of this source code is governed by an MIT-style license that can be // found in the LICENSE file. -import { isTransientError, Uint8Buffer, sleep } from "./util"; import { AlreadyRunningError, + NotListeningError, NotRunningError, ReadAlreadyInProgressError, TimeoutError, - NotListeningError, } from "./errors"; +import { isTransientError, sleep, Uint8Buffer } from "./util"; export type Unlisten = () => void; @@ -184,9 +184,9 @@ export class Reader { if (this.listenRef <= 0) { throw NotListeningError; } - const deadline = Date.now() + timeoutMs; - while (true) { - await this.waitData(minLength, deadline - Date.now()); + let maxRetries = 1000; + while (maxRetries--) { + await this.waitData(minLength, timeoutMs); const res = this.buffer.packet(); if (res !== undefined) { @@ -195,6 +195,8 @@ export class Reader { // no packet was available in minLength, so we wait for another byte. minLength++; } + + throw TimeoutError; } }