Class UnserializableComponentWrapper<S extends Serializable,T extends com.vaadin.flow.component.Component>
- Type Parameters:
S- the type of the state object that is created with the serializerT- the type of the wrappedComponent
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasStyle,Serializable
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic voidafterSerialization(UnserializableComponentWrapper<?, ?> wrapper) Restores the UI by adding the unserializable component to the component tree using the wrapper.static voidbeforeSerialization(UnserializableComponentWrapper<?, ?> wrapper) 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, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods 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 unserializableComponentto be wrappedserializer- the serializer function that generates the serializable state object from the wrappedComponentduring the serializationdeserializer- the deserializer function that reconstructs theComponentfrom scratch using the state object, after the entire graph has been restored upon the deserialization
-
-
Method Details
-
beforeSerialization
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
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
-