Search in sources :

Example 1 with ConstantPool

use of com.vaadin.client.flow.ConstantPool 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, Date start) {
    assert getServerId(valueMap) == -1 || getServerId(valueMap) == lastSeenServerSyncId;
    try {
        double processUidlStart = Duration.currentTimeMillis();
        JsonObject json = valueMap.cast();
        if (json.hasKey("templates")) {
            TemplateRegistry templates = registry.getTemplateRegistry();
            JsonObject templatesJson = json.getObject("templates");
            templates.importFromJson(templatesJson);
        }
        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
            Reactive.addPostFlushListener(() -> registry.getExecuteJavaScriptProcessor().execute(json.getArray(JsonConstants.UIDL_KEY_EXECUTE)));
        }
        Console.log("handleUIDLMessage: " + (Duration.currentTimeMillis() - processUidlStart) + " ms");
        ValueMap meta = valueMap.getValueMap("meta");
        if (meta != null) {
            Profiler.enter("Error handling");
            if (meta.containsKey(JsonConstants.META_SESSION_EXPIRED)) {
                if (nextResponseSessionExpiredHandler != null) {
                    nextResponseSessionExpiredHandler.execute();
                } else {
                    registry.getSystemErrorHandler().handleSessionExpiredError(null);
                    registry.getUILifecycle().setState(UIState.TERMINATED);
                }
            } else if (meta.containsKey("appError")) {
                ValueMap error = meta.getValueMap("appError");
                registry.getSystemErrorHandler().handleUnrecoverableError(error.getString("caption"), error.getString("message"), error.getString("details"), error.getString("url"));
                registry.getUILifecycle().setState(UIState.TERMINATED);
            }
            Profiler.leave("Error handling");
        }
        nextResponseSessionExpiredHandler = null;
        Reactive.flush();
        lastProcessingTime = (int) ((new Date().getTime()) - start.getTime());
        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 : TemplateRegistry(com.vaadin.client.flow.template.TemplateRegistry) ConstantPool(com.vaadin.client.flow.ConstantPool) ValueMap(com.vaadin.client.ValueMap) JsonObject(elemental.json.JsonObject) Date(java.util.Date)

Example 2 with ConstantPool

use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.

the class GwtTemplateBinderTest method setModelType.

private static void setModelType(StateNode node, JsonObject modelTypeJson) {
    String constantId = "modelType " + modelTypeCount++;
    JsonObject constantsJson = Json.createObject();
    constantsJson.put(constantId, modelTypeJson);
    ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
    constantPool.importFromJson(constantsJson);
    node.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.MODEL_DESCRIPTOR).setValue(constantId);
}
Also used : ConstantPool(com.vaadin.client.flow.ConstantPool) JsonObject(elemental.json.JsonObject)

Example 3 with ConstantPool

use of com.vaadin.client.flow.ConstantPool 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 4 with ConstantPool

use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.

the class SimpleElementBindingStrategy method handleDomEvent.

private void handleDomEvent(Event event, Node element, StateNode node) {
    assert element instanceof Element : "Cannot handle DOM event for a Node";
    String type = event.getType();
    NodeMap listenerMap = getDomEventListenerMap(node);
    ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
    String expressionConstantKey = (String) listenerMap.getProperty(type).getValue();
    assert expressionConstantKey != null;
    assert constantPool.has(expressionConstantKey);
    JsArray<String> dataExpressions = constantPool.get(expressionConstantKey);
    JsonObject eventData;
    if (dataExpressions.isEmpty()) {
        eventData = null;
    } else {
        eventData = Json.createObject();
        for (int i = 0; i < dataExpressions.length(); i++) {
            String expressionString = dataExpressions.get(i);
            EventDataExpression expression = getOrCreateExpression(expressionString);
            JsonValue expressionValue = expression.evaluate(event, (Element) element);
            eventData.put(expressionString, expressionValue);
        }
    }
    node.getTree().sendEventToServer(node, type, eventData);
}
Also used : ConstantPool(com.vaadin.client.flow.ConstantPool) DomElement(com.vaadin.client.flow.dom.DomElement) Element(elemental.dom.Element) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 5 with ConstantPool

use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.

the class GwtTemplateBinderTest method gwtSetUp.

@Override
protected void gwtSetUp() throws Exception {
    super.gwtSetUp();
    registry = new Registry() {

        {
            set(TemplateRegistry.class, new TemplateRegistry());
            set(ConstantPool.class, new ConstantPool());
            set(ExistingElementMap.class, new ExistingElementMap());
            set(InitialPropertiesHandler.class, new InitialPropertiesHandler(this));
            set(StateTree.class, new StateTree(this) {

                @Override
                public void sendTemplateEventToServer(StateNode node, String methodName, JsArray<?> argValues) {
                    serverMethods.put(methodName, argValues);
                    serverRpcNodes.put(methodName, node);
                }
            });
        }
    };
    tree = registry.getStateTree();
    /**
     * This state node is ALWAYS a template !!!
     */
    stateNode = new StateNode(0, tree) {

        @Override
        public boolean hasFeature(int id) {
            if (id == NodeFeatures.TEMPLATE) {
                return true;
            }
            return super.hasFeature(id);
        }
    };
    // Use the most common model structure by default
    JsonObject modelType = Json.createObject();
    modelType.put(MODEL_KEY, "String");
    setModelType(stateNode, modelType);
}
Also used : InitialPropertiesHandler(com.vaadin.client.InitialPropertiesHandler) StateTree(com.vaadin.client.flow.StateTree) JsArray(com.vaadin.client.flow.collection.JsArray) ConstantPool(com.vaadin.client.flow.ConstantPool) StateNode(com.vaadin.client.flow.StateNode) ExistingElementMap(com.vaadin.client.ExistingElementMap) JsonObject(elemental.json.JsonObject) Registry(com.vaadin.client.Registry)

Aggregations

ConstantPool (com.vaadin.client.flow.ConstantPool)8 JsonObject (elemental.json.JsonObject)6 StateTree (com.vaadin.client.flow.StateTree)3 ApplicationConfiguration (com.vaadin.client.ApplicationConfiguration)2 ExistingElementMap (com.vaadin.client.ExistingElementMap)2 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)2 Registry (com.vaadin.client.Registry)2 ValueMap (com.vaadin.client.ValueMap)2 StateNode (com.vaadin.client.flow.StateNode)2 JsArray (com.vaadin.client.flow.collection.JsArray)2 DomElement (com.vaadin.client.flow.dom.DomElement)2 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)2 Element (elemental.dom.Element)2 JsonArray (elemental.json.JsonArray)2 JsonValue (elemental.json.JsonValue)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 Scheduler (com.google.gwt.core.client.Scheduler)1 Command (com.vaadin.client.Command)1 Console (com.vaadin.client.Console)1 CustomScheduler (com.vaadin.client.CustomScheduler)1