41 lines
1.9 KiB
TypeScript
41 lines
1.9 KiB
TypeScript
export type OwnedBatchState = "pending" | "running" | "unknown" | "committed"
|
|
/** A detached batch is never merged with new arrivals or replayed after an
|
|
* uncertain commit. Only a barrier-backed receipt lookup can resolve unknown. */
|
|
export class OwnedFlowBatch<T> {
|
|
private stateValue: OwnedBatchState = "pending"
|
|
constructor(readonly id: string, private proof: string, readonly payload: T) {}
|
|
get sourceProof():string { return this.proof }
|
|
assertProof(current:string):void {
|
|
if(!this.proof||current!==this.proof)throw new Error("Owned flow batch payload changed after identification")
|
|
}
|
|
prove(proof:string):void {
|
|
if(this.stateValue!=="pending"||this.proof||!/^[a-f0-9]{64}$/.test(proof))throw new Error("Owned batch proof cannot change")
|
|
this.proof=proof
|
|
}
|
|
get state(): OwnedBatchState { return this.stateValue }
|
|
assertResolved():void {
|
|
if(this.stateValue!=="committed")throw new Error("Cannot reset or discard unresolved owned flow batch: "+this.stateValue)
|
|
}
|
|
begin(): void {
|
|
if (this.stateValue !== "pending") throw new Error("Owned flow batch cannot dispatch: " + this.stateValue)
|
|
if(!/^[a-f0-9]{64}$/.test(this.proof))throw new Error("Owned batch has no source proof")
|
|
this.stateValue = "running"
|
|
}
|
|
committed(): void {
|
|
if (this.stateValue !== "running") throw new Error("Flow commit without a running batch")
|
|
this.stateValue = "committed"
|
|
}
|
|
rolledBack(): void {
|
|
if (this.stateValue !== "running") throw new Error("Flow rollback without a running batch")
|
|
this.stateValue = "pending"
|
|
}
|
|
unknown(): void {
|
|
if (this.stateValue !== "running") throw new Error("Unknown flow outcome without a running batch")
|
|
this.stateValue = "unknown"
|
|
}
|
|
reconciled(receipt: "committed" | "absent-after-barrier"): void {
|
|
if (this.stateValue !== "unknown") throw new Error("Only an unknown batch can be reconciled")
|
|
this.stateValue = receipt === "committed" ? "committed" : "pending"
|
|
}
|
|
}
|