Commit 3f74cd1c authored by Aleksey Pivovarov's avatar Aleksey Pivovarov
Browse files

BackgroundTaskUtil: cleanup - inline method

parent 699a49aa
Branches unavailable Tags unavailable
No related merge requests found
Showing with 44 additions and 53 deletions
+44 -53
......@@ -205,67 +205,63 @@ public class BackgroundTaskUtil {
@NotNull
@CalledInAny
public static ProgressIndicator executeOnPooledThread(@NotNull Disposable parent, @NotNull Runnable runnable) {
ModalityState modalityState = ModalityState.defaultModalityState();
return runUnderDisposeAwareIndicator(runnable, parent, modalityState, true);
}
@CalledInAny
private static ProgressIndicator runUnderDisposeAwareIndicator(@NotNull Runnable task,
@NotNull Disposable parent,
@NotNull ModalityState modalityState,
boolean onPooledThread) {
ProgressIndicator indicator = new EmptyProgressIndicator(modalityState);
ProgressIndicator indicator = new EmptyProgressIndicator();
indicator.start();
if (onPooledThread) {
CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
ProgressManager.getInstance().runProcess(task, indicator);
}, AppExecutorUtil.getAppExecutorService());
CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
ProgressManager.getInstance().runProcess(runnable, indicator);
}, AppExecutorUtil.getAppExecutorService());
Disposable disposable = () -> {
if (indicator.isRunning()) indicator.cancel();
try {
future.get(1, TimeUnit.SECONDS);
}
catch (ExecutionException e) {
if (e.getCause() instanceof ProcessCanceledException) {
// ignore: expected cancellation
}
else {
LOG.error(e);
}
Disposable disposable = () -> {
if (indicator.isRunning()) indicator.cancel();
try {
future.get(1, TimeUnit.SECONDS);
}
catch (ExecutionException e) {
if (e.getCause() instanceof ProcessCanceledException) {
// ignore: expected cancellation
}
catch (InterruptedException | TimeoutException e) {
else {
LOG.error(e);
}
};
if (!registerIfParentNotDisposed(parent, disposable)) {
indicator.cancel();
return indicator;
}
future.whenComplete((o, e) -> Disposer.dispose(disposable));
}
else {
Disposable disposable = () -> {
if (indicator.isRunning()) indicator.cancel();
};
if (!registerIfParentNotDisposed(parent, disposable)) {
indicator.cancel();
return indicator;
catch (InterruptedException | TimeoutException e) {
LOG.error(e);
}
};
try {
ProgressManager.getInstance().runProcess(task, indicator);
}
finally {
Disposer.dispose(disposable);
}
if (!registerIfParentNotDisposed(parent, disposable)) {
indicator.cancel();
return indicator;
}
future.whenComplete((o, e) -> Disposer.dispose(disposable));
return indicator;
}
@CalledInAny
public static void runUnderDisposeAwareIndicator(@NotNull Disposable parent, @NotNull Runnable task) {
ProgressIndicator indicator = new EmptyProgressIndicator(ModalityState.defaultModalityState());
indicator.start();
Disposable disposable = () -> {
if (indicator.isRunning()) indicator.cancel();
};
if (!registerIfParentNotDisposed(parent, disposable)) {
indicator.cancel();
return;
}
try {
ProgressManager.getInstance().runProcess(task, indicator);
}
finally {
Disposer.dispose(disposable);
}
}
private static boolean registerIfParentNotDisposed(@NotNull Disposable parent, @NotNull Disposable disposable) {
return ReadAction.compute(() -> {
if (Disposer.isDisposed(parent)) return false;
......@@ -280,11 +276,6 @@ public class BackgroundTaskUtil {
});
}
@CalledInAny
public static void runUnderDisposeAwareIndicator(@NotNull Disposable parent, @NotNull Runnable task) {
runUnderDisposeAwareIndicator(task, parent, ModalityState.defaultModalityState(), false);
}
/**
* Wraps {@link MessageBus#syncPublisher(Topic)} in a dispose check,
* and throws a {@link ProcessCanceledException} if the project is disposed,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment