Client API
The Spark Client API provides the React hooks and controls necessary to manage your background training workers from your main user interface.
spark.use()
The primary entry point for React applications. It manages the worker lifecycle and provides reactive bindings.
function use<T>(workerFn: () => void): SparkProxy<T>;Example Usage
const s = spark.use(myWorker);
// Access reactive state
const loss = s.state.loss.value;
// Call worker functions
const onStart = () => s.train(100);The Controller (s.ctrl)
The ctrl object gives you direct control over the execution state of the current worker function.
| Method | Action |
|---|---|
| pause() | Suspends execution at the next spark.checkpoint() |
| resume() | Resumes a previously paused worker |
| stop() | Terminates the current function execution immediately |
<button onClick={() => s.ctrl.stop()}>Emergency Stop</button>Managing Loading States
Because workers are initialized asynchronously, the s.state object might be undefined for the first few frames. Always use optional chaining or provide default values.
const accuracy = s.state?.accuracy?.value ?? 0;Next Steps
- Worker API - See how to expose values from the other side of the bridge.
- File I/O - Connect your client to the cloud.