@Override
  public void invokeAndWait(@NotNull Runnable runnable, @NotNull ModalityState modalityState) {
    if (isDispatchThread()) {
      runIntendedWriteActionOnCurrentThread(runnable);
      return;
    }
    if (EDT.isCurrentThreadEdt()) {
      runIntendedWriteActionOnCurrentThread(runnable);
      return;
    }

    if (holdsReadLock()) {
      throw new IllegalStateException("Calling invokeAndWait from read-action leads to possible deadlock.");
    }

    Runnable r =
      myTransactionGuard.wrapLaterInvocation(AppScheduledExecutorService.captureContextCancellationForRunnableThatDoesNotOutliveContextScope(runnable), modalityState);
    LaterInvocator.invokeAndWait(modalityState, wrapWithRunIntendedWriteAction(r));
  }
@Override
  public void invokeAndWait(@NotNull Runnable runnable, @NotNull ModalityState modalityState)