Class StateTree
- java.lang.Object
-
- com.vaadin.flow.internal.StateTree
-
- All Implemented Interfaces:
NodeOwner,Serializable
public class StateTree extends Object implements NodeOwner
The state tree that is synchronized with the client-side.For internal use only. May be renamed or removed in a future release.
- Since:
- 1.0
- Author:
- Vaadin Ltd
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStateTree.BeforeClientResponseEntryA task to be executed before the client response, together with an execution sequence number and context object.static interfaceStateTree.ExecutionRegistrationA registration object for removing a task registered for execution before the client response.
-
Constructor Summary
Constructors Constructor Description StateTree(UIInternals uiInternals, Class<? extends NodeFeature>... features)Creates a new state tree with a set of features defined for the root node.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StateTree.ExecutionRegistrationbeforeClientResponse(StateNode context, SerializableConsumer<ExecutionContext> execution)Registers a task to be executed before the response is sent to the client.voidcollectChanges(Consumer<NodeChange> collector)Collects all changes made to this tree since the last timecollectChanges(Consumer)has been called.Set<StateNode>collectDirtyNodes()Gets all the nodes that have been marked.StateNodegetNodeById(int id)Finds a node with the given id.StateNodegetRootNode()Gets the root node of this state tree.UIgetUI()Gets theUIthat this tree belongs to.booleanhasDirtyNodes()Checks if there are nodes that have been marked as dirty since the last timecollectDirtyNodes()was invoked.booleanhasNode(StateNode node)Check if given node is registered to this node owner.booleanisDirty()Checks if there are changes waiting to be sent to the client side.voidmarkAsDirty(StateNode node)Marks a node owned by this instance as dirty.voidprepareForResync()Prepares the tree for resynchronization, meaning that the client will receive the same changes as when the component tree was initially attached, so that it can build the DOM tree from scratch.intregister(StateNode node)Registers a node with this node owner.voidrunExecutionsBeforeClientResponse()Called internally by the framework before the response is sent to the client.voidunregister(StateNode node)Unregisters a node from this owner.
-
-
-
Constructor Detail
-
StateTree
@SafeVarargs public StateTree(UIInternals uiInternals, Class<? extends NodeFeature>... features)
Creates a new state tree with a set of features defined for the root node.- Parameters:
features- the features of the root nodeuiInternals- the internals for the UI that this tree belongs to
-
-
Method Detail
-
getRootNode
public StateNode getRootNode()
Gets the root node of this state tree. The root node is created together with the tree and can't be detached.- Returns:
- the root node
-
register
public int register(StateNode node)
Description copied from interface:NodeOwnerRegisters a node with this node owner. The new node should already be set to be owned by this instance.
-
unregister
public void unregister(StateNode node)
Description copied from interface:NodeOwnerUnregisters a node from this owner. This must be done before the node is set to not be owned by this instance.- Specified by:
unregisterin interfaceNodeOwner- Parameters:
node- the node to unregister
-
hasNode
public boolean hasNode(StateNode node)
Description copied from interface:NodeOwnerCheck if given node is registered to this node owner.
-
getNodeById
public StateNode getNodeById(int id)
Finds a node with the given id.- Parameters:
id- the node id to look for- Returns:
- the node with the given id;
nullif the id is not registered with this tree - See Also:
StateNode.getId()
-
collectChanges
public void collectChanges(Consumer<NodeChange> collector)
Collects all changes made to this tree since the last timecollectChanges(Consumer)has been called.WARNING: This is an internal method which is not intended to be used outside. The only proper caller of this method is
UidlWriterclass (theUidlWriter::encodeChangesmethod). Any call of this method in any other place will break the expectedUIstate.- Parameters:
collector- a consumer accepting node changes
-
markAsDirty
public void markAsDirty(StateNode node)
Description copied from interface:NodeOwnerMarks a node owned by this instance as dirty. Dirty nodes are collected from an owner usingcollectDirtyNodes().- Specified by:
markAsDirtyin interfaceNodeOwner- Parameters:
node- the node to be marked as dirty
-
collectDirtyNodes
public Set<StateNode> collectDirtyNodes()
Gets all the nodes that have been marked.- Returns:
- a set of dirty nodes, in the order they were marked dirty
-
hasDirtyNodes
public boolean hasDirtyNodes()
Checks if there are nodes that have been marked as dirty since the last timecollectDirtyNodes()was invoked.- Returns:
- true if there are dirty nodes, false otherwise
-
getUI
public UI getUI()
Gets theUIthat this tree belongs to.- Returns:
- the UI that this tree belongs to
-
beforeClientResponse
public StateTree.ExecutionRegistration beforeClientResponse(StateNode context, SerializableConsumer<ExecutionContext> execution)
Registers a task to be executed before the response is sent to the client. The tasks are executed in order of registration. If tasks register more tasks, they are executed after all already registered tasks for the moment.Example: three tasks are submitted,
A,BandC, whereBproduces two more tasks during execution,DandE. The resulting execution would beABCDE.If the
StateNoderelated to the task is not attached to the document by the time the task is evaluated, the execution is postponed to before the next response.The task receives a
ExecutionContextas parameter, which contains information about the node state before the response.- Parameters:
context- the StateNode relevant for the execution. Can not benullexecution- the task to be executed. Can not benull- Returns:
- a registration that can be used to cancel the execution of the task
-
runExecutionsBeforeClientResponse
public void runExecutionsBeforeClientResponse()
Called internally by the framework before the response is sent to the client. All tasks registered atbeforeClientResponse(StateNode, SerializableConsumer)are evaluated and executed if able.
-
isDirty
public boolean isDirty()
Checks if there are changes waiting to be sent to the client side.- Returns:
trueif there are pending changes,falseotherwise
-
prepareForResync
public void prepareForResync()
Prepares the tree for resynchronization, meaning that the client will receive the same changes as when the component tree was initially attached, so that it can build the DOM tree from scratch.
-
-