import {Op} from "../op/op.js" import {Signal} from "./signal.js" export class OpSignal extends Signal> { #relevant = 0 constructor(op: Op.For) { super(op) } async load(fn: () => Promise) { const id = ++this.#relevant return Op.load( op => { if (this.#relevant === id) this.value = op }, fn, ) } setLoading() { this.value = Op.loading() } setError(reason: string) { this.value = Op.error(reason) } setReady(payload: V) { this.value = Op.ready(payload) } isLoading(): this is Signal { return Op.is.loading(this.value) } isError(): this is Signal { return Op.is.error(this.value) } isReady(): this is Signal> { return Op.is.ready(this.value) } get payload() { return Op.payload(this.value) as ( this extends Signal> ? V : this extends Signal ? undefined : this extends Signal ? undefined : V | undefined ) } select(choices: Op.Choices) { return Op.select(this.value, choices) } }