Class UnserializableComponentWrapper<S extends Serializable,T extends com.vaadin.flow.component.Component>

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.kubernetes.starter.sessiontracker.UnserializableComponentWrapper<S,T>
Type Parameters:
S - the type of the state object that is created with the serializer
T - the type of the wrapped Component
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasStyle, Serializable

@Tag("div") public class UnserializableComponentWrapper<S extends Serializable,T extends com.vaadin.flow.component.Component> extends com.vaadin.flow.component.Component
A wrapper component that allows an otherwise unserializable Component to be serialized and deserialized using the provided serializer and deserializer functions.

During serialization, the serializer generates a serializable state object from the wrapped component. This state object is intended to store serializable and cacheable properties of the component. Upon deserialization, the deserializer reconstructs the component from scratch using the state object, after the entire graph has been restored. Developers are responsible for ensuring that the necessary component properties are properly persisted and restored. For example:

 
 record State(int x) implements Serializable {
 }

 public CustomView() {
     Unserializable unserializable = new Unserializable();
     UnserializableComponentWrapper<State, Unserializable> wrapper = new UnserializableComponentWrapper<>(
             unserializable, CustomView::serializer,
             CustomView::deserializer);
     add(wrapper);
 }

 private static State serializer(Unserializable unserializable) {
     var state = new State();
     state.setX(unserializable.getX());
     return state;
 }

 private static Unserializable deserializer(State state) {
     var unserializable = new Unserializable();
     unserializable.setX(state.getX());
     return unserializable;
 }
 
 
Unserializable components are temporarily removed from the component tree during serialization and reinserted after deserialization. Any UI changes caused by their removal and re-addition are silently ignored.

Important Note: Any attach or detach listeners registered on the wrapped component will still be triggered.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    UnserializableComponentWrapper(T component, com.vaadin.flow.function.SerializableFunction<T,S> serializer, com.vaadin.flow.function.SerializableFunction<S,T> deserializer)
    Constructs a new unserializable component wrapper instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Restores the UI by adding the unserializable component to the component tree using the wrapper.
    static void
    Prepares the UI for serialization by removing wrapped unserializable component from the component tree.

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
  • Constructor Details

    • UnserializableComponentWrapper

      public UnserializableComponentWrapper(T component, com.vaadin.flow.function.SerializableFunction<T,S> serializer, com.vaadin.flow.function.SerializableFunction<S,T> deserializer)
      Constructs a new unserializable component wrapper instance.
      Parameters:
      component - the unserializable Component to be wrapped
      serializer - the serializer function that generates the serializable state object from the wrapped Component during the serialization
      deserializer - the deserializer function that reconstructs the Component from scratch using the state object, after the entire graph has been restored upon the deserialization
  • Method Details

    • beforeSerialization

      public static void beforeSerialization(UnserializableComponentWrapper<?,?> wrapper)
      Prepares the UI for serialization by removing wrapped unserializable component from the component tree.

      The changes to the UI caused by the removal are silently ignored.

      IMPORTANT NOTE: Any detach listener registered on the wrapped components will be executed.

      For internal use only.

      Parameters:
      wrapper - the wrapper that contains the unserializable component to remove
    • afterSerialization

      public static void afterSerialization(UnserializableComponentWrapper<?,?> wrapper)
      Restores the UI by adding the unserializable component to the component tree using the wrapper.

      The changes to the UI caused by re-adding the components are silently ignored.

      IMPORTANT NOTE: Any attach listener registered on the wrapped components will be executed.

      For internal use only.

      Parameters:
      wrapper - the wrapper the unserializable component is added to