Fix throttle function, add retryAuth

This commit is contained in:
Jon Staab
2025-09-03 17:31:19 -07:00
parent f009801c84
commit 3b99cb7d4c
3 changed files with 23 additions and 13 deletions
+14 -2
View File
@@ -29,6 +29,8 @@ export type AuthStateEvents = {
[AuthStateEvent.Status]: (status: AuthStatus) => void
}
type Sign = (event: StampedEvent) => Promise<SignedEvent>
export class AuthState extends EventEmitter {
challenge: string | undefined
request: string | undefined
@@ -89,7 +91,7 @@ export class AuthState extends EventEmitter {
this.emit(AuthStateEvent.Status, status)
}
async doAuth(sign: (event: StampedEvent) => Promise<SignedEvent>) {
async doAuth(sign: Sign) {
if (!this.challenge) {
throw new Error("Attempted to authenticate with no challenge")
}
@@ -111,7 +113,7 @@ export class AuthState extends EventEmitter {
}
}
async attemptAuth(sign: (event: StampedEvent) => Promise<SignedEvent>) {
async attemptAuth(sign: Sign) {
this.socket.attemptToOpen()
if (![AuthStatus.Forbidden, AuthStatus.Ok].includes(this.status)) {
@@ -131,6 +133,16 @@ export class AuthState extends EventEmitter {
}
}
async retryAuth(sign: Sign) {
if (this.challenge) {
this.request = undefined
this.details = undefined
this.setStatus(AuthStatus.Requested)
}
await this.attemptAuth(sign)
}
cleanup() {
this.removeAllListeners()
this._unsubscribers.forEach(call)