@blac/react
Version:
React bindings for BlaC — useBloc hook with automatic re-render optimization
32 lines (31 loc) • 1.59 kB
TypeScript
/**
* Build a per-consumer proxy pair for a bloc instance.
*
* Returns two proxies:
* - `proxy` — the stable outer proxy returned to the consumer. Getter
* properties are invoked with `thisProxy` as `this` so that `this.state`
* reads inside getters are redirected to the current render's tracking
* proxy. Non-getter properties fall through to the live instance.
* - `thisProxy` — the inner `this`-proxy used as the receiver for getter
* calls. Intercepts `state` to return `trackedStateRef.current` when a
* tracking context is active; otherwise falls through to the live value.
* When `onDepHandle` is supplied, a read off `this` whose value carries the
* `DEP_BRAND` symbol (i.e. a `depend()` handle) is routed through
* `onDepHandle`, which returns a per-consumer wrapper whose `.track()` opts
* the consumer into cross-bloc reactivity. Core stays decoupled — detection
* is purely by the `DEP_BRAND` symbol.
*
* Both allocations happen exactly once per bloc acquisition (inside `useMemo`)
* so the proxies are stable across renders.
*
* @param onDepHandle - Optional callback invoked when a getter reads a branded
* dep handle off `this`. Receives the original handle and returns the value
* to expose in its place (the session-bound wrapper). The callback is
* responsible for caching wrappers per handle to avoid re-allocation.
*/
export declare function buildTrackedProxy<T extends object>(instance: T, trackedStateRef: {
current: unknown;
}, onDepHandle?: (handle: object) => unknown): {
proxy: T;
thisProxy: T;
};