Skip to content

Commit

Permalink
feat: retry failed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinSauce committed Sep 21, 2022
1 parent 126ecbe commit a75b988
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/BaseDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,22 @@ export class BaseDevice {
command: Command,
options: CommandOptions = {}
): Promise<string | ResponseData> {
return this.#queue.enqueue(() =>
this.runCommand(command, options)
) as Promise<string | ResponseData>;
const debug = createDebug('pmu:queueCommand');

return this.#queue.enqueue(async () => {
// The API has issues with rapid-fire commands, a single retry appears to be enough to be reliable
try {
const response = await this.runCommand(command, options); // DO NOT "simplify" this, we need to await in try
return response;
} catch (error) {
if (typeof error === 'string' && error.includes('timed out')) {
debug('Command timed out, retrying');
await this.runCommand(Command.Reset);
return this.runCommand(command, options);
}
throw error;
}
}) as Promise<string | ResponseData>;
}

disconnect(): Promise<void> {
Expand Down

0 comments on commit a75b988

Please sign in to comment.