Skip to Content
Bear documentation

context

Import from bear/engine/context.

Interfaces

BearContext

Defined in: bear/engine/context.ts:10

A typed context key created via createContext<T>(). The object is the lookup key; the generic carries the value type.

Type Parameters

Type Parameter
T

Properties

PropertyModifierTypeDescription
_type?readonlyTPhantom field so T is not unused.
keyreadonlysymbolUnique symbol used as the map key on TreeNode.context.
labelreadonlystringDebug label for error messages.

Functions

createContext()

function createContext<T>(label): BearContext<T>;

Defined in: bear/engine/context.ts:20

Create a typed context key. Each call produces a unique key.

Type Parameters

Type Parameter
T

Parameters

ParameterType
labelstring

Returns

BearContext<T>


ctx()

Call Signature

function ctx<T>(context, value): ContextMap;

Defined in: bear/engine/context.ts:29

Build a ContextMap from a single typed context-value pair.

Type Parameters
Type Parameter
T
Parameters
ParameterType
contextBearContext<T>
valueT
Returns

ContextMap

Call Signature

function ctx(...maps): ContextMap;

Defined in: bear/engine/context.ts:31

Merge multiple ContextMaps into one.

Parameters
ParameterType
...mapsContextMap[]
Returns

ContextMap


getContext()

function getContext<T>(node, context): T | undefined;

Defined in: bear/engine/context.ts:63

Walk the ancestor chain from node upward, returning the first value found for the given context in any ancestor's context map.

The node itself is checked first, then its parent, grandparent, etc. Returns undefined if no ancestor provides the context.

Type Parameters

Type Parameter
T

Parameters

ParameterType
nodeAncestorNode
contextBearContext<T>

Returns

T | undefined


getContextWithProvider()

function getContextWithProvider<T>(node, context): 
  | {
  provider: AncestorNode;
  value: T;
}
  | undefined;

Defined in: bear/engine/context.ts:75

Like getContext, but also returns the node that provided the value, for consumers that need the provider's coordinate frame (e.g. gridline planes live in the grid provider's frame).

Type Parameters

Type Parameter
T

Parameters

ParameterType
nodeAncestorNode
contextBearContext<T>

Returns

| { provider: AncestorNode; value: T; } | undefined

On this page