Skip to content

Commit

Permalink
feat: make ready state and error public (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek authored Jun 25, 2024
1 parent 5e105d4 commit e1ed616
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class UnleashClient extends TinyEmitter {
private usePOSTrequests = false;
private started = false;
private sdkState: SdkState;
private lastError: any;

constructor({
storageProvider,
Expand Down Expand Up @@ -206,6 +207,7 @@ export class UnleashClient extends TinyEmitter {
console.error(error);
this.sdkState = 'error';
this.emit(EVENTS.ERROR, error);
this.lastError = error;
resolve();
});
});
Expand Down Expand Up @@ -393,6 +395,14 @@ export class UnleashClient extends TinyEmitter {
this.metrics.stop();
}

public isReady(): boolean {
return this.readyEventEmitted;
}

public getError(): SdkState {
return this.sdkState === 'error' ? this.lastError : undefined;
}

private async resolveSessionId(): Promise<string> {
if (this.context.sessionId) {
return this.context.sessionId;
Expand Down Expand Up @@ -485,6 +495,10 @@ export class UnleashClient extends TinyEmitter {
type: 'HttpError',
code: response.status,
});
this.lastError = {
type: 'HttpError',
code: response.status,
};
}
} catch (e) {
if (!(e instanceof DOMException && e.name === 'AbortError')) {
Expand All @@ -494,6 +508,7 @@ export class UnleashClient extends TinyEmitter {
);
this.sdkState = 'error';
this.emit(EVENTS.ERROR, e);
this.lastError = e;
}
} finally {
this.abortController = null;
Expand Down

0 comments on commit e1ed616

Please sign in to comment.