Search in sources :

Example 26 with StateNode

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

the class PolymerUtils method registerChangeHandlers.

private static void registerChangeHandlers(StateNode node, NodeFeature feature, JsonValue value) {
    JsArray<EventRemover> registrations = JsCollections.array();
    if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
        assert feature instanceof NodeMap : "Received an inconsistent NodeFeature for a node that has a ELEMENT_PROPERTIES feature. It should be NodeMap, but it is: " + feature;
        NodeMap map = (NodeMap) feature;
        registerPropertyChangeHandlers(value, registrations, map);
        registerPropertyAddHandler(value, registrations, map);
    } else if (node.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
        assert feature instanceof NodeList : "Received an inconsistent NodeFeature for a node that has a TEMPLATE_MODELLIST feature. It should be NodeList, but it is: " + feature;
        NodeList list = (NodeList) feature;
        registrations.push(list.addSpliceListener(event -> handleListChange(event, value)));
    }
    assert !registrations.isEmpty() : "Node should have ELEMENT_PROPERTIES or TEMPLATE_MODELLIST feature";
    registrations.push(node.addUnregisterListener(event -> registrations.forEach(EventRemover::remove)));
}
Also used : ShadowRoot(elemental.dom.ShadowRoot) JsWeakMap(com.vaadin.client.flow.collection.JsWeakMap) Reactive(com.vaadin.client.flow.reactive.Reactive) DomApi(com.vaadin.client.flow.dom.DomApi) Json(elemental.json.Json) NodeProperties(com.vaadin.flow.internal.nodefeature.NodeProperties) JsonArray(elemental.json.JsonArray) ListSpliceEvent(com.vaadin.client.flow.nodefeature.ListSpliceEvent) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) NodeFeatures(com.vaadin.flow.internal.nodefeature.NodeFeatures) StateNode(com.vaadin.client.flow.StateNode) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) HTMLCollection(elemental.html.HTMLCollection) JsonValue(elemental.json.JsonValue) NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) NodeList(com.vaadin.client.flow.nodefeature.NodeList) EventRemover(elemental.events.EventRemover) JsSet(com.vaadin.client.flow.collection.JsSet) Node(elemental.dom.Node) JsCollections(com.vaadin.client.flow.collection.JsCollections) Element(elemental.dom.Element) JsonObject(elemental.json.JsonObject) JsArray(com.vaadin.client.flow.collection.JsArray) EventRemover(elemental.events.EventRemover) NodeList(com.vaadin.client.flow.nodefeature.NodeList) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 27 with StateNode

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

the class PushConfiguration method getParameters.

/**
 * Gets all configured push parameters.
 *
 * The parameters configured on the server, including transports.
 *
 * @return a map of all parameters configured on the server
 */
public JsMap<String, String> getParameters() {
    MapProperty p = getConfigurationMap().getProperty(PushConfigurationMap.PARAMETERS_KEY);
    StateNode parametersNode = (StateNode) p.getValue();
    NodeMap parametersMap = parametersNode.getMap(NodeFeatures.UI_PUSHCONFIGURATION_PARAMETERS);
    JsMap<String, String> parameters = JsCollections.map();
    parametersMap.forEachProperty((property, key) -> {
        parameters.set(key, (String) property.getValue());
    });
    return parameters;
}
Also used : MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 28 with StateNode

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

the class MessageHandler method processChanges.

private void processChanges(JsonObject json) {
    StateTree tree = registry.getStateTree();
    JsSet<StateNode> updatedNodes = TreeChangeProcessor.processChanges(tree, json.getArray("changes"));
    if (!registry.getApplicationConfiguration().isProductionMode()) {
        try {
            JsonObject debugJson = tree.getRootNode().getDebugJson();
            Console.log("StateTree after applying changes:");
            Console.log(debugJson);
        } catch (Exception e) {
            Console.error("Failed to log state tree");
            Console.error(e);
        }
    }
    Reactive.addPostFlushListener(() -> Scheduler.get().scheduleDeferred(() -> updatedNodes.forEach(this::afterServerUpdates)));
}
Also used : StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode) JsonObject(elemental.json.JsonObject)

Example 29 with StateNode

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

the class InitialPropertiesHandlerTest method flushPropertyUpdates_updateIsNotInProgress_flushForEechProperty.

@Test
public void flushPropertyUpdates_updateIsNotInProgress_flushForEechProperty() {
    Mockito.when(tree.isUpdateInProgress()).thenReturn(false);
    StateNode node = new StateNode(1, tree);
    NodeMap properties = node.getMap(NodeFeatures.ELEMENT_PROPERTIES);
    MapProperty property1 = properties.getProperty("foo");
    property1.setValue("bar");
    MapProperty property2 = properties.getProperty("other");
    property2.setValue("value");
    handler.nodeRegistered(node);
    Mockito.when(tree.getNode(node.getId())).thenReturn(node);
    handler.flushPropertyUpdates();
    property1.setValue("baz");
    property2.setValue("foo");
    handler.handlePropertyUpdate(property1);
    handler.handlePropertyUpdate(property2);
    AtomicInteger count = new AtomicInteger();
    FlushListener listener = () -> count.incrementAndGet();
    property1.addChangeListener(event -> Reactive.addFlushListener(listener));
    property2.addChangeListener(event -> Reactive.addFlushListener(listener));
    Reactive.flush();
    Assert.assertEquals(2, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlushListener(com.vaadin.client.flow.reactive.FlushListener) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) Test(org.junit.Test)

Example 30 with StateNode

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

the class ClientJsonCodecTest method decodeWithTypeInfo_element.

@Test
public void decodeWithTypeInfo_element() {
    StateTree tree = new StateTree(null);
    StateNode node = new StateNode(42, tree);
    tree.registerNode(node);
    JsElement element = new JsElement() {
    };
    node.setDomNode(element);
    JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId()));
    Object decoded = ClientJsonCodec.decodeWithTypeInfo(tree, json);
    Assert.assertSame(element, decoded);
}
Also used : JsonArray(elemental.json.JsonArray) StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode) JsElement(elemental.js.dom.JsElement) Test(org.junit.Test)

Aggregations

StateNode (com.vaadin.client.flow.StateNode)73 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)21 Element (elemental.dom.Element)21 Test (org.junit.Test)16 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)14 Node (elemental.dom.Node)13 JsonObject (elemental.json.JsonObject)11 StateTree (com.vaadin.client.flow.StateTree)9 DomNode (com.vaadin.client.flow.dom.DomNode)9 NodeList (com.vaadin.client.flow.nodefeature.NodeList)7 JsonValue (elemental.json.JsonValue)5 ExistingElementMap (com.vaadin.client.ExistingElementMap)4 JsArray (com.vaadin.client.flow.collection.JsArray)4 NativeFunction (com.vaadin.client.flow.util.NativeFunction)4 JsonArray (elemental.json.JsonArray)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)3 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)2 Command (com.vaadin.client.Command)2 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)2