useAsyncControl (hook)

Molecule

State machine behind ControlTile: idle -> pending -> confirmed / mismatch / failed. `commit` returns a Promise (a rejection rolls the display back to the bound `value`); an optional `reported` value keeps the write pending until the other side agrees, else it ends as mismatch after `timeoutMs`; `confirm` parks the value until `accept()` / `dismiss()`. No transport knowledge. In kui-ejs it is `KuiControl.createAsyncControl` (modules/ui/ControlTile/scripts/async-control.js), the counterpart of the kui-react hook in libs/hooks/useAsyncControl.ts.

Success: pending then confirmed

Preview
Code
const ctl = KuiControl.createAsyncControl({ value, commit: async (n) => { await api.write(n); ctl.setValue(n); }, onChange: render });

Failure: rolls back (with Retry)

Preview
Code
const ctl = KuiControl.createAsyncControl({ value, commit: () => Promise.reject(new Error('Device offline')), onChange: render });
// snapshot().state === 'failed', snapshot().error === 'Device offline', ctl.retry()

Mismatch: the device reports a different value

Preview
Code
const ctl = KuiControl.createAsyncControl({ value, reported: deviceState, timeoutMs: 1500, commit: write, onChange: render });

Confirm before commit

Preview
Code
const ctl = KuiControl.createAsyncControl({ value, confirm: true, commit: write, onChange: render });
// ctl.set(next) -> snapshot().awaiting -> ctl.accept() | ctl.dismiss()
Source modules/ui/ControlTile/scripts/async-control.js