Skip to content

Commit

Permalink
Avoid re-exporting tickets from exports
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Dec 12, 2023
1 parent 95e99fd commit 5fabcc7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -776,14 +776,7 @@ public Promise<?> getObject(JsVariableDefinition definition) {
} else if (JsVariableType.HIERARCHICALTABLE.equalsIgnoreCase(definition.getType())) {
return getHierarchicalTable(definition);
} else {
if (JsVariableType.TABLEMAP.equalsIgnoreCase(definition.getType())) {
JsLog.warn(
"TableMap is now known as PartitionedTable, fetching as a plain widget. To fetch as a PartitionedTable use that as the type.");
}
if (JsVariableType.TREETABLE.equalsIgnoreCase(definition.getType())) {
JsLog.warn(
"TreeTable is now HierarchicalTable, fetching as a plain widget. To fetch as a HierarchicalTable use that as this type.");
}
warnLegacyTicketTypes(definition.getType());
return getWidget(definition).then(JsWidget::refetch);
}
}
Expand Down Expand Up @@ -814,6 +807,45 @@ public Promise<?> getJsObject(JsPropertyMap<Object> definitionObject) {
}
}

public Promise<?> getObject(TypedTicket typedTicket) {
if (JsVariableType.TABLE.equalsIgnoreCase(typedTicket.getType())) {
throw new IllegalArgumentException("wrong way to get a table from a ticket");
} else if (JsVariableType.FIGURE.equalsIgnoreCase(typedTicket.getType())) {
return new JsFigure(this, c -> {
JsWidget widget = new JsWidget(this, typedTicket);
widget.refetch().then(ignore -> {
c.apply(null, makeFigureFetchResponse(widget));
return null;
});
}).refetch();
} else if (JsVariableType.PANDAS.equalsIgnoreCase(typedTicket.getType())) {
return getWidget(typedTicket)
.then(JsWidget::refetch)
.then(widget -> {
widget.close();
return widget.getExportedObjects()[0].fetch();
});
} else if (JsVariableType.PARTITIONEDTABLE.equalsIgnoreCase(typedTicket.getType())) {
return new JsPartitionedTable(this, new JsWidget(this, typedTicket)).refetch();
} else if (JsVariableType.HIERARCHICALTABLE.equalsIgnoreCase(typedTicket.getType())) {
return new JsWidget(this, typedTicket).refetch().then(w -> Promise.resolve(new JsTreeTable(this, w)));
} else {
warnLegacyTicketTypes(typedTicket.getType());
return getWidget(typedTicket).then(JsWidget::refetch);
}
}

private static void warnLegacyTicketTypes(String ticketType) {
if (JsVariableType.TABLEMAP.equalsIgnoreCase(ticketType)) {
JsLog.warn(
"TableMap is now known as PartitionedTable, fetching as a plain widget. To fetch as a PartitionedTable use that as the type.");
}
if (JsVariableType.TREETABLE.equalsIgnoreCase(ticketType)) {
JsLog.warn(
"TreeTable is now HierarchicalTable, fetching as a plain widget. To fetch as a HierarchicalTable use that as this type.");
}
}

@JsMethod
@SuppressWarnings("ConstantConditions")
public JsRunnable subscribeToFieldUpdates(JsConsumer<JsVariableChanges> callback) {
Expand Down Expand Up @@ -927,12 +959,7 @@ public Promise<JsFigure> getFigure(JsVariableDefinition varDef) {
.then(server -> new JsFigure(this,
c -> {
getWidget(varDef).then(JsWidget::refetch).then(widget -> {
FetchObjectResponse legacyResponse = new FetchObjectResponse();
legacyResponse.setData(widget.getDataAsU8());
legacyResponse.setType(widget.getType());
legacyResponse.setTypedExportIdsList(Arrays.stream(widget.getExportedObjects())
.map(JsWidgetExportedObject::typedTicket).toArray(TypedTicket[]::new));
c.apply(null, legacyResponse);
c.apply(null, makeFigureFetchResponse(widget));
widget.close();
return null;
}, error -> {
Expand All @@ -942,6 +969,15 @@ public Promise<JsFigure> getFigure(JsVariableDefinition varDef) {
}).refetch());
}

private static FetchObjectResponse makeFigureFetchResponse(JsWidget widget) {
FetchObjectResponse legacyResponse = new FetchObjectResponse();
legacyResponse.setData(widget.getDataAsU8());
legacyResponse.setType(widget.getType());
legacyResponse.setTypedExportIdsList(Arrays.stream(widget.getExportedObjects())
.map(JsWidgetExportedObject::typedTicket).toArray(TypedTicket[]::new));
return legacyResponse;
}

private TypedTicket createTypedTicket(JsVariableDefinition varDef) {
TypedTicket typedTicket = new TypedTicket();
typedTicket.setTicket(TableTicket.createTicket(varDef));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class JsVariableDefinition {
private final String applicationName;

public JsVariableDefinition(String type, String title, String id, String description) {
// base64('s/') ==> 'cy8'
if (!id.startsWith("cy8")) {
throw new IllegalArgumentException("Cannot create a VariableDefinition from a non-scope ticket");
}
this.type = type;
this.title = title == null ? JS_UNAVAILABLE : title;
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public JsWidgetExportedObject(WorkerConnection connection, TypedTicket ticket) {
return Promise.resolve(table);
});
} else {
return this.connection.getObject(
new JsVariableDefinition(ticket.getType(), null, ticket.getTicket().getTicket_asB64(), null));
return this.connection.getObject(ticket);
}
});
}
Expand All @@ -75,6 +74,7 @@ public TypedTicket typedTicket() {
/**
* Exports another copy of this reference, allowing it to be fetched separately. Results in rejection if the ticket
* was already closed (either by calling {@link #close()} or closing the object returned from {@link #fetch()}).
*
* @return a promise returning a reexported copy of this object, still referencing the same server-side object.
*/
@JsMethod
Expand Down

0 comments on commit 5fabcc7

Please sign in to comment.