Search in sources :

Example 1 with JsRunnable

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);
}
Also used : JsRunnable(io.deephaven.web.shared.fu.JsRunnable) RemoverFn(io.deephaven.web.shared.fu.RemoverFn) JsMethod(jsinterop.annotations.JsMethod) Promise(elemental2.promise.Promise) ConnectToken(io.deephaven.web.shared.data.ConnectToken) JsPropertyMap(jsinterop.base.JsPropertyMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) JsProperty(jsinterop.annotations.JsProperty) CANCELLATION_MESSAGE(io.deephaven.web.shared.fu.PromiseLike.CANCELLATION_MESSAGE) ResponseStreamWrapper(io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper) StartConsoleRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.StartConsoleRequest) LazyPromise(io.deephaven.web.client.fu.LazyPromise) JsLog(io.deephaven.web.client.fu.JsLog) JsConsumer(io.deephaven.web.shared.fu.JsConsumer) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) DomGlobal(elemental2.dom.DomGlobal) GetConsoleTypesRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.GetConsoleTypesRequest) CustomEventInit(elemental2.dom.CustomEventInit) IdeSession(io.deephaven.ide.shared.IdeSession) JsArray(elemental2.core.JsArray) JsSet(elemental2.core.JsSet) LogItem(io.deephaven.web.shared.data.LogItem) JsIgnore(jsinterop.annotations.JsIgnore) List(java.util.List) GetConsoleTypesResponse(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.GetConsoleTypesResponse) CancellablePromise(io.deephaven.web.client.fu.CancellablePromise) IdeSession(io.deephaven.ide.shared.IdeSession) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) StartConsoleRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.StartConsoleRequest) JsRunnable(io.deephaven.web.shared.fu.JsRunnable) LazyPromise(io.deephaven.web.client.fu.LazyPromise) JsMethod(jsinterop.annotations.JsMethod)

Example 2 with JsRunnable

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;
        }
    };
}
Also used : JsRunnable(io.deephaven.web.shared.fu.JsRunnable) JsMethod(jsinterop.annotations.JsMethod)

Example 3 with JsRunnable

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));
    });
}
Also used : SelectOrUpdateRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.SelectOrUpdateRequest) IntStream(java.util.stream.IntStream) JsRunnable(io.deephaven.web.shared.fu.JsRunnable) BatchTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.BatchTableRequest) Arrays(java.util.Arrays) JsOptional(jsinterop.annotations.JsOptional) Promise(elemental2.promise.Promise) JsPropertyMap(jsinterop.base.JsPropertyMap) ArrayList(java.util.ArrayList) Callbacks(io.deephaven.web.client.api.Callbacks) JsProperty(jsinterop.annotations.JsProperty) TableReference(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.TableReference) Operation(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.batchtablerequest.Operation) JsTable(io.deephaven.web.client.api.JsTable) DeleteTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.DeleteTableRequest) ResponseStreamWrapper(io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper) JsType(jsinterop.annotations.JsType) JsObject(elemental2.core.JsObject) SelectOrUpdateRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.SelectOrUpdateRequest) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) ExportedTableCreationResponse(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.ExportedTableCreationResponse) AddTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.AddTableRequest) MergeTablesRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.MergeTablesRequest) Column(io.deephaven.web.client.api.Column) JsIgnore(jsinterop.annotations.JsIgnore) List(java.util.List) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) JsRunnable(io.deephaven.web.shared.fu.JsRunnable) ArrayList(java.util.ArrayList) Operation(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.batchtablerequest.Operation) DeleteTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.DeleteTableRequest) Promise(elemental2.promise.Promise) JsTable(io.deephaven.web.client.api.JsTable) TableReference(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.TableReference) BatchTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.BatchTableRequest) ResponseStreamWrapper(io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper) MergeTablesRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.MergeTablesRequest) JsObject(elemental2.core.JsObject)

Example 4 with JsRunnable

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);
    });
}
Also used : JsRunnable(io.deephaven.web.shared.fu.JsRunnable) JsOptional(jsinterop.annotations.JsOptional) JsCommandResult(io.deephaven.web.client.api.console.JsCommandResult) JsFigure(io.deephaven.web.client.api.widget.plot.JsFigure) HashMap(java.util.HashMap) Promise(elemental2.promise.Promise) BiDiStream(io.deephaven.web.client.api.barrage.stream.BiDiStream) JsPropertyMap(jsinterop.base.JsPropertyMap) Supplier(java.util.function.Supplier) Any(jsinterop.base.Any) EVENT_TABLE_OPENED(io.deephaven.web.client.api.QueryConnectable.EVENT_TABLE_OPENED) Js(jsinterop.base.Js) JsArrayLike(jsinterop.base.JsArrayLike) JsVariableChanges(io.deephaven.web.client.api.console.JsVariableChanges) Map(java.util.Map) io.deephaven.web.client.api(io.deephaven.web.client.api) io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb) JsType(jsinterop.annotations.JsType) LazyPromise(io.deephaven.web.client.fu.LazyPromise) ExecutionHandle(io.deephaven.web.shared.ide.ExecutionHandle) JsLog(io.deephaven.web.client.fu.JsLog) JsConsumer(io.deephaven.web.shared.fu.JsConsumer) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) CustomEventInit(elemental2.dom.CustomEventInit) TextDocumentContentChangeEvent(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.changedocumentrequest.TextDocumentContentChangeEvent) JsArray(elemental2.core.JsArray) JsSet(elemental2.core.JsSet) Consumer(java.util.function.Consumer) LogItem(io.deephaven.web.shared.data.LogItem) JsIgnore(jsinterop.annotations.JsIgnore) JsVariableDefinition(io.deephaven.web.client.api.console.JsVariableDefinition) CancellablePromise(io.deephaven.web.client.fu.CancellablePromise) Timer(com.google.gwt.user.client.Timer) Promise(elemental2.promise.Promise) LazyPromise(io.deephaven.web.client.fu.LazyPromise) CancellablePromise(io.deephaven.web.client.fu.CancellablePromise) JsRunnable(io.deephaven.web.shared.fu.JsRunnable) JsConsumer(io.deephaven.web.shared.fu.JsConsumer) Consumer(java.util.function.Consumer) JsVariableDefinition(io.deephaven.web.client.api.console.JsVariableDefinition) LazyPromise(io.deephaven.web.client.fu.LazyPromise) JsVariableChanges(io.deephaven.web.client.api.console.JsVariableChanges)

Example 5 with JsRunnable

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();
}
Also used : JsRunnable(io.deephaven.web.shared.fu.JsRunnable) ClientTableState(io.deephaven.web.client.state.ClientTableState) TableViewportSubscription(io.deephaven.web.client.api.subscription.TableViewportSubscription) JsMethod(jsinterop.annotations.JsMethod)

Aggregations

JsRunnable (io.deephaven.web.shared.fu.JsRunnable)6 Promise (elemental2.promise.Promise)3 Ticket (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket)3 JsIgnore (jsinterop.annotations.JsIgnore)3 JsMethod (jsinterop.annotations.JsMethod)3 JsPropertyMap (jsinterop.base.JsPropertyMap)3 JsArray (elemental2.core.JsArray)2 JsSet (elemental2.core.JsSet)2 CustomEventInit (elemental2.dom.CustomEventInit)2 ResponseStreamWrapper (io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper)2 CancellablePromise (io.deephaven.web.client.fu.CancellablePromise)2 JsLog (io.deephaven.web.client.fu.JsLog)2 LazyPromise (io.deephaven.web.client.fu.LazyPromise)2 LogItem (io.deephaven.web.shared.data.LogItem)2 JsConsumer (io.deephaven.web.shared.fu.JsConsumer)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Supplier (java.util.function.Supplier)2 JsProperty (jsinterop.annotations.JsProperty)2 Timer (com.google.gwt.user.client.Timer)1