modeldb-main/backend/common/src/main/java/ai/verta/modeldb/common/futures/FutureGrpc.java
package ai.verta.modeldb.common.futures;

import ai.verta.modeldb.common.CommonUtils;
import com.google.protobuf.GeneratedMessageV3;
import io.grpc.stub.StreamObserver;

@SuppressWarnings({"squid:S100"})
public class FutureGrpc {
  private FutureGrpc() {}

  // Injects the result of the Scala future into the grpc StreamObserver as the return of the server
  public static <T extends GeneratedMessageV3> void ServerResponse(
      StreamObserver<T> observer, InternalFuture<T> f, FutureExecutor ex) {
    f.whenComplete(
        (v, t) -> {
          if (t == null) {
            observer.onNext(v);
            observer.onCompleted();
          } else {
            CommonUtils.observeError(observer, t);
          }
        },
        ex);
  }

  public static <T extends GeneratedMessageV3> void serverResponse(
      StreamObserver<T> observer, Future<T> f) {
    f.whenComplete(
        (v, t) -> {
          if (t == null) {
            observer.onNext(v);
            observer.onCompleted();
          } else {
            CommonUtils.observeError(observer, t);
          }
        });
  }
}
