Skip to content

Commit

Permalink
Fix Client:Get(remoteId) causing delays (#84)
Browse files Browse the repository at this point in the history
* Only poll if remote doesn't exist

* Update index.ts

---------

Co-authored-by: Jonathan Holmes <[email protected]>
  • Loading branch information
boatbomber and Vorlias authored Mar 28, 2023
1 parent 622aed1 commit b6bd0f2
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,25 @@ export function format(message: string, vars: { [name: string]: unknown }) {
/** @internal */
export function waitForRemote<K extends keyof RemoteTypes>(remoteType: K, name: string, timeout: number) {
return Promise.defer<RemoteTypes[K]>((resolve, reject) => {
let i = 0;
let result: RemoteTypes[K] | undefined;
do {
const [step] = runService.Heartbeat.Wait();
i += step;
result = findRemote(remoteType, name);
} while (i < timeout && !result);
// First, check if remote already exists
let result: RemoteTypes[K] | undefined = findRemote(remoteType, name);
if (result) {
resolve(result);
} else {
reject(`Timed out while waiting for ${remoteType} '${name}' after ${timeout} seconds.`);
return;
}

// If not, poll until timeout
let elapsed = 0;
while (elapsed < timeout) {
[elapsed] += runService.Heartbeat.Wait();
result = findRemote(remoteType, name);
if (result) {
resolve(result);
return;
}
}

reject(`Timed out while waiting for ${remoteType} '${name}' after ${elapsed} seconds.`);
});
}

Expand Down

0 comments on commit b6bd0f2

Please sign in to comment.