From 051dbb7d13157b56b087564899f709e6cb142897 Mon Sep 17 00:00:00 2001 From: mikebender Date: Tue, 5 Dec 2023 19:57:12 -0500 Subject: [PATCH] feat: Add `getVariableDefinition` method - In cases of embedded widgets, we want to be able to just fetch the widget by just name, as we may not have type - We already have a utility method doing this in JS: https://github.com/deephaven/web-client-ui/blob/4c0200e71e350fcf5261b0cc28440cb798bec207/packages/jsapi-utils/src/ConnectionUtils.ts#L18 - Figure we should just expose the getVariableDefinition, since it's doing the exact same thing --- .../web/client/api/WorkerConnection.java | 17 ++++++---------- .../web/client/ide/IdeConnection.java | 20 +++++++++++++++++++ .../deephaven/web/client/ide/IdeSession.java | 5 +++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/web/client-api/src/main/java/io/deephaven/web/client/api/WorkerConnection.java b/web/client-api/src/main/java/io/deephaven/web/client/api/WorkerConnection.java index f9611a1bc57..52a636ce8a8 100644 --- a/web/client-api/src/main/java/io/deephaven/web/client/api/WorkerConnection.java +++ b/web/client-api/src/main/java/io/deephaven/web/client/api/WorkerConnection.java @@ -691,7 +691,7 @@ public void onError(Throwable throwable) { info.failureHandled(throwable.toString()); } - public Promise getVariableDefinition(String name, String type) { + public Promise getVariableDefinition(String name, @Nullable String type) { LazyPromise promise = new LazyPromise<>(); final class Listener implements Consumer { @@ -705,11 +705,11 @@ final class Listener implements Consumer { public void accept(JsVariableChanges changes) { JsVariableDefinition foundField = changes.getCreated() .find((field, p1, p2) -> field.getTitle().equals(name) - && field.getType().equalsIgnoreCase(type)); + && (type == null || field.getType().equalsIgnoreCase(type))); if (foundField == null) { foundField = changes.getUpdated().find((field, p1, p2) -> field.getTitle().equals(name) - && field.getType().equalsIgnoreCase(type)); + && (type == null || field.getType().equalsIgnoreCase(type))); } if (foundField != null) { @@ -789,20 +789,15 @@ public Promise getJsObject(JsPropertyMap definitionObject) { return getObject((JsVariableDefinition) definitionObject); } - if (!definitionObject.has("type")) { - throw new IllegalArgumentException("no type field; could not getObject"); - } - String type = definitionObject.getAsAny("type").asString(); - + String type = definitionObject.has("type") ? definitionObject.getAsAny("type").asString() : null; boolean hasName = definitionObject.has("name"); boolean hasId = definitionObject.has("id"); if (hasName && hasId) { throw new IllegalArgumentException("has both name and id field; could not getObject"); } else if (hasName) { String name = definitionObject.getAsAny("name").asString(); - return getVariableDefinition(name, type) - .then(this::getObject); - } else if (hasId) { + return getVariableDefinition(name, type).then(this::getObject); + } else if (hasId && type != null) { String id = definitionObject.getAsAny("id").asString(); return getObject(new JsVariableDefinition(type, null, id, null)); } else { diff --git a/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeConnection.java b/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeConnection.java index a32b290c613..1b7c13688ae 100644 --- a/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeConnection.java +++ b/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeConnection.java @@ -16,6 +16,7 @@ import io.deephaven.web.client.api.WorkerConnection; import io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper; import io.deephaven.web.client.api.console.JsVariableChanges; +import io.deephaven.web.client.api.console.JsVariableDefinition; import io.deephaven.web.client.api.console.JsVariableDescriptor; import io.deephaven.web.client.fu.JsLog; import io.deephaven.web.shared.data.ConnectToken; @@ -130,11 +131,30 @@ public Promise running() { } } + /** + * Gets an object from the server, given a definition object. The definition object you can get: + * - From {@link #getVariableDefinition(String)}, or + * - Returned from a {@link #subscribeToFieldUpdates(JsConsumer)} callback, or + * - Created manually, see {@link JsVariableDefinition} + * @param definitionObject A VariableDefinition object. See {@link JsVariableDefinition} + * @return Promise that will resolve to the object being fetched + */ public Promise getObject(@TsTypeRef(JsVariableDescriptor.class) JsPropertyMap definitionObject) { WorkerConnection conn = connection.get(); return onConnected().then(e -> conn.getJsObject(definitionObject)); } + /** + * Gets a variable definition from the server, given a name. Can be used to get the type of the variable, and then + * use {@link #getObject(JsPropertyMap)} to get the actual object by passing in the definition returned from this. + * @param name Name of the variable to get the definition for + * @return Promise that will resolve to the variable definition, or reject if it is not found. + */ + public Promise getVariableDefinition(String name) { + WorkerConnection conn = connection.get(); + return onConnected().then(e -> conn.getVariableDefinition(name, null)); + } + public JsRunnable subscribeToFieldUpdates(JsConsumer callback) { // Need to make sure the connection is initialized and connected WorkerConnection conn = connection.get(); diff --git a/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeSession.java b/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeSession.java index 6d8e1b69cb0..2b1af2a1a00 100644 --- a/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeSession.java +++ b/web/client-api/src/main/java/io/deephaven/web/client/ide/IdeSession.java @@ -16,6 +16,7 @@ import io.deephaven.web.client.api.barrage.stream.BiDiStream; import io.deephaven.web.client.api.console.JsCommandResult; import io.deephaven.web.client.api.console.JsVariableChanges; +import io.deephaven.web.client.api.console.JsVariableDefinition; import io.deephaven.web.client.api.console.JsVariableDescriptor; import io.deephaven.web.client.api.console.JsVariableType; import io.deephaven.web.client.api.tree.JsTreeTable; @@ -138,6 +139,10 @@ public Promise getHierarchicalTable(String name) { .then(connection::getTreeTable); } + public Promise getVariableDefinition(String name) { + return connection.getVariableDefinition(name, null); + } + public Promise getObject(@TsTypeRef(JsVariableDescriptor.class) JsPropertyMap definitionObject) { return connection.getJsObject(definitionObject); }