Class 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
    • 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 node
        uiInternals - 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: NodeOwner
        Registers a node with this node owner. The new node should already be set to be owned by this instance.
        Specified by:
        register in interface NodeOwner
        Parameters:
        node - the node to register
        Returns:
        the id of the registered node
      • unregister

        public void unregister​(StateNode node)
        Description copied from interface: NodeOwner
        Unregisters a node from this owner. This must be done before the node is set to not be owned by this instance.
        Specified by:
        unregister in interface NodeOwner
        Parameters:
        node - the node to unregister
      • hasNode

        public boolean hasNode​(StateNode node)
        Description copied from interface: NodeOwner
        Check if given node is registered to this node owner.
        Specified by:
        hasNode in interface NodeOwner
        Parameters:
        node - node to check registration status for
        Returns:
        true if node is registered
      • 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; null if 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 time collectChanges(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 UidlWriter class (the UidlWriter::encodeChanges method). Any call of this method in any other place will break the expected UI state.

        Parameters:
        collector - a consumer accepting node changes
      • markAsDirty

        public void markAsDirty​(StateNode node)
        Description copied from interface: NodeOwner
        Marks a node owned by this instance as dirty. Dirty nodes are collected from an owner using collectDirtyNodes().
        Specified by:
        markAsDirty in interface NodeOwner
        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 time collectDirtyNodes() was invoked.
        Returns:
        true if there are dirty nodes, false otherwise
      • getUI

        public UI getUI()
        Gets the UI that 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, B and C, where B produces two more tasks during execution, D and E. The resulting execution would be ABCDE.

        If the StateNode related 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 ExecutionContext as parameter, which contains information about the node state before the response.

        Parameters:
        context - the StateNode relevant for the execution. Can not be null
        execution - the task to be executed. Can not be null
        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 at beforeClientResponse(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:
        true if there are pending changes, false otherwise
      • 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.