Skip to content

Commit

Permalink
Create an internal Event type for use in non-browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Oct 17, 2024
1 parent d3abea6 commit 26020ba
Show file tree
Hide file tree
Showing 33 changed files with 192 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import elemental2.dom.DomGlobal;
import elemental2.promise.Promise;
import elemental2.promise.Promise.PromiseExecutorCallbackFn.RejectCallbackFn;
import io.deephaven.web.client.api.event.HasEventHandling;
import io.deephaven.web.shared.fu.JsBiConsumer;

import javax.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.config_pb.ConfigurationConstantsResponse;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.config_pb_service.ConfigServiceClient;
import io.deephaven.javascript.proto.dhinternal.jspb.Map;
import io.deephaven.web.client.api.event.HasEventHandling;
import io.deephaven.web.client.api.storage.JsStorageService;
import io.deephaven.web.client.fu.JsLog;
import io.deephaven.web.client.fu.LazyPromise;
Expand Down Expand Up @@ -50,9 +51,7 @@ public CoreClient(String serverUrl, @TsTypeRef(ConnectOptions.class) @JsOptional

// For now the only real connection is the IdeConnection, so we re-fire the auth token refresh
// event here for the UI to listen to
ideConnection.addEventListener(EVENT_REFRESH_TOKEN_UPDATED, event -> {
fireEvent(EVENT_REFRESH_TOKEN_UPDATED, event);
});
ideConnection.addEventListener(EVENT_REFRESH_TOKEN_UPDATED, this::fireEvent);
}

private <R> Promise<String[][]> getConfigs(Consumer<JsBiConsumer<Object, R>> rpcCall,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import elemental2.core.JsArray;
import elemental2.core.JsObject;
import elemental2.core.JsSet;
import elemental2.dom.CustomEvent;
import elemental2.dom.CustomEventInit;
import elemental2.dom.Event;
import elemental2.promise.Promise;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.partitionedtable_pb.GetTableRequest;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.partitionedtable_pb.MergeRequest;
Expand All @@ -18,6 +15,7 @@
import io.deephaven.web.client.api.barrage.WebBarrageUtils;
import io.deephaven.web.client.api.barrage.def.ColumnDefinition;
import io.deephaven.web.client.api.barrage.def.InitialTableDefinition;
import io.deephaven.web.client.api.event.Event;
import io.deephaven.web.client.api.lifecycle.HasLifecycle;
import io.deephaven.web.client.api.subscription.SubscriptionTableData;
import io.deephaven.web.client.api.subscription.TableSubscription;
Expand Down Expand Up @@ -112,10 +110,8 @@ public Promise<JsPartitionedTable> refetch() {
fireEvent(EVENT_RECONNECT);
return null;
}, failure -> {
CustomEventInit<Object> init = CustomEventInit.create();
init.setDetail(failure);
unsuppressEvents();
fireEvent(EVENT_RECONNECTFAILED, init);
fireEvent(EVENT_RECONNECTFAILED, failure);
suppressEvents();
return null;
});
Expand All @@ -140,20 +136,16 @@ private Promise<JsPartitionedTable> subscribeToBaseTable() {
return promise.asPromise();
}

private void handleKeys(Event update) {
// noinspection unchecked
CustomEvent<SubscriptionTableData> event = (CustomEvent<SubscriptionTableData>) update;
private void handleKeys(Event<SubscriptionTableData> update) {

// We're only interested in added rows, send an event indicating the new keys that are available
SubscriptionTableData eventData = event.detail;
SubscriptionTableData eventData = update.getDetail();
RangeSet added = eventData.getAdded().getRange();
added.indexIterator().forEachRemaining((long index) -> {
// extract the key to use
JsArray<Object> key = eventData.getColumns().map((c, p1) -> eventData.getData(index, c));
knownKeys.add(key.asList());
CustomEventInit<JsArray<Object>> init = CustomEventInit.create();
init.setDetail(key);
fireEvent(EVENT_KEYADDED, init);
fireEvent(EVENT_KEYADDED, key);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.vertispan.tsdefs.annotations.TsUnion;
import com.vertispan.tsdefs.annotations.TsUnionMember;
import elemental2.core.JsArray;
import elemental2.dom.CustomEventInit;
import elemental2.promise.IThenable.ThenOnFulfilledCallbackFn;
import elemental2.promise.Promise;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.hierarchicaltable_pb.RollupRequest;
Expand Down Expand Up @@ -1668,9 +1667,7 @@ public void setState(final ClientTableState state) {
});
});
}
final CustomEventInit init = CustomEventInit.create();
init.setDetail(state);
fireEvent(INTERNAL_EVENT_STATECHANGED, init);
fireEvent(INTERNAL_EVENT_STATECHANGED, state);
}
}

Expand Down Expand Up @@ -1739,9 +1736,7 @@ public void setSize(double s) {
// If the size changed, and we have no subscription active, fire. Otherwise, we want to let the
// subscription itself manage this, so that the size changes are synchronized with data changes,
// and consumers won't be confused by the table size not matching data.
CustomEventInit event = CustomEventInit.create();
event.setDetail(s);
fireEvent(JsTable.EVENT_SIZECHANGED, event);
fireEvent(JsTable.EVENT_SIZECHANGED, s);
}
fireEvent(JsTable.INTERNAL_EVENT_SIZELISTENER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import com.vertispan.tsdefs.annotations.TsTypeRef;
import elemental2.core.JsArray;
import elemental2.core.JsString;
import elemental2.dom.CustomEvent;
import elemental2.promise.Promise;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.TypedTicket;
import io.deephaven.web.client.api.console.JsVariableType;
import io.deephaven.web.client.api.event.Event;
import io.deephaven.web.client.api.event.EventFn;
import io.deephaven.web.client.api.filter.FilterCondition;
import io.deephaven.web.client.api.subscription.AbstractTableSubscription;
import io.deephaven.web.client.api.subscription.ViewportData;
Expand Down Expand Up @@ -223,7 +224,7 @@ public <T> boolean removeEventListener(String name, EventFn<T> callback) {
}

@JsMethod
public <T> Promise<CustomEvent<T>> nextEvent(String eventName, Double timeoutInMillis) {
public <T> Promise<Event<T>> nextEvent(String eventName, Double timeoutInMillis) {
return wrappedTable.nextEvent(eventName, timeoutInMillis);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.vertispan.tsdefs.annotations.TsIgnore;
import elemental2.core.JsArray;
import elemental2.core.JsSet;
import elemental2.dom.CustomEventInit;
import elemental2.promise.Promise;
import io.deephaven.javascript.proto.dhinternal.grpcweb.Grpc;
import io.deephaven.javascript.proto.dhinternal.grpcweb.client.RpcOptions;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb.TerminationNotificationResponse;
import io.deephaven.web.client.api.event.HasEventHandling;
import io.deephaven.web.client.api.grpc.MultiplexedWebsocketTransport;
import io.deephaven.web.client.ide.IdeSession;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.*;
Expand Down Expand Up @@ -65,12 +65,10 @@ public void notifyConnectionError(ResponseStreamWrapper.Status status) {
}
notifiedConnectionError = true;

CustomEventInit<JsPropertyMap<Object>> event = CustomEventInit.create();
event.setDetail(JsPropertyMap.of(
fireEvent(HACK_CONNECTION_FAILURE, JsPropertyMap.of(
"status", status.getCode(),
"details", status.getDetails(),
"metadata", status.getMetadata()));
fireEvent(HACK_CONNECTION_FAILURE, event);
JsLog.warn(
"The event dh.IdeConnection.HACK_CONNECTION_FAILURE is deprecated and will be removed in a later release");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import elemental2.core.JsSet;
import elemental2.core.JsWeakMap;
import elemental2.core.Uint8Array;
import elemental2.dom.CustomEventInit;
import elemental2.dom.DomGlobal;
import elemental2.promise.Promise;
import io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.browserflight_pb_service.BrowserFlightServiceClient;
Expand Down Expand Up @@ -65,6 +64,7 @@
import io.deephaven.web.client.api.console.JsVariableChanges;
import io.deephaven.web.client.api.console.JsVariableDefinition;
import io.deephaven.web.client.api.console.JsVariableType;
import io.deephaven.web.client.api.event.HasEventHandling;
import io.deephaven.web.client.api.grpc.UnaryWithHeaders;
import io.deephaven.web.client.api.i18n.JsTimeZone;
import io.deephaven.web.client.api.impl.TicketAndPromise;
Expand Down Expand Up @@ -465,9 +465,8 @@ private Promise<Void> authUpdate() {
if (!existing.getAt(0).equals(authorization.getAt(0))) {
// use this new token
metadata().set(FLIGHT_AUTH_HEADER_NAME, authorization);
CustomEventInit<JsRefreshToken> init = CustomEventInit.create();
init.setDetail(new JsRefreshToken(authorization.getAt(0), sessionTimeoutMs));
info.fireEvent(EVENT_REFRESH_TOKEN_UPDATED, init);
info.fireEvent(EVENT_REFRESH_TOKEN_UPDATED,
new JsRefreshToken(authorization.getAt(0), sessionTimeoutMs));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//
package io.deephaven.web.client.api.batch;

import elemental2.dom.CustomEventInit;
import elemental2.promise.Promise;
import elemental2.promise.Promise.PromiseExecutorCallbackFn.RejectCallbackFn;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket;
Expand Down Expand Up @@ -341,7 +340,6 @@ public Promise<Void> sendRequest() {
}

private void failTable(JsTable t, String failureMessage) {
final CustomEventInit event = CustomEventInit.create();
ClientTableState best = t.state();
for (ClientTableState state : best.reversed()) {
if (allStates().anyMatch(state::equals)) {
Expand All @@ -350,17 +348,16 @@ private void failTable(JsTable t, String failureMessage) {
}
}

event.setDetail(JsPropertyMap.of(
"errorMessage", failureMessage,
"configuration", best.toJs()));
try {
t.rollback();
} catch (Exception e) {
JsLog.warn(
"An exception occurred trying to rollback the table. This means that there will be no ticking data until the table configuration is applied again in a way that makes sense. See IDS-5199 for more detail.",
e);
}
t.fireEvent(CoreClient.EVENT_REQUEST_FAILED, event);
t.fireEvent(CoreClient.EVENT_REQUEST_FAILED, JsPropertyMap.of(
"errorMessage", failureMessage,
"configuration", best.toJs()));
}

private void failed(RejectCallbackFn reject, String fail) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.web.client.api.event;

import com.vertispan.tsdefs.annotations.TsInterface;
import com.vertispan.tsdefs.annotations.TsName;
import jsinterop.annotations.JsNullable;
import jsinterop.annotations.JsProperty;

/**
* Similar to the browser {@code CustomEvent} type, this class holds only the type of the event, and optionally some
* details about the event.
*
* @param <T> the type of the event detail
*/
@TsInterface
@TsName(namespace = "dh")
public class Event<T> {
private final String type;
private final T detail;

public Event(String type, T detail) {
this.type = type;
this.detail = detail;
}

@JsProperty
public String getType() {
return type;
}

@JsProperty
@JsNullable
public T getDetail() {
return detail;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.web.client.api.event;

import jsinterop.annotations.JsFunction;

/**
* Event handler function. In JS/TS, this will only show up as a function type when used, but these docs will never be
* visible.
*/
@JsFunction
public interface EventFn<T> {
void onEvent(Event<T> e);
}
Loading

0 comments on commit 26020ba

Please sign in to comment.