fun awaitFuture()

in src/main/com/intellij/lang/jsgraphql/GraphQLConcurrencyUtil.kt [45:102]


fun <T> awaitFuture(future: Future<T?>, timeoutMills: Long): T? {
  if (ApplicationManager.getApplication().isDispatchThread() &&
      !ApplicationManager.getApplication().isHeadlessEnvironment()
  ) {
    LOG.error("Await future on EDT may cause a deadlock")
    return null
  }

  ProgressManager.checkCanceled()

  try {
    if (future.isDone) {
      return future.get()
    }

    var totalWait = timeoutMills
    while ((timeoutMills < 0 || totalWait > 0)) {
      try {
        return future.get(QUOTA_MS, TimeUnit.MILLISECONDS)
      }
      catch (_: TimeoutException) {
        //no action
      }
      catch (e: ExecutionException) {
        if (e.cause !is CancellationException) {
          LOG.info("Awaiting future failed with exception", e)
        }
        return null
      }
      catch (_: CancellationException) {
        return null
      }
      catch (_: InterruptedException) {
        return null
      }
      totalWait -= QUOTA_MS

      ProgressManager.checkCanceled()
    }

    if (future.isDone) {
      return future.get()
    }
    else {
      if (timeoutMills != 0L) {
        LOG.debug("Timeout waiting for $timeoutMills ms")
      }
    }
  }
  catch (e: ProcessCanceledException) {
    throw e
  }
  catch (_: Exception) {
    // do nothing
  }

  return null
}