memlnaut-nisps/manifold/src/engine/useEngine.ts
monkey-w1n5t0n 19b7f7eee8 feat(manifold): add convertible React front-end + simplify console chrome
Adds the Manifold app (manifold/) — the convertible-mode React front-end on
the real NISPS ML+audio engine, served live at meml.lnfinitemonkeys.org/next/.

Console chrome trimmed per UI pass:
- drop mode label + subtitle from the top-left overlay (keep MEMLNaut wordmark)
- remove the composite split preset/ratio readout (top-centre)
- remove the OUTPUT corner tag above the bars
- remove the A/B compare toggle from the verdict cluster
- remove the follow button + input/noise readout (bottom-left)
- remove AltitudeNav focus switcher (bottom-right)
2026-06-28 03:28:45 +02:00

43 lines
1.6 KiB
TypeScript

/**
* useEngine / useEngineVersion — React hooks over the EngineApi.
*
* `useEngine()` returns the EngineApi from context (or null before it loads).
*
* `useEngineVersion()` subscribes to engine state changes via
* `useSyncExternalStore(engine.subscribe, engine.version)`. It returns the
* VERSION COUNTER (a number), NOT the output array — so a component re-renders
* when engine state changes but reads the live `Float32Array` imperatively
* (`engine.getOutputs()` / `engine.routedOutput()`) inside a rAF loop or on
* render. This keeps per-frame audio inference off React's render cycle.
*/
import { useContext, useSyncExternalStore } from 'react';
import type { EngineApi } from './engine-api';
import { EngineContext } from './EngineProvider';
/** The EngineApi from context, or null until the WASM has loaded. */
export function useEngine(): EngineApi | null {
return useContext(EngineContext);
}
/** Like {@link useEngine} but throws if used outside a ready provider. */
export function useEngineOrThrow(): EngineApi {
const engine = useContext(EngineContext);
if (!engine) {
throw new Error('useEngineOrThrow: no EngineApi in context (still loading or no provider)');
}
return engine;
}
/**
* Subscribe to the engine's monotonic version counter. Re-renders the caller
* on any engine state change; the returned number is the counter (read the
* live arrays imperatively from the engine). Returns 0 when there's no engine.
*/
export function useEngineVersion(engine: EngineApi | null): number {
return useSyncExternalStore(
(cb) => (engine ? engine.subscribe(cb) : () => {}),
() => (engine ? engine.version() : 0),
() => 0,
);
}