在code代码中,咱们常常碰到异步方法嵌套。好比提交文件以后在提交表单,提交数据根据是否成功而后作出其余逻辑处理。kotlin
里面提出协程概念,利用语法糖来解决这个问题。在javaScript
里面也有async/await
来使异步用起来像同步。而在java
中我暂时没有找到该特性,使得写起来异步嵌套感受就是地狱,像吃了屎同样。利用这春节几天时间,尝试着按本身思路去解决这个问题,造个流式的轮子,因而写了Flow
小框子。java
从生活中思考代码,方法嵌套和水流的原理很类似,咱们把每一个异步当成一个水管,水从一个个管道流过,每一个管道能够对水进行加工转换。转换的这个过程咱们当成一个事件Event
。在包装事件中,咱们能够对它进行线程转换,事件转换,合并拆分等一系列转换。若是碰到异常,则直接终止这个流。git
经过Flow
静态create
方法建立一个流,then
串联下个流,若是不须要返回Void
泛型。Event
有两个泛型P、R
,第一个是前个流Flow
的返回值类型,第二个是当前流Flow
返回类型。await exec
方法是结束当前事件流,并将结果代入下个流。github
打印两句话bash
Flow.create(new Event<Void,Void>() {
@Override
public void run(Flow flow, Void aVoid, Await<Void> await) {
System.out.println("this is first flow");
await.exec(null);
}
}).then(new Event<Void, Void>() {
@Override
public void run(Flow flow, Void aVoid, Await<Void> await) {
System.out.println("this is two flow");
await.exec(null);
}
}).start();
复制代码
Lambda
简化以后网络
Flow.create((NoneEvent) (flow, await) -> {
System.out.println("this is first flow");
await.exec();
}).then((NoneEvent) (flow, await) -> {
System.out.println("this is two flow");
await.exec();
}).start();
复制代码
两数相加框架
Flow.create((FirstEvent<Integer>) (flow, await) ->
await.exec(3))
.then((Event<Integer, Integer>) (flow, integer, await) ->
await.exec(integer + 5))
.resultThen((flow, result) ->
System.out.println("total is"+result))
.start();
复制代码
resultThen
方法返回是当前流的结果,每一个flow
后面使用resultThen
均可以获取流的结果。若是遇到异常,能够经过flow throwException
方法抛出,能够在flow
后面catchThen
马上处理,也能够在最后flow
catchThen
处理。finallyThen
是事件流结束一个通知。异步
Flow.create((FirstEvent<Integer>) (flow, await) ->
await.exec(0))
.then((Event<Integer, Integer>) (flow, perVal, await) ->{
if(perVal == 0){
flow.throwException("Dividend cannot be 0!");
}else{
await.exec(perVal/5);
}
})
.resultThen((flow, result) ->
System.out.println("total is"+result))
.catchThen((flow, e) ->
System.out.println(e.getMessage()))
.finallyThen((flow, await) ->
System.out.println("this is flow end")).start();
复制代码
使用flow on
方法能够切换线程,on
传递一个Converter
参数,表明下个流切换。若是两个Converter
参数,表明当前流和下个流都切换线程。固然你也能够实现Converter
接口来实现其余功能。async
Flow.create((FirstEvent<Integer>) (flow, await) ->
await.exec(0))
.on(AndroidMain.get(),SingleThread.get())
.then((Event<Integer, Integer>) (flow, perVal, await) ->{
if(perVal == 0){
flow.throwException("Dividend cannot be 0!");
}else{
await.exec(perVal/5);
}
})
.on(AndroidMain.get())
.resultThen((flow, result) ->
System.out.println("total is"+result))
.on(AndroidMain.get())
.catchThen((flow, e) ->
System.out.println(e.getMessage()))
.on(SingleThread.get())
.finallyThen((flow, await) ->
System.out.println("this is flow end")).start();
复制代码
Collection
结果转换成多个流Flow.each((FirstEvent<List<String>>) (flow, await) -> {
ArrayList<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
await.exec(list);
}).then((LastEvent<String>) (flow, s, await) -> {
System.out.println("this is"+s);
}).start();
复制代码
Flow.merge((flow, await) -> await.exec(1),
(flow, await) -> await.exec(2),
(flow, await) -> await.exec(2)).resultThen((flow, result)
-> System.out.println"result"+result)).start();
复制代码
根据条件判断从新发起Flow
流(返回参数能够不同)ide
Flow.create((NoneEvent) (flow,await) ->{
System.out.println("start");
await.exec();
})
.on(SingleThread.get())
.conditionThen((VoidCondition) () -> false,
Flow.create((NoneEvent) (flow,await) -> {
System.out.println("this is true");
await.exec();
}),
Flow.create((NoneEvent) (flow,await) -> {
System.out.println("this is false");
await.exec();
})).start();
复制代码
根据条件判断执行Flow
流,能够合并到一块儿。(返回参数必须一致)ui
Flow.condition2(() -> isGo, (FirstEvent<Integer>) (flow, await) -> {
System.out.println("this is true");
await.exec(1);
}, (flow, await) -> {
System.out.println("this is false");
await.exec(0);
}).resultThen((flow, result) -> System.out.println("result"+result))
.watch(this).start();
复制代码
经过flow watch
方法。被观察者必须实现ILifeObservable
接口。
Flow.create((FirstEvent<Integer>) (flow, await) ->await.exec(0))
.watch(this).start();
复制代码
框子也里面提供了一些简化的类,也能够和项目网络请求框架抽象本身的Event
,这样和js
的网络的then
就几乎同样了。后续根据实际需求再作调整,试验中。