v8 and wip sdk: use idea platform ActionCallback

This commit is contained in:
Vladimir Krivosheev
2013-04-16 13:09:42 +02:00
parent ca39c07358
commit 0662426221
2 changed files with 47 additions and 1 deletions
@@ -110,7 +110,7 @@ public class ActionCallback implements Disposable {
}
@NotNull
public final ActionCallback notify(@NotNull final ActionCallback child) {
public ActionCallback notify(@NotNull final ActionCallback child) {
return doWhenDone(child.createSetDoneRunnable()).doWhenRejected(child.createSetRejectedRunnable());
}
@@ -15,6 +15,8 @@
*/
package com.intellij.openapi.util;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
public class AsyncResult<T> extends ActionCallback {
@@ -34,6 +36,18 @@ public class AsyncResult<T> extends ActionCallback {
return this;
}
@NotNull
public <DependentResult> AsyncResult<DependentResult> subResult(@NotNull Function<T, DependentResult> doneHandler) {
return subResult(new AsyncResult<DependentResult>(), doneHandler);
}
@NotNull
public <SubResult, SubAsyncResult extends AsyncResult<SubResult>> SubAsyncResult subResult(@NotNull SubAsyncResult subResult,
@NotNull Function<T, SubResult> doneHandler) {
doWhenDone(new SubResultDoneCallback<T, SubResult, SubAsyncResult>(subResult, doneHandler)).notifyWhenRejected(subResult);
return subResult;
}
@NotNull
public AsyncResult<T> doWhenDone(@NotNull final Handler<T> handler) {
doWhenDone(new Runnable() {
@@ -54,6 +68,12 @@ public class AsyncResult<T> extends ActionCallback {
return this;
}
@NotNull
public final AsyncResult<T> notify(@NotNull final ActionCallback child) {
super.notify(child);
return this;
}
public T getResult() {
return myResult;
}
@@ -77,4 +97,30 @@ public class AsyncResult<T> extends ActionCallback {
setRejected(value);
}
}
// 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 dependentResult;
private final Function<Result, SubResult> doneHandler;
public SubResultDoneCallback(AsyncSubResult dependentResult, Function<Result, SubResult> doneHandler) {
this.dependentResult = dependentResult;
this.doneHandler = doneHandler;
}
public void run(Result result) {
SubResult v;
try {
v = doneHandler.fun(result);
}
catch (Throwable e) {
dependentResult.setRejected();
LOG.error(e);
return;
}
dependentResult.setDone(v);
}
}
}