Search in sources :

Example 1 with ListFieldsRequest

use of io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.ListFieldsRequest in project deephaven-core by deephaven.

the class WorkerConnection method subscribeToFieldUpdates.

@JsMethod
@SuppressWarnings("ConstantConditions")
public JsRunnable subscribeToFieldUpdates(JsConsumer<JsVariableChanges> callback) {
    fieldUpdatesCallback.add(callback);
    if (fieldUpdatesCallback.size == 1) {
        fieldsChangeUpdateStream = ResponseStreamWrapper.of(applicationServiceClient.listFields(new ListFieldsRequest(), metadata));
        fieldsChangeUpdateStream.onData(data -> {
            final JsVariableDefinition[] created = new JsVariableDefinition[0];
            final JsVariableDefinition[] updated = new JsVariableDefinition[0];
            final JsVariableDefinition[] removed = new JsVariableDefinition[0];
            JsArray<FieldInfo> removedFI = data.getRemovedList();
            for (int i = 0; i < removedFI.length; ++i) {
                String removedId = removedFI.getAt(i).getTypedTicket().getTicket().getTicket_asB64();
                JsVariableDefinition result = knownFields.get(removedId);
                removed[removed.length] = result;
                knownFields.remove(removedId);
            }
            JsArray<FieldInfo> createdFI = data.getCreatedList();
            for (int i = 0; i < createdFI.length; ++i) {
                JsVariableDefinition result = new JsVariableDefinition(createdFI.getAt(i));
                created[created.length] = result;
                knownFields.put(result.getId(), result);
            }
            JsArray<FieldInfo> updatedFI = data.getUpdatedList();
            for (int i = 0; i < updatedFI.length; ++i) {
                JsVariableDefinition result = new JsVariableDefinition(updatedFI.getAt(i));
                updated[updated.length] = result;
                knownFields.put(result.getId(), result);
            }
            // Ensure that if a new subscription is in line to receive its initial update, we need to defer
            // the updates until after it receives its initial state.
            LazyPromise.runLater(() -> notifyFieldsChangeListeners(new JsVariableChanges(created, updated, removed)));
        });
        fieldsChangeUpdateStream.onEnd(this::checkStatus);
    } else {
        final JsVariableDefinition[] empty = new JsVariableDefinition[0];
        final JsVariableChanges update = new JsVariableChanges(knownFields.values().toArray(empty), empty, empty);
        LazyPromise.runLater(() -> {
            callback.apply(update);
        });
    }
    return () -> {
        fieldUpdatesCallback.delete(callback);
        if (fieldUpdatesCallback.size == 0) {
            knownFields.clear();
            if (fieldsChangeUpdateStream != null) {
                fieldsChangeUpdateStream.cancel();
                fieldsChangeUpdateStream = null;
            }
        }
    };
}
Also used : JsVariableDefinition(io.deephaven.web.client.api.console.JsVariableDefinition) ListFieldsRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.ListFieldsRequest) JsVariableChanges(io.deephaven.web.client.api.console.JsVariableChanges) FieldInfo(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.FieldInfo) JsMethod(jsinterop.annotations.JsMethod)

Aggregations

FieldInfo (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.FieldInfo)1 ListFieldsRequest (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.ListFieldsRequest)1 JsVariableChanges (io.deephaven.web.client.api.console.JsVariableChanges)1 JsVariableDefinition (io.deephaven.web.client.api.console.JsVariableDefinition)1 JsMethod (jsinterop.annotations.JsMethod)1