Vertx: Future异步操作设计:修订间差异

来自WHY42
 
(未显示同一用户的3个中间版本)
第25行: 第25行:
interface Future<T>
interface Future<T>
     extends AsyncResult<T> {
     extends AsyncResult<T> {
    /* 注册相关的回调函数,在执行完成的时候运行。倘若希望成功时执行那么处理结果(T),关心失败则处理异常;
      倘若无论成功与否都希望执行,那么需要处理AsyncResult<T> */
     boolean isComplete();
     boolean isComplete();
     Future<T> onComplete(Handler<AsyncResult<T>> handler);
     Future<T> onComplete(Handler<AsyncResult<T>> handler);
     Future<T> onSuccess(Handler<T> handler);
     Future<T> onSuccess(Handler<T> handler);
     Future<T> onFailure(Handler<Throwable> handler);
     Future<T> onFailure(Handler<Throwable> handler);
    /* 跟map差不多,但是map的对象本身也是一个Future */
     <V> Future<V> compose(Function<T, Future<V>> mapper);
     <V> Future<V> compose(Function<T, Future<V>> mapper);
     <V> Future<V> compose(Function<T, Future<V>> successMapper,  
     <V> Future<V> compose(Function<T, Future<V>> successMapper,  
                       Function<Throwable, Future<V>> failureMapper);
                       Function<Throwable, Future<V>> failureMapper);
    /* 从错误中恢复, 跟otherwise类似但是mapper也是一个Future。即如果失败那干别的 */
     Future<T> recover(Function<Throwable, Future<T>> mapper);
     Future<T> recover(Function<Throwable, Future<T>> mapper);
    /* 根据结果决定下一步要怎么去做 */
     <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
     <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    /* 无论结果如何都会执行的动作 */
     <V> Future<T> eventually(Function<Void, Future<V>> mapper);
     <V> Future<T> eventually(Function<Void, Future<V>> mapper);
     CompletionStage<T> toCompletionState();
     CompletionStage<T> toCompletionState();
    /* 重载了AsyncResult相关的转换操作 */
    <V> Future<V> map(Function<T, V> mapper);
    <V> Future<V> map(V value);
    <V> Future<V> mapEmpty();
    Future<T> otherwise(T value);
    Future<T> otherwise(Function<Throwable, T> mapper);
    Future<T> otherwiseEmpty();
}
}


</syntaxhighlight>
</syntaxhighlight>


== Promise ==
Promis表示一个可写的动作。
<syntaxhighlight lang="java">
interface Promist<T>
    extends Handler<AsyncResult<T>> {
    void handle(AsyncResult<T> asyncResult);
    void complete(T result);
    void complete();
    void fail(Throwable cause);
    void fail(String message);
    boolean tryComplete(T result);
    boolean tryFail(Throwable cause);
    boolean tryFail(String message);
    Future<T> future();
}
</syntaxhighlight>
[[Category:Vert.x]]
[[Category:Vert.x]]

2021年7月26日 (一) 07:25的最新版本

Future相关操作的设计

AsyncResult

AsyncResult表示一个异步操作的结果,这个结果要么是成功的,要么是失败的。

interface AsyncResult<T> {
    /* 获取异步结果,或者失败原因。倘若失败,那么结果为null */
    T getResult();
    Throwable cause();
    boolean succeeded();
    boolean failed();

    /* 对结果进行转换。如果成功,那么可以将成功的结果转换为另一种形式;否则可以得到一个替代的结果。*/
    <V> AsyncResult<V> map(Function<T, V> mapper);
    <V> AsyncResult<V> map(V value);
    <V> AsyncResult<V> mapEmpty();
    AsyncResult<T> otherwise(T value);
    AsyncResult<T> otherwise(Function<Throwable, T> mapper);
    AsyncResult<T> otherwiseEmpty();
}

Future

Future表示一个尚未完成的异步操作,并继承自AsyncResult

interface Future<T>
    extends AsyncResult<T> {
    /* 注册相关的回调函数,在执行完成的时候运行。倘若希望成功时执行那么处理结果(T),关心失败则处理异常;
       倘若无论成功与否都希望执行,那么需要处理AsyncResult<T> */
    boolean isComplete();
    Future<T> onComplete(Handler<AsyncResult<T>> handler);
    Future<T> onSuccess(Handler<T> handler);
    Future<T> onFailure(Handler<Throwable> handler);

    /* 跟map差不多,但是map的对象本身也是一个Future */
    <V> Future<V> compose(Function<T, Future<V>> mapper);
    <V> Future<V> compose(Function<T, Future<V>> successMapper, 
                      Function<Throwable, Future<V>> failureMapper);
    /* 从错误中恢复, 跟otherwise类似但是mapper也是一个Future。即如果失败那干别的 */
    Future<T> recover(Function<Throwable, Future<T>> mapper);
    /* 根据结果决定下一步要怎么去做 */
    <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    /* 无论结果如何都会执行的动作 */
    <V> Future<T> eventually(Function<Void, Future<V>> mapper);

    CompletionStage<T> toCompletionState();

    /* 重载了AsyncResult相关的转换操作 */
    <V> Future<V> map(Function<T, V> mapper);
    <V> Future<V> map(V value);
    <V> Future<V> mapEmpty();
    Future<T> otherwise(T value);
    Future<T> otherwise(Function<Throwable, T> mapper);
    Future<T> otherwiseEmpty();
}

Promise

Promis表示一个可写的动作。

interface Promist<T>
    extends Handler<AsyncResult<T>> {
    void handle(AsyncResult<T> asyncResult);
    void complete(T result);
    void complete();
    void fail(Throwable cause);
    void fail(String message);
    boolean tryComplete(T result);
    boolean tryFail(Throwable cause);
    boolean tryFail(String message);
    Future<T> future();
}