Skip to content

CompositionContext

Namespace: Hex1b.Composition

Assembly: Hex1b.dll

The hooks-style context passed to . Provides per-instance state storage and an ambient context API for sharing values between a composite and its descendants.

csharp
public sealed class CompositionContext : WidgetContext<Hex1bWidget>

Inheritance

Object → WidgetContext<Hex1bWidget> → CompositionContext

Properties

CancellationToken

The cancellation token associated with the current reconciliation pass. Composites that perform asynchronous work in Build overrides should observe this token.

Returns: CancellationToken

csharp
public CancellationToken CancellationToken { get; }

IsNew

True the first time this composite is reconciled (i.e. its node was just created). Use this to perform one-time initialisation against state objects after they are created by .

Returns: Boolean

csharp
public bool IsNew { get; }

Methods

Provide<T>(T)

Publishes value as ambient context for descendants of this composite. Any descendant within the same subtree can resolve it via or .

Parameters:

  • value (<T>): The value to publish.
csharp
public void Provide<T>(T value) where T : class

Require<T>()

Resolves the nearest ambient value of type T from an ancestor composite, throwing if none has been provided.

Returns: <T>

csharp
public T Require<T>() where T : class

Use<T>()

Resolves the nearest ambient value of type T from an ancestor composite. Returns null if no ancestor has provided one.

Returns: <T>

csharp
public T? Use<T>() where T : class

UseState<T>(Func<T>)

Gets or creates a per-instance state object of type T. The factory is invoked once on first call; subsequent calls return the same instance for the lifetime of this composite node.

Parameters:

  • factory (Func<<T>>): Factory invoked on first access to create the state object.

Returns: <T>

csharp
public T UseState<T>(Func<T> factory) where T : class

Remarks

A new is constructed on every reconciliation pass, but the underlying composite node persists, so calls to return the same instance across frames.

Every value published with lives on the composite's own node; descendants resolve it by walking the ancestor chain. Use this for ambient values that should flow naturally down a subtree (e.g. theme overrides, form context, controllers).

derives from , so the full fluent widget-building API (ctx.Text(...), ctx.VStack(...), ctx.Button(...), etc.) is available directly inside Build.

Released under the MIT License.