Search in sources :

Example 1 with TemplateRegistry

use of com.vaadin.client.flow.template.TemplateRegistry 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 TemplateRegistry

use of com.vaadin.client.flow.template.TemplateRegistry in project flow by vaadin.

the class GwtBasicElementBinderTest method testAddTemplateChild.

public void testAddTemplateChild() {
    final int templateId = 43;
    TestElementTemplateNode templateNode = TestElementTemplateNode.create("child");
    TemplateRegistry templates = new TemplateRegistry();
    templates.register(templateId, templateNode);
    Registry registry = new Registry() {

        {
            set(TemplateRegistry.class, templates);
            set(ExistingElementMap.class, new ExistingElementMap());
        }
    };
    StateTree stateTree = new StateTree(registry);
    StateNode templateStateNode = new StateNode(345, stateTree);
    templateStateNode.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.ROOT_TEMPLATE_ID).setValue(Double.valueOf(templateId));
    StateNode parentElementNode = new StateNode(94, stateTree);
    parentElementNode.getMap(NodeFeatures.ELEMENT_DATA).getProperty(NodeProperties.TAG).setValue("div");
    parentElementNode.getList(NodeFeatures.ELEMENT_CHILDREN).add(0, templateStateNode);
    Element element = Browser.getDocument().createElement("div");
    Binder.bind(parentElementNode, element);
    Reactive.flush();
    assertEquals(1, element.getChildElementCount());
    assertEquals("CHILD", element.getFirstElementChild().getTagName());
}
Also used : TemplateRegistry(com.vaadin.client.flow.template.TemplateRegistry) TestElementTemplateNode(com.vaadin.client.flow.template.TestElementTemplateNode) Element(elemental.dom.Element) ExistingElementMap(com.vaadin.client.ExistingElementMap) Registry(com.vaadin.client.Registry) TemplateRegistry(com.vaadin.client.flow.template.TemplateRegistry)

Aggregations

TemplateRegistry (com.vaadin.client.flow.template.TemplateRegistry)2 ExistingElementMap (com.vaadin.client.ExistingElementMap)1 Registry (com.vaadin.client.Registry)1 ValueMap (com.vaadin.client.ValueMap)1 ConstantPool (com.vaadin.client.flow.ConstantPool)1 TestElementTemplateNode (com.vaadin.client.flow.template.TestElementTemplateNode)1 Element (elemental.dom.Element)1 JsonObject (elemental.json.JsonObject)1 Date (java.util.Date)1