withView.ts
Purpose
This module implements the withView() extension and the provideView() provider used to pass a ViewBinding into controller creation.
Overview
The file connects the controller runtime to the UI layer. It defines ViewControllerContext, can infer view props from controller context, and ensures that a view binding is actually present in extension params.
Conceptual Architecture
The module is built around the extension mechanism from controller.ts:
withView<TProps>()returns anExtensionFnthat extractsViewBindingthrough the service keyNRGY_EXTENSION_VIEW_KEY.- If the view is missing,
ControllerConstructorErroris thrown. provideView(view)packages the view object into anExtensionParamsProvider.- The types
ViewControllerContext<TProps>andInferViewPropsFromControllerContext<TContext, ElseType>form a type-safe bridge between a controller and its view props.
Public API Description
NRGY_EXTENSION_VIEW_KEY
- Service key used to store
ViewBindinginside extension params.
ViewControllerContext<TProps>
- Controller context extended with the
viewfield.
InferViewPropsFromControllerContext<TContext, ElseType>
- Extracts the view prop type from a controller context.
withView<TProps>(): ExtensionFn<BaseControllerContext, ViewControllerContext<TProps>>
- Returns an extension that adds
viewto the controller context.
provideView(view): ExtensionParamsProvider
- Wraps a
ViewBindinginto a provider for controller creation.
Usage Examples
ts
import { declareController, provideView, withView, createViewProxy } from '@nrgyjs/core';
const GreetingController = declareController()
.extend(withView<{ name: string }>())
.apply(({ view }) => ({
greet: () => `Hello, ${view.props.name()}!`,
}));
const view = createViewProxy({ name: 'Ada' });
const controller = new GreetingController([provideView(view)]);