Search in sources :

Example 1 with RpcInvocationHandler

use of com.vaadin.flow.server.communication.rpc.RpcInvocationHandler in project flow by vaadin.

the class ServerRpcHandler method handleInvocations.

/**
 * Processes invocations data received from the client.
 * <p>
 * The invocations data can contain any number of RPC calls.
 *
 * @param ui
 *            the UI receiving the invocations data
 * @param invocationsData
 *            JSON containing all information needed to execute all
 *            requested RPC calls.
 */
private void handleInvocations(UI ui, JsonArray invocationsData) {
    List<JsonObject> data = new ArrayList<>(invocationsData.length());
    List<Runnable> pendingChangeEvents = new ArrayList<>();
    RpcInvocationHandler mapSyncHandler = getInvocationHandlers().get(JsonConstants.RPC_TYPE_MAP_SYNC);
    for (int i = 0; i < invocationsData.length(); i++) {
        JsonObject invocationJson = invocationsData.getObject(i);
        String type = invocationJson.getString(JsonConstants.RPC_TYPE);
        assert type != null;
        if (JsonConstants.RPC_TYPE_MAP_SYNC.equals(type)) {
            // Handle these before any RPC invocations.
            mapSyncHandler.handle(ui, invocationJson).ifPresent(pendingChangeEvents::add);
        } else {
            data.add(invocationJson);
        }
    }
    pendingChangeEvents.forEach(runnable -> runMapSyncTask(ui, runnable));
    data.forEach(json -> handleInvocationData(ui, json));
}
Also used : RpcInvocationHandler(com.vaadin.flow.server.communication.rpc.RpcInvocationHandler) ArrayList(java.util.ArrayList) JsonObject(elemental.json.JsonObject)

Example 2 with RpcInvocationHandler

use of com.vaadin.flow.server.communication.rpc.RpcInvocationHandler in project flow by vaadin.

the class ServerRpcHandler method handleInvocationData.

private void handleInvocationData(UI ui, JsonObject invocationJson) {
    String type = invocationJson.getString(JsonConstants.RPC_TYPE);
    RpcInvocationHandler handler = getInvocationHandlers().get(type);
    if (handler == null) {
        throw new IllegalArgumentException("Unsupported event type: " + type);
    }
    try {
        Optional<Runnable> handle = handler.handle(ui, invocationJson);
        assert !handle.isPresent() : "RPC handler " + handler.getClass().getName() + " returned a Runnable even though it shouldn't";
    } catch (Throwable throwable) {
        ui.getSession().getErrorHandler().error(new ErrorEvent(throwable));
    }
}
Also used : RpcInvocationHandler(com.vaadin.flow.server.communication.rpc.RpcInvocationHandler) ErrorEvent(com.vaadin.flow.server.ErrorEvent)

Aggregations

RpcInvocationHandler (com.vaadin.flow.server.communication.rpc.RpcInvocationHandler)2 ErrorEvent (com.vaadin.flow.server.ErrorEvent)1 JsonObject (elemental.json.JsonObject)1 ArrayList (java.util.ArrayList)1