use of io.deephaven.web.client.api.console.JsVariableChanges in project deephaven-core by deephaven.
the class IdeSession method runCode.
public CancellablePromise<JsCommandResult> runCode(String code) {
LazyPromise<JsCommandResult> promise = new LazyPromise<>();
ExecuteCommandRequest request = new ExecuteCommandRequest();
request.setConsoleId(this.result);
request.setCode(code);
Promise<ExecuteCommandResponse> runCodePromise = Callbacks.grpcUnaryPromise(c -> {
connection.consoleServiceClient().executeCommand(request, connection.metadata(), c::apply);
});
runCodePromise.then(response -> {
JsVariableChanges changes = JsVariableChanges.from(response.getChanges());
promise.succeed(new JsCommandResult(changes, response.getErrorMessage()));
return null;
}, err -> {
promise.fail(err);
return null;
});
CancellablePromise<JsCommandResult> result = promise.asPromise(() -> {
// connection.consoleServiceClient().cancelCommand(cancelRequest, connection.metadata());
throw new UnsupportedOperationException("cancelCommand");
});
CommandInfo commandInfo = new CommandInfo(code, result);
final CustomEventInit event = CustomEventInit.create();
event.setDetail(commandInfo);
fireEvent(IdeSession.EVENT_COMMANDSTARTED, event);
return result;
}
use of io.deephaven.web.client.api.console.JsVariableChanges 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;
}
}
};
}
use of io.deephaven.web.client.api.console.JsVariableChanges in project deephaven-core by deephaven.
the class IdeSession method getVariableDefinition.
private Promise<JsVariableDefinition> getVariableDefinition(String name, String type) {
LazyPromise<JsVariableDefinition> promise = new LazyPromise<>();
final class Listener implements Consumer<JsVariableChanges> {
final JsRunnable subscription;
Listener() {
subscription = subscribeToFieldUpdates(this::accept);
}
@Override
public void accept(JsVariableChanges changes) {
JsVariableDefinition foundField = changes.getCreated().find((field, p1, p2) -> field.getTitle().equals(name) && field.getType().equals(type));
if (foundField == null) {
foundField = changes.getUpdated().find((field, p1, p2) -> field.getTitle().equals(name) && field.getType().equals(type));
}
if (foundField != null) {
subscription.run();
promise.succeed(foundField);
}
}
}
Listener listener = new Listener();
return promise.timeout(10_000).asPromise().then(Promise::resolve, fail -> {
listener.subscription.run();
// noinspection unchecked, rawtypes
return (Promise<JsVariableDefinition>) (Promise) Promise.reject(fail);
});
}
Aggregations