Search in sources :

Example 1 with UIState

use of com.vaadin.client.UILifecycle.UIState in project flow by vaadin.

the class MessageHandler method processMessage.

/**
 * Performs the actual processing of a server message when all dependencies
 * have been loaded.
 *
 * @param valueMap
 *            the message payload
 * @param lock
 *            the lock object for this response
 * @param start
 *            the time stamp when processing started
 */
private void processMessage(ValueMap valueMap, Object lock, double start) {
    assert getServerId(valueMap) == -1 || getServerId(valueMap) == lastSeenServerSyncId;
    try {
        double processUidlStart = Duration.currentTimeMillis();
        JsonObject json = valueMap.cast();
        if (json.hasKey("constants")) {
            ConstantPool constantPool = registry.getConstantPool();
            JsonObject constants = json.getObject("constants");
            constantPool.importFromJson(constants);
        }
        if (json.hasKey("changes")) {
            processChanges(json);
        }
        if (json.hasKey(JsonConstants.UIDL_KEY_EXECUTE)) {
            // Invoke JS only after all tree changes have been
            // propagated and after post flush listeners added during
            // message processing (so add one more post flush listener which
            // is called after all added post listeners).
            Reactive.addPostFlushListener(() -> Reactive.addPostFlushListener(() -> registry.getExecuteJavaScriptProcessor().execute(json.getArray(JsonConstants.UIDL_KEY_EXECUTE))));
        }
        Console.log("handleUIDLMessage: " + (Duration.currentTimeMillis() - processUidlStart) + " ms");
        Reactive.flush();
        ValueMap meta = valueMap.getValueMap("meta");
        if (meta != null) {
            Profiler.enter("Error handling");
            final UIState uiState = registry.getUILifecycle().getState();
            if (meta.containsKey(JsonConstants.META_SESSION_EXPIRED)) {
                if (nextResponseSessionExpiredHandler != null) {
                    nextResponseSessionExpiredHandler.execute();
                } else if (uiState != UIState.TERMINATED) {
                    registry.getSystemErrorHandler().handleSessionExpiredError(null);
                    registry.getUILifecycle().setState(UIState.TERMINATED);
                }
            } else if (meta.containsKey("appError") && uiState != UIState.TERMINATED) {
                ValueMap error = meta.getValueMap("appError");
                registry.getSystemErrorHandler().handleUnrecoverableError(error.getString("caption"), error.getString("message"), error.getString("details"), error.getString("url"), error.getString("querySelector"));
                registry.getUILifecycle().setState(UIState.TERMINATED);
            }
            Profiler.leave("Error handling");
        }
        nextResponseSessionExpiredHandler = null;
        lastProcessingTime = (int) (Duration.currentTimeMillis() - start);
        totalProcessingTime += lastProcessingTime;
        if (!initialMessageHandled) {
            initialMessageHandled = true;
            double fetchStart = getFetchStartTime();
            if (fetchStart != 0) {
                int time = (int) (Duration.currentTimeMillis() - fetchStart);
                Console.log("First response processed " + time + " ms after fetchStart");
            }
            bootstrapTime = calculateBootstrapTime();
            if (Profiler.isEnabled() && bootstrapTime != -1) {
                Profiler.logBootstrapTimings();
            }
        }
    } finally {
        Console.log(" Processing time was " + String.valueOf(lastProcessingTime) + "ms");
        endRequestIfResponse(valueMap);
        resumeResponseHandling(lock);
        if (Profiler.isEnabled()) {
            Scheduler.get().scheduleDeferred(() -> {
                Profiler.logTimings();
                Profiler.reset();
            });
        }
    }
}
Also used : ConstantPool(com.vaadin.client.flow.ConstantPool) UIState(com.vaadin.client.UILifecycle.UIState) ValueMap(com.vaadin.client.ValueMap) JsonObject(elemental.json.JsonObject)

Example 2 with UIState

use of com.vaadin.client.UILifecycle.UIState in project flow by vaadin.

the class MessageHandler method handleMessage.

/**
 * Handles a received UIDL JSON text, parsing it, and passing it on to the
 * appropriate handlers, while logging timing information.
 *
 * @param json
 *            The JSON to handle
 */
public void handleMessage(final ValueMap json) {
    if (json == null) {
        throw new IllegalArgumentException("The json to handle cannot be null");
    }
    if (getServerId(json) == -1) {
        ValueMap meta = json.getValueMap("meta");
        // Log the error only if session didn't expire.
        if (meta == null || !meta.containsKey(JsonConstants.META_SESSION_EXPIRED)) {
            Console.error("Response didn't contain a server id. " + "Please verify that the server is up-to-date and that the response data has not been modified in transmission.");
        }
    }
    UIState state = registry.getUILifecycle().getState();
    if (state == UIState.INITIALIZING) {
        // Application is starting up for the first time
        state = UIState.RUNNING;
        registry.getUILifecycle().setState(state);
    }
    if (state == UIState.RUNNING) {
        handleJSON(json);
    } else {
        Console.warn("Ignored received message because application has already been stopped");
    }
}
Also used : UIState(com.vaadin.client.UILifecycle.UIState) ValueMap(com.vaadin.client.ValueMap)

Aggregations

UIState (com.vaadin.client.UILifecycle.UIState)2 ValueMap (com.vaadin.client.ValueMap)2 ConstantPool (com.vaadin.client.flow.ConstantPool)1 JsonObject (elemental.json.JsonObject)1