Java Code Examples for elemental.json.JsonObject#getNumber()

The following examples show how to use elemental.json.JsonObject#getNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TreeChangeProcessor.java    From flow with Apache License 2.0 6 votes vote down vote up
private static JsSet<StateNode> processAttachChanges(StateTree tree,
        JsonArray changes) {
    JsSet<StateNode> nodes = JsCollections.set();
    int length = changes.length();
    for (int i = 0; i < length; i++) {
        JsonObject change = changes.getObject(i);
        if (isAttach(change)) {
            int nodeId = (int) change.getNumber(JsonConstants.CHANGE_NODE);

            if (nodeId != tree.getRootNode().getId()) {
                StateNode node = new StateNode(nodeId, tree);
                tree.registerNode(node);
                nodes.add(node);
            }
        }
    }
    return nodes;
}
 
Example 2
Source File: MapSyncRpcHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private Serializable tryConvert(Serializable value, StateNode context) {
    if (value instanceof JsonObject) {
        JsonObject json = (JsonObject) value;
        if (json.hasKey("nodeId")) {
            StateTree tree = (StateTree) context.getOwner();
            double id = json.getNumber("nodeId");
            StateNode stateNode = tree.getNodeById((int) id);
            return tryCopyStateNode(stateNode, json);
        }
    }
    return value;
}
 
Example 3
Source File: ReturnChannelHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
protected Optional<Runnable> handleNode(StateNode node,
        JsonObject invocationJson) {
    int channelId = (int) invocationJson
            .getNumber(JsonConstants.RPC_CHANNEL);
    JsonArray arguments = invocationJson
            .getArray(JsonConstants.RPC_CHANNEL_ARGUMENTS);

    if (!node.hasFeature(ReturnChannelMap.class)) {
        getLogger().warn("Node has no return channels: {}", invocationJson);
        return Optional.empty();
    }

    ReturnChannelRegistration channel = node
            .getFeatureIfInitialized(ReturnChannelMap.class)
            .map(map -> map.get(channelId)).orElse(null);

    if (channel == null) {
        getLogger().warn("Return channel not found: {}", invocationJson);
        return Optional.empty();
    }

    if (!node.isEnabled() && channel
            .getDisabledUpdateMode() != DisabledUpdateMode.ALWAYS) {
        getLogger().warn("Ignoring update for disabled return channel: {}",
                invocationJson);
        return Optional.empty();
    }

    channel.invoke(arguments);

    return Optional.empty();
}
 
Example 4
Source File: TreeChangeProcessor.java    From flow with Apache License 2.0 5 votes vote down vote up
private static MapProperty findProperty(JsonObject change, StateNode node) {
    int nsId = (int) change.getNumber(JsonConstants.CHANGE_FEATURE);
    NodeMap map = node.getMap(nsId);
    String key = change.getString(JsonConstants.CHANGE_MAP_KEY);

    return map.getProperty(key);
}
 
Example 5
Source File: ScrollPositionHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private void readAndRestoreScrollPositionsFromHistoryAndSessionStorage(
        boolean delayAfterResponse) {
    // restore history index & token from state if applicable
    JsonObject state = (JsonObject) Browser.getWindow().getHistory()
            .getState();
    if (state != null && state.hasKey(HISTORY_INDEX)
            && state.hasKey(HISTORY_TOKEN)) {

        currentHistoryIndex = (int) state.getNumber(HISTORY_INDEX);
        historyResetToken = state.getNumber(HISTORY_TOKEN);

        String jsonString = null;
        try {
            jsonString = Browser.getWindow().getSessionStorage()
                    .getItem(createSessionStorageKey(historyResetToken));
        } catch (JavaScriptException e) {
            Console.error(
                    "Failed to get session storage: " + e.getMessage());
        }
        if (jsonString != null) {
            JsonObject jsonObject = Json.parse(jsonString);

            xPositions = convertJsonArrayToArray(
                    jsonObject.getArray(X_POSITIONS));
            yPositions = convertJsonArrayToArray(
                    jsonObject.getArray(Y_POSITIONS));
            // array lengths checked in restoreScrollPosition
            restoreScrollPosition(delayAfterResponse);
        } else {
            Console.warn(
                    "History.state has scroll history index, but no scroll positions found from session storage matching token <"
                            + historyResetToken
                            + ">. User has navigated out of site in an unrecognized way.");
            resetScrollPositionTracking();
        }
    } else {
        resetScrollPositionTracking();
    }
}
 
Example 6
Source File: ScrollPositionHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Store scroll positions and restore scroll positions depending on the
 * given pop state event.
 * <p>
 * This method behaves differently if there has been a
 * {@link #beforeClientNavigation(String)} before this, and if the pop state
 * event is caused by a fragment change that doesn't require a server side
 * round-trip.
 *
 * @param event
 *            the pop state event
 * @param triggersServerSideRoundtrip
 *            <code>true</code> if the pop state event triggers a server
 *            side request, <code>false</code> if not
 */
public void onPopStateEvent(PopStateEvent event,
        boolean triggersServerSideRoundtrip) {
    if (ignoreScrollRestorationOnNextPopStateEvent) {
        Browser.getWindow().getHistory().replaceState(
                createStateObjectWithHistoryIndexAndToken(), "",
                Browser.getDocument().getLocation().getHref());

        ignoreScrollRestorationOnNextPopStateEvent = false;
        return;
    }

    captureCurrentScrollPositions();

    JsonObject state = (JsonObject) event.getState();
    if (state == null || !state.hasKey(HISTORY_INDEX)
            || !state.hasKey(HISTORY_TOKEN)) {
        // state doesn't contain history index info, just log & reset
        Console.warn(MISSING_STATE_VARIABLES_MESSAGE);
        resetScrollPositionTracking();
        return;
    }

    double token = state.getNumber(HISTORY_TOKEN);
    if (!Objects.equals(token, historyResetToken)) {
        // current scroll positions are not for this history entry
        // try to restore arrays or reset
        readAndRestoreScrollPositionsFromHistoryAndSessionStorage(
                triggersServerSideRoundtrip);
        return;
    }

    currentHistoryIndex = (int) state.getNumber(HISTORY_INDEX);
    restoreScrollPosition(triggersServerSideRoundtrip);
}
 
Example 7
Source File: AbstractRpcInvocationHandler.java    From flow with Apache License 2.0 4 votes vote down vote up
private static int getNodeId(JsonObject invocationJson) {
    return (int) invocationJson.getNumber(JsonConstants.RPC_NODE);
}
 
Example 8
Source File: RpcLoggerServlet.java    From flow with Apache License 2.0 4 votes vote down vote up
private static int getNodeId(JsonObject invocationJson) {
    return (int) invocationJson.getNumber(JsonConstants.RPC_NODE);
}
 
Example 9
Source File: TreeChangeProcessor.java    From flow with Apache License 2.0 4 votes vote down vote up
private static void processClearChange(JsonObject change, StateNode node) {
    int nsId = (int) change.getNumber(JsonConstants.CHANGE_FEATURE);
    NodeList list = node.getList(nsId);
    list.clear();
}