mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 04:11:35 +00:00
38 lines
1 KiB
TypeScript
38 lines
1 KiB
TypeScript
import type { UpdaterApplyResultWire, UpdaterStrategy } from './index'
|
|
|
|
/** Preserve one native selection across check/apply and one owner through handoff. */
|
|
export class UpdateOperation {
|
|
private strategy: Promise<UpdaterStrategy | null> | undefined
|
|
private applying: boolean = false
|
|
|
|
constructor(private readonly initialize: () => Promise<UpdaterStrategy | null>) {}
|
|
|
|
resolve(): Promise<UpdaterStrategy | null> {
|
|
this.strategy ??= this.initialize().catch((error: Error): never => {
|
|
this.strategy = undefined
|
|
throw error
|
|
})
|
|
|
|
return this.strategy
|
|
}
|
|
|
|
async apply(run: () => Promise<UpdaterApplyResultWire>): Promise<UpdaterApplyResultWire> {
|
|
if (this.applying) {
|
|
throw new Error('An update is already in progress.')
|
|
}
|
|
|
|
this.applying = true
|
|
let handedOff: boolean = false
|
|
|
|
try {
|
|
const result: UpdaterApplyResultWire = await run()
|
|
handedOff = result.handedOff === true
|
|
|
|
return result
|
|
} finally {
|
|
if (!handedOff) {
|
|
this.applying = false
|
|
}
|
|
}
|
|
}
|
|
}
|