use of io.deephaven.web.client.fu.LazyPromise 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.fu.LazyPromise in project deephaven-core by deephaven.
the class JsTable method getInternalViewportData.
public Promise<TableData> getInternalViewportData() {
final LazyPromise<TableData> promise = new LazyPromise<>();
final ClientTableState active = state();
active.onRunning(state -> {
if (currentViewportData == null) {
// no viewport data received yet; let's setup a one-shot UPDATED event listener
addEventListenerOneShot(EVENT_UPDATED, ignored -> promise.succeed(currentViewportData));
} else {
promise.succeed(currentViewportData);
}
}, promise::fail, () -> promise.fail("Table closed before viewport data was read"));
return promise.asPromise(MAX_BATCH_TIME);
}
use of io.deephaven.web.client.fu.LazyPromise in project deephaven-core by deephaven.
the class JsTable method copy.
@JsMethod
public Promise<JsTable> copy(boolean resolved) {
if (resolved) {
LazyPromise<ClientTableState> promise = new LazyPromise<>();
final ClientTableState unresolved = state();
unresolved.onRunning(promise::succeed, promise::fail, () -> promise.fail("Table failed or closed, copy could not complete"));
return promise.asPromise(MAX_BATCH_TIME).then(s -> Promise.resolve(new JsTable(this)));
}
return Promise.resolve(new JsTable(this));
}
use of io.deephaven.web.client.fu.LazyPromise 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.client.fu.LazyPromise 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