Skip to content

Commit

Permalink
Fix hanging connect (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobqvist authored Nov 11, 2021
1 parent fade77b commit ba304a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 7 additions & 5 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -195,6 +195,8 @@ export class Reader {
// no packet was available in minLength, so we wait for another byte.
minLength++;
}

throw TimeoutError;
}
}

Expand Down

0 comments on commit ba304a7

Please sign in to comment.