Search in sources :

Example 11 with PendingJavaScriptInvocation

use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.

the class Element method scheduleJavaScriptInvocation.

private PendingJavaScriptResult scheduleJavaScriptInvocation(String expression, Stream<Serializable> parameters) {
    StateNode node = getNode();
    JavaScriptInvocation invocation = new JavaScriptInvocation(expression, parameters.toArray(Serializable[]::new));
    PendingJavaScriptInvocation pending = new PendingJavaScriptInvocation(node, invocation);
    node.runWhenAttached(ui -> ui.getInternals().getStateTree().beforeClientResponse(node, context -> {
        if (!pending.isCanceled()) {
            context.getUI().getInternals().addJavaScriptInvocation(pending);
        }
    }));
    return pending;
}
Also used : IntStream(java.util.stream.IntStream) Component(com.vaadin.flow.component.Component) VirtualChildrenList(com.vaadin.flow.internal.nodefeature.VirtualChildrenList) ComponentUtil(com.vaadin.flow.component.ComponentUtil) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) Registration(com.vaadin.flow.shared.Registration) Json(elemental.json.Json) HashMap(java.util.HashMap) AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource) BasicTextElementStateProvider(com.vaadin.flow.dom.impl.BasicTextElementStateProvider) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) JsonValue(elemental.json.JsonValue) Locale(java.util.Locale) Map(java.util.Map) JsonCodec(com.vaadin.flow.internal.JsonCodec) Command(com.vaadin.flow.server.Command) Page(com.vaadin.flow.component.page.Page) TextNodeMap(com.vaadin.flow.internal.nodefeature.TextNodeMap) StateNode(com.vaadin.flow.internal.StateNode) BasicElementStateProvider(com.vaadin.flow.dom.impl.BasicElementStateProvider) Predicate(java.util.function.Predicate) Set(java.util.Set) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) CustomAttribute(com.vaadin.flow.dom.impl.CustomAttribute) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) List(java.util.List) JsonUtils(com.vaadin.flow.internal.JsonUtils) Stream(java.util.stream.Stream) ThemeListImpl(com.vaadin.flow.dom.impl.ThemeListImpl) Document(org.jsoup.nodes.Document) Optional(java.util.Optional) StreamResource(com.vaadin.flow.server.StreamResource) JavaScriptSemantics(com.vaadin.flow.internal.JavaScriptSemantics) Serializable(java.io.Serializable) StateNode(com.vaadin.flow.internal.StateNode) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation)

Example 12 with PendingJavaScriptInvocation

use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.

the class UidlWriter method createUidl.

/**
 * Creates a JSON object containing all pending changes to the given UI.
 *
 * @param ui
 *            The {@link UI} whose changes to write
 * @param async
 *            True if this message is sent by the server asynchronously,
 *            false if it is a response to a client message
 * @param resync
 *            True iff the client should be asked to resynchronize
 * @return JSON object containing the UIDL response
 */
public JsonObject createUidl(UI ui, boolean async, boolean resync) {
    JsonObject response = Json.createObject();
    UIInternals uiInternals = ui.getInternals();
    VaadinSession session = ui.getSession();
    VaadinService service = session.getService();
    // Purge pending access calls as they might produce additional changes
    // to write out
    service.runPendingAccessTasks(session);
    // Paints components
    getLogger().debug("* Creating response to client");
    int syncId = service.getDeploymentConfiguration().isSyncIdCheckEnabled() ? uiInternals.getServerSyncId() : -1;
    response.put(ApplicationConstants.SERVER_SYNC_ID, syncId);
    if (resync) {
        response.put(ApplicationConstants.RESYNCHRONIZE_ID, true);
    }
    int nextClientToServerMessageId = uiInternals.getLastProcessedClientToServerId() + 1;
    response.put(ApplicationConstants.CLIENT_TO_SERVER_ID, nextClientToServerMessageId);
    SystemMessages messages = service.getSystemMessages(ui.getLocale(), null);
    JsonObject meta = new MetadataWriter().createMetadata(ui, false, async, messages);
    if (meta.keys().length > 0) {
        response.put("meta", meta);
    }
    JsonArray stateChanges = Json.createArray();
    encodeChanges(ui, stateChanges);
    populateDependencies(response, uiInternals.getDependencyList(), new ResolveContext(service, session.getBrowser()));
    if (uiInternals.getConstantPool().hasNewConstants()) {
        response.put("constants", uiInternals.getConstantPool().dumpConstants());
    }
    if (stateChanges.length() != 0) {
        response.put("changes", stateChanges);
    }
    List<PendingJavaScriptInvocation> executeJavaScriptList = uiInternals.dumpPendingJavaScriptInvocations();
    if (!executeJavaScriptList.isEmpty()) {
        response.put(JsonConstants.UIDL_KEY_EXECUTE, encodeExecuteJavaScriptList(executeJavaScriptList));
    }
    if (service.getDeploymentConfiguration().isRequestTiming()) {
        response.put("timings", createPerformanceData(ui));
    }
    uiInternals.incrementServerId();
    return response;
}
Also used : JsonArray(elemental.json.JsonArray) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) JsonObject(elemental.json.JsonObject) UIInternals(com.vaadin.flow.component.internal.UIInternals) SystemMessages(com.vaadin.flow.server.SystemMessages) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation)

Aggregations

PendingJavaScriptInvocation (com.vaadin.flow.component.internal.PendingJavaScriptInvocation)12 Test (org.junit.Test)8 JavaScriptInvocation (com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)6 UI (com.vaadin.flow.component.UI)4 JsonArray (elemental.json.JsonArray)4 JsonObject (elemental.json.JsonObject)4 Component (com.vaadin.flow.component.Component)2 JsonUtils (com.vaadin.flow.internal.JsonUtils)2 BootstrapHandlerTest (com.vaadin.flow.server.BootstrapHandlerTest)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 MockUI (com.vaadin.tests.util.MockUI)2 Json (elemental.json.Json)2 List (java.util.List)2 Map (java.util.Map)2 ComponentUtil (com.vaadin.flow.component.ComponentUtil)1 Tag (com.vaadin.flow.component.Tag)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 Page (com.vaadin.flow.component.page.Page)1