mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
continue use idea async* instead of custom callbacks
This commit is contained in:
@@ -29,7 +29,7 @@ public class ActionCallback implements Disposable {
|
||||
private final ExecutionCallback myDone;
|
||||
private final ExecutionCallback myRejected;
|
||||
|
||||
private String myError;
|
||||
protected String myError;
|
||||
|
||||
private final String myName;
|
||||
|
||||
|
||||
@@ -18,9 +18,12 @@ package com.intellij.openapi.util;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.PairConsumer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AsyncResult<T> extends ActionCallback {
|
||||
private static final Logger LOG = Logger.getInstance(AsyncResult.class);
|
||||
|
||||
protected T myResult;
|
||||
|
||||
@NotNull
|
||||
@@ -49,6 +52,13 @@ public class AsyncResult<T> extends ActionCallback {
|
||||
return subResult;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ActionCallback subCallback(@NotNull Consumer<T> doneHandler) {
|
||||
ActionCallback subCallback = new ActionCallback();
|
||||
doWhenDone(new SubCallbackDoneCallback<T>(subCallback, doneHandler)).notifyWhenRejected(subCallback);
|
||||
return subCallback;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public AsyncResult<T> doWhenDone(@NotNull final Handler<T> handler) {
|
||||
doWhenDone(new Runnable() {
|
||||
@@ -63,6 +73,7 @@ public class AsyncResult<T> extends ActionCallback {
|
||||
@NotNull
|
||||
public AsyncResult<T> doWhenDone(@NotNull final Consumer<T> consumer) {
|
||||
doWhenDone(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
consumer.consume(myResult);
|
||||
}
|
||||
@@ -82,6 +93,17 @@ public class AsyncResult<T> extends ActionCallback {
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public AsyncResult<T> doWhenRejected(@NotNull final PairConsumer<T, String> consumer) {
|
||||
doWhenRejected(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
consumer.consume(myResult, myError);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final AsyncResult<T> notify(@NotNull final ActionCallback child) {
|
||||
super.notify(child);
|
||||
@@ -114,8 +136,6 @@ public class AsyncResult<T> extends ActionCallback {
|
||||
|
||||
// we don't use inner class, avoid memory leak, we don't want to hold this result while dependent is computing
|
||||
private static class SubResultDoneCallback<Result, SubResult, AsyncSubResult extends AsyncResult<SubResult>> implements Handler<Result> {
|
||||
private static final Logger LOG = Logger.getInstance(SubResultDoneCallback.class);
|
||||
|
||||
private final AsyncSubResult subResult;
|
||||
private final Function<Result, SubResult> doneHandler;
|
||||
|
||||
@@ -138,4 +158,27 @@ public class AsyncResult<T> extends ActionCallback {
|
||||
subResult.setDone(v);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SubCallbackDoneCallback<Result> implements Handler<Result> {
|
||||
private final ActionCallback subResult;
|
||||
private final Consumer<Result> doneHandler;
|
||||
|
||||
public SubCallbackDoneCallback(ActionCallback subResult, Consumer<Result> doneHandler) {
|
||||
this.subResult = subResult;
|
||||
this.doneHandler = doneHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Result result) {
|
||||
try {
|
||||
doneHandler.consume(result);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
subResult.reject(e.getMessage());
|
||||
LOG.error(e);
|
||||
return;
|
||||
}
|
||||
subResult.setDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user