use of io.deephaven.web.shared.fu.JsRunnable in project deephaven-core by deephaven.
the class QueryConnectable method startSession.
@JsMethod
public CancellablePromise<IdeSession> startSession(String type) {
JsLog.debug("Starting", type, "console session");
LazyPromise<Ticket> promise = new LazyPromise<>();
final ClientConfiguration config = connection.get().getConfig();
final Ticket ticket = new Ticket();
ticket.setTicket(config.newTicketRaw());
final JsRunnable closer = () -> {
boolean run = !cancelled.has(ticket);
if (run) {
cancelled.add(ticket);
connection.get().releaseTicket(ticket);
}
};
onConnected().then(e -> Callbacks.grpcUnaryPromise(callback -> {
StartConsoleRequest request = new StartConsoleRequest();
request.setSessionType(type);
request.setResultId(ticket);
connection.get().consoleServiceClient().startConsole(request, connection.get().metadata(), callback::apply);
})).then(result -> {
promise.succeed(ticket);
return null;
}, error -> {
promise.fail(error);
return null;
});
return promise.asPromise(result -> {
if (cancelled.has(ticket)) {
// a bit hacky, but it works...
throw new RuntimeException(CANCELLATION_MESSAGE);
}
final IdeSession session = new IdeSession(connection.get(), result, closer);
sessions.add(session);
return session;
}, closer);
}
use of io.deephaven.web.shared.fu.JsRunnable in project deephaven-core by deephaven.
the class QueryConnectable method onLogMessage.
@JsMethod
public JsRunnable onLogMessage(JsConsumer<LogItem> callback) {
final WorkerConnection connect = connection.get();
// The method returns a singleton, but we'll be extra safe here anyway.
final JsRunnable DO_NOTHING = JsRunnable.doNothing();
// we'll use this array to handle async nature of connections.
JsRunnable[] cancel = { DO_NOTHING };
connect.onOpen((s, f) -> {
// if the open did not fail, and the cancel array has not been modified...
if (f == null && cancel[0] == DO_NOTHING) {
// then go ahead and subscribe, plus stash the removal callback.
cancel[0] = connect.subscribeToLogs(callback);
}
});
return () -> {
if (cancel[0] != null) {
// if we have subscribed, this will cancel that subscription.
cancel[0].run();
// we'll use null here to tell the onOpen above to skip doing work
cancel[0] = null;
}
};
}
use of io.deephaven.web.shared.fu.JsRunnable in project deephaven-core by deephaven.
the class JsInputTable method deleteTables.
public Promise<JsInputTable> deleteTables(JsTable[] tablesToDelete) {
if (tablesToDelete.length == 0) {
return Promise.resolve(this);
}
// for each table, make a view on that table of only key columns, then union the tables and drop together
final List<JsRunnable> cleanups = new ArrayList<>();
final Ticket ticketToDelete;
final Promise<?> failureToReport;
if (tablesToDelete.length == 1) {
JsTable onlyTable = tablesToDelete[0];
// don't try too hard to find matching columns, if it looks like we have a match go for it
if (onlyTable.getColumns().length == keys.length && onlyTable.findColumns(keys).length == keys.length) {
ticketToDelete = onlyTable.getHandle().makeTicket();
failureToReport = Promise.resolve((Object) null);
} else {
// view the only table
ticketToDelete = table.getConnection().getConfig().newTicket();
cleanups.add(() -> table.getConnection().releaseTicket(ticketToDelete));
SelectOrUpdateRequest view = new SelectOrUpdateRequest();
view.setSourceId(onlyTable.state().getHandle().makeTableReference());
view.setResultId(ticketToDelete);
view.setColumnSpecsList(keys);
failureToReport = Callbacks.grpcUnaryPromise(c -> table.getConnection().tableServiceClient().view(view, table.getConnection().metadata(), c::apply));
}
} else {
// there is more than one table here, construct a merge after making a view of each table
ticketToDelete = table.getConnection().getConfig().newTicket();
cleanups.add(() -> table.getConnection().releaseTicket(ticketToDelete));
BatchTableRequest batch = new BatchTableRequest();
for (int i = 0; i < tablesToDelete.length; i++) {
JsTable toDelete = tablesToDelete[i];
SelectOrUpdateRequest view = new SelectOrUpdateRequest();
view.setSourceId(toDelete.state().getHandle().makeTableReference());
view.setColumnSpecsList(keys);
batch.addOps(new Operation()).setView(view);
}
MergeTablesRequest mergeRequest = new MergeTablesRequest();
mergeRequest.setSourceIdsList(IntStream.range(0, tablesToDelete.length).mapToObj(i -> {
TableReference ref = new TableReference();
ref.setBatchOffset(i);
return ref;
}).toArray(TableReference[]::new));
mergeRequest.setResultId(ticketToDelete);
batch.addOps(new Operation()).setMerge(mergeRequest);
failureToReport = new Promise<>((resolve, reject) -> {
ResponseStreamWrapper<ExportedTableCreationResponse> wrapper = ResponseStreamWrapper.of(table.getConnection().tableServiceClient().batch(batch, table.getConnection().metadata()));
wrapper.onData(response -> {
// kill the promise on the first failure we see
if (!response.getSuccess()) {
reject.onInvoke(response.getErrorInfo());
}
});
wrapper.onEnd(status -> resolve.onInvoke((Object) null));
});
}
// perform the delete on the current input table
DeleteTableRequest deleteRequest = new DeleteTableRequest();
deleteRequest.setInputTable(table.getHeadHandle().makeTicket());
deleteRequest.setTableToRemove(ticketToDelete);
return Callbacks.grpcUnaryPromise(c -> {
table.getConnection().inputTableServiceClient().deleteTableFromInputTable(deleteRequest, table.getConnection().metadata(), c::apply);
}).then(success -> {
cleanups.forEach(JsRunnable::run);
return Promise.resolve(this);
}, err -> {
cleanups.forEach(JsRunnable::run);
// call
return (Promise) failureToReport.then(ignore -> Promise.reject(err));
});
}
use of io.deephaven.web.shared.fu.JsRunnable 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);
});
}
use of io.deephaven.web.shared.fu.JsRunnable in project deephaven-core by deephaven.
the class JsTable method close.
@JsMethod
public void close() {
if (currentState == null) {
// deliberately avoiding JsLog so that it shows up (with stack trace) in developer's console
JsLog.warn("Table.close() called twice, second call being ignored", this);
return;
}
onClosed.forEach(JsRunnable::run);
onClosed.clear();
currentState.pause(this);
for (ClientTableState s : currentState.reversed()) {
s.releaseTable(this);
}
// make this table unusable.
currentState = null;
// LATER: add more cleanup / assertions to aggressively enable GC
subscriptions.values().forEach(TableViewportSubscription::internalClose);
subscriptions.clear();
}
Aggregations