温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何造个android Flow流式响应的轮子

发布时间:2020-10-21 21:55:14 来源:脚本之家 阅读:133 作者:黄燕斌 栏目:移动开发

点我啊,代码在这里

原因

在code代码中,我们经常碰到异步方法嵌套。比如提交文件之后在提交表单,提交数据根据是否成功然后做出其他逻辑处理。kotlin里面提出协程概念,利用语法糖来解决这个问题。在javaScript里面也有async/await来使异步用起来像同步。而在java中我暂时没有找到该特性,使得写起来异步嵌套感觉就是地狱,像吃了屎一样。利用这春节几天时间,尝试着按自己思路去解决这个问题,造个流式的轮子,于是写了Flow小框子。

想法

从生活中思考代码,方法嵌套和水流的原理很相似,我们把每个异步当成一个水管,水从一个个管道流过,每个管道可以对水进行加工转换。转换的这个过程我们当成一个事件Event。在包装事件中,我们可以对它进行线程转换,事件转换,合并拆分等一系列转换。如果碰到异常,则直接终止这个流。

功能

简单使用

通过Flow 静态create方法创建一个流,then串联下个流,如果不需要返回Void泛型。Event有两个泛型P、R,第一个是前个流Flow的返回值类型,第二个是当前流Flow返回类型。await exec方法是结束当前事件流,并将结果代入下个流。

打印两句话

 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接口来实现其他功能。

 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流(返回参数可以不一样)

 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流,可以合并到一起。(返回参数必须一致)

 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就几乎一样了。后续根据实际需求再做调整,试验中。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI