mirror of
https://github.com/stablyai/orca.git
synced 2026-10-08 16:02:37 +00:00
59 lines
1.3 KiB
TypeScript
59 lines
1.3 KiB
TypeScript
import { scheduleSharedControlReconnect } from './remote-runtime-shared-control-state'
|
|
|
|
export class SharedControlReconnectScheduler {
|
|
private timer: ReturnType<typeof setTimeout> | null = null
|
|
private attempt = 0
|
|
|
|
get isScheduled(): boolean {
|
|
return this.timer !== null
|
|
}
|
|
|
|
get attemptCount(): number {
|
|
return this.attempt
|
|
}
|
|
|
|
schedule(args: {
|
|
intentionallyClosed: boolean
|
|
delaysMs: readonly number[]
|
|
open: () => void
|
|
}): void {
|
|
// Why: a passive subscription owns recovery until its caller closes it; roaming outages are unbounded.
|
|
const scheduled = scheduleSharedControlReconnect({
|
|
...args,
|
|
current: this.timer,
|
|
reconnectAttempt: this.attempt,
|
|
open: () => {
|
|
this.timer = null
|
|
args.open()
|
|
}
|
|
})
|
|
this.timer = scheduled.timer
|
|
this.attempt = scheduled.reconnectAttempt
|
|
}
|
|
|
|
scheduleWithDefaultBackoff(intentionallyClosed: boolean, open: () => void): void {
|
|
this.schedule({
|
|
intentionallyClosed,
|
|
delaysMs: [250, 500, 1000, 2000, 4000, 8000, 15_000, 30_000],
|
|
open
|
|
})
|
|
}
|
|
|
|
clear(): void {
|
|
if (this.timer) {
|
|
clearTimeout(this.timer)
|
|
this.timer = null
|
|
}
|
|
}
|
|
|
|
clearWhenIdle(isIdle: boolean): void {
|
|
if (isIdle) {
|
|
this.clear()
|
|
}
|
|
}
|
|
|
|
resetAttempt(): void {
|
|
this.attempt = 0
|
|
}
|
|
}
|