Skip to content

Commit

Permalink
fix: LC start should never block
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluard committed Apr 2, 2024
1 parent ecc8f8b commit 62db411
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions packages/light-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,10 @@ export class Lightclient {
}

/**
* @returns a `Promise` that will resolve once `LightclientEvent.statusChange` with `RunStatusCode.started` value is emitted
* @returns a `Promise` that will resolve once `runStatus` equals `RunStatusCode.started`
*/
start(): Promise<void> {
const startPromise = new Promise<void>((resolve) => {
const lightclientStarted = (status: RunStatusCode): void => {
if (status === RunStatusCode.started) {
this.emitter.off(LightclientEvent.statusChange, lightclientStarted);
resolve();
}
};
this.emitter.on(LightclientEvent.statusChange, lightclientStarted);
});

void this.runLoop();

return startPromise;
return this.runLoop();
}

stop(): void {
Expand Down Expand Up @@ -272,17 +260,14 @@ export class Lightclient {
}

// Wait for the next epoch
if (this.runStatus.code !== RunStatusCode.started) {
return;
} else {
try {
await sleep(timeUntilNextEpoch(this.config, this.genesisTime), this.runStatus.controller.signal);
} catch (e) {
if (isErrorAborted(e)) {
return;
}
throw e;
try {
const runStatus = this.runStatus as {code: RunStatusCode.started; controller: AbortController}; // At this point, client is started
await sleep(timeUntilNextEpoch(this.config, this.genesisTime), runStatus.controller.signal);
} catch (e) {
if (isErrorAborted(e)) {
return;
}
throw e;
}
}
}
Expand Down

0 comments on commit 62db411

Please sign in to comment.