Packages > @fluidframework/tree > TreeNodeApi

TreeNodeApi Interface

Provides various functions for analyzing TreeNode s.

Signature

export interface TreeNodeApi

Properties

Property Modifiers Type Description
status readonly (node: TreeNode) => TreeStatus Returns the TreeStatus of the given node.

Methods

Method Return Type Description
is(value, schema) value is NodeFromSchema<TSchema> Narrow the type of the given value if it satisfies the given schema.
key(node) string | number The key of the given node under its parent.
on(node, eventName, listener) () => void Register an event listener on the given node.
parent(node) TreeNode | undefined Return the node under which this node resides in the tree (or undefined if this is a root node of the tree).
schema(node) TreeNodeSchema<string, NodeKind, unknown, T> The schema information for this node.
shortId(node) number | string | undefined Returns the identifier of the given node in the most compressed form possible.

Property Details

status

Returns the TreeStatus of the given node.

Signature

readonly status: (node: TreeNode) => TreeStatus;

Method Details

is

Narrow the type of the given value if it satisfies the given schema.

Signature

is<TSchema extends TreeNodeSchema>(value: unknown, schema: TSchema): value is NodeFromSchema<TSchema>;
Type Parameters
Parameter Constraint Description
TSchema TreeNodeSchema

Example

if (node.is(myNode, point)) {
    const y = myNode.y; // `myNode` is now known to satisfy the `point` schema and therefore has a `y` coordinate.
}

Parameters

Parameter Type Description
value unknown
schema TSchema

Returns

Return type: value is NodeFromSchema <TSchema>

key

The key of the given node under its parent.

Signature

key(node: TreeNode): string | number;

Remarks

If node is an element in a TreeArrayNode , this returns the index of node in the array node (a number). Otherwise, this returns the key of the field that it is under (a string).

Parameters

Parameter Type Description
node TreeNode

Returns

Return type: string | number

on

Register an event listener on the given node.

Signature

on<K extends keyof TreeChangeEvents>(node: TreeNode, eventName: K, listener: TreeChangeEvents[K]): () => void;
Type Parameters
Parameter Constraint Description
K keyof TreeChangeEvents

Parameters

Parameter Type Description
node TreeNode The node whose events should be subscribed to.
eventName K Which event to subscribe to.
listener TreeChangeEvents[K] The callback to trigger for the event. The tree can be read during the callback, but it is invalid to modify the tree during this callback.

Returns

A callback function which will deregister the event. This callback should be called only once.

Return type: () => void

parent

Return the node under which this node resides in the tree (or undefined if this is a root node of the tree).

Signature

parent(node: TreeNode): TreeNode | undefined;

Parameters

Parameter Type Description
node TreeNode

Returns

Return type: TreeNode | undefined

schema

The schema information for this node.

Signature

schema<T extends TreeNode | TreeLeafValue>(node: T): TreeNodeSchema<string, NodeKind, unknown, T>;
Type Parameters
Parameter Constraint Description
T TreeNode | TreeLeafValue

Parameters

Parameter Type Description
node T

Returns

Return type: TreeNodeSchema <string, NodeKind , unknown, T>

shortId

Returns the identifier of the given node in the most compressed form possible.

Signature

shortId(node: TreeNode): number | string | undefined;

Remarks

If the node’s identifier is a valid StableNodeKey, then this will return a unique process-local integer corresponding to that identifier. If the node’s identifier is any other string, then this will return that string. If the node has no identifier (that is, it has no field of an identifier field kind), then this returns undefined.

Parameters

Parameter Type Description
node TreeNode

Returns

Return type: number | string | undefined