Search in sources :

Example 1 with StateTree

use of com.vaadin.client.flow.StateTree 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)

Example 2 with StateTree

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

the class RegistryTest method setAndGet.

@Test
public void setAndGet() {
    StateTree instance = new StateTree(registry);
    registry.set(StateTree.class, instance);
    Assert.assertSame(instance, registry.get(StateTree.class));
    Assert.assertSame(instance, registry.getStateTree());
}
Also used : StateTree(com.vaadin.client.flow.StateTree) Test(org.junit.Test)

Example 3 with StateTree

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

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

the class Poller method poll.

/**
 * Polls the server for changes.
 */
public void poll() {
    StateTree stateTree = registry.getStateTree();
    stateTree.sendEventToServer(stateTree.getRootNode(), PollEvent.DOM_EVENT_NAME, null);
}
Also used : StateTree(com.vaadin.client.flow.StateTree)

Example 5 with StateTree

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

the class GwtExecuteJavaScriptElementUtilsTest method testReturnChannel_passedToExecJavaScript_messageSentToServer.

public void testReturnChannel_passedToExecJavaScript_messageSentToServer() {
    ApplicationConfiguration applicationConfiguration = new ApplicationConfiguration();
    applicationConfiguration.setApplicationId("test");
    // Pass a number to the channel
    String expression = "$0(2)";
    int expectedNodeId = 10;
    int expectedChannelId = 20;
    int[] runCountHolder = { 0 };
    ExecuteJavaScriptProcessor processor = new ExecuteJavaScriptProcessor(new Registry() {

        {
            set(StateTree.class, new StateTree(this));
            set(ApplicationConfiguration.class, applicationConfiguration);
            set(ServerConnector.class, new ServerConnector(this) {

                @Override
                public void sendReturnChannelMessage(int stateNodeId, int channelId, JsonArray arguments) {
                    assertEquals(expectedNodeId, stateNodeId);
                    assertEquals(expectedChannelId, channelId);
                    assertEquals("Args array should contain the value passed to the channel function", "[2]", arguments.toJson());
                    runCountHolder[0]++;
                }
            });
        }
    });
    JsonArray serializedChannel = Json.createArray();
    serializedChannel.set(0, JsonCodec.RETURN_CHANNEL_TYPE);
    serializedChannel.set(1, expectedNodeId);
    serializedChannel.set(2, expectedChannelId);
    JsonArray invocation = Json.createArray();
    // Assign channel as $0
    invocation.set(0, serializedChannel);
    invocation.set(1, expression);
    JsonArray invocations = Json.createArray();
    invocations.set(0, invocation);
    processor.execute(invocations);
    assertEquals(1, runCountHolder[0]);
}
Also used : ServerConnector(com.vaadin.client.communication.ServerConnector) JsonArray(elemental.json.JsonArray) StateTree(com.vaadin.client.flow.StateTree) ExecuteJavaScriptProcessor(com.vaadin.client.flow.ExecuteJavaScriptProcessor)

Aggregations

StateTree (com.vaadin.client.flow.StateTree)12 StateNode (com.vaadin.client.flow.StateNode)8 Test (org.junit.Test)4 JsonArray (elemental.json.JsonArray)3 Registry (com.vaadin.client.Registry)2 ConstantPool (com.vaadin.client.flow.ConstantPool)2 JsElement (elemental.js.dom.JsElement)2 JsonObject (elemental.json.JsonObject)2 ApplicationConfiguration (com.vaadin.client.ApplicationConfiguration)1 CustomScheduler (com.vaadin.client.CustomScheduler)1 ExistingElementMap (com.vaadin.client.ExistingElementMap)1 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)1 UILifecycle (com.vaadin.client.UILifecycle)1 URIResolver (com.vaadin.client.URIResolver)1 ServerConnector (com.vaadin.client.communication.ServerConnector)1 ExecuteJavaScriptProcessor (com.vaadin.client.flow.ExecuteJavaScriptProcessor)1 JsArray (com.vaadin.client.flow.collection.JsArray)1 Node (elemental.dom.Node)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1