viewProxy.ts
Purpose
This module implements ViewProxy and createViewProxy(), representing a view binding as a programmatically controlled object.
Overview
ViewProxy is especially useful in tests and adapter layers. It simulates a view by storing props as atoms, tracking mount/update/unmount status, and broadcasting lifecycle events to listeners.
Conceptual Architecture
The implementation consists of several parts:
- A
statusatom stores the current view state. - For each initial prop, a source atom is created while a read-only version is exposed publicly.
- Three
Emitterinstances handlemount,update, andunmountevents. - A local
Scopeowns the prop atoms and is destroyed indestroy().
Methods mount(), update(), unmount(), and destroy() synchronize the internal state and notify subscribers.
Public API Description
type ViewProxy<TProps>
- Extends
ViewBinding<TProps>. - Adds
mount(),update(props?),unmount(), anddestroy().
createViewProxy(): ViewProxy<Record<string, never>>
- Creates a proxy without initial props.
createViewProxy<TProps>(initialProps: TProps): ViewProxy<TProps>
initialProps: initial props of the view.- Returns a
ViewProxy<TProps>instance.
Usage Examples
ts
import { createViewProxy } from '@nrgyjs/core';
const view = createViewProxy({ id: '42' });
view.onMount(() => {
console.log(view.props.id());
});
view.mount();
view.update({ id: '43' });
view.destroy();