use of com.vaadin.flow.data.provider.DataKeyMapper in project flow-components by vaadin.
the class LitRenderer method createJsRendererFunction.
private Registration createJsRendererFunction(Element container, DataKeyMapper<SOURCE> keyMapper, String rendererName) {
ReturnChannelRegistration returnChannel = container.getNode().getFeature(ReturnChannelMap.class).registerChannel(arguments -> {
// Invoked when the client calls one of the client callables
String handlerName = arguments.getString(0);
String itemKey = arguments.getString(1);
JsonArray args = arguments.getArray(2);
SerializableBiConsumer<SOURCE, JsonArray> handler = clientCallables.get(handlerName);
SOURCE item = keyMapper.get(itemKey);
handler.accept(item, args);
});
JsonArray clientCallablesArray = JsonUtils.listToJson(new ArrayList<>(clientCallables.keySet()));
List<Registration> registrations = new ArrayList<>();
// Since the renderer is set manually on the client-side, an attach
// listener for the host component is required so that the renderer gets
// applied even when the host component gets a new Web Component
// instance (for example on detach + reattach).
//
// The attach listener needs to be released when the Renderer instance
// is no longer used so the registration is cleared by the renderer
// registration.
registrations.add(container.addAttachListener(e -> {
setElementRenderer(container, rendererName, templateExpression, returnChannel, clientCallablesArray, propertyNamespace);
}));
// Call once initially
setElementRenderer(container, rendererName, templateExpression, returnChannel, clientCallablesArray, propertyNamespace);
// Get the renderer function cleared when the LitRenderer is
// unregistered
registrations.add(() -> container.executeJs("window.Vaadin.unsetLitRenderer(this, $0, $1)", rendererName, propertyNamespace));
return () -> registrations.forEach(Registration::remove);
}
Aggregations