Skip to content

Commit

Permalink
feat: Add getVariableDefinition method
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mofojed committed Dec 6, 2023
1 parent 374188a commit 051dbb7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ public void onError(Throwable throwable) {
info.failureHandled(throwable.toString());
}

public Promise<JsVariableDefinition> getVariableDefinition(String name, String type) {
public Promise<JsVariableDefinition> getVariableDefinition(String name, @Nullable String type) {
LazyPromise<JsVariableDefinition> promise = new LazyPromise<>();

final class Listener implements Consumer<JsVariableChanges> {
Expand All @@ -705,11 +705,11 @@ final class Listener implements Consumer<JsVariableChanges> {
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) {
Expand Down Expand Up @@ -789,20 +789,15 @@ public Promise<?> getJsObject(JsPropertyMap<Object> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,11 +131,30 @@ public Promise<IdeConnection> 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<Object> 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<JsVariableDefinition> getVariableDefinition(String name) {
WorkerConnection conn = connection.get();
return onConnected().then(e -> conn.getVariableDefinition(name, null));
}

public JsRunnable subscribeToFieldUpdates(JsConsumer<JsVariableChanges> callback) {
// Need to make sure the connection is initialized and connected
WorkerConnection conn = connection.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,6 +139,10 @@ public Promise<JsTreeTable> getHierarchicalTable(String name) {
.then(connection::getTreeTable);
}

public Promise<JsVariableDefinition> getVariableDefinition(String name) {
return connection.getVariableDefinition(name, null);
}

public Promise<?> getObject(@TsTypeRef(JsVariableDescriptor.class) JsPropertyMap<Object> definitionObject) {
return connection.getJsObject(definitionObject);
}
Expand Down

0 comments on commit 051dbb7

Please sign in to comment.