點我啊,代碼在這里

為宣漢等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務,及宣漢網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務為成都做網(wǎng)站、網(wǎng)站設(shè)計、宣漢網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
原因
在code代碼中,我們經(jīng)常碰到異步方法嵌套。比如提交文件之后在提交表單,提交數(shù)據(jù)根據(jù)是否成功然后做出其他邏輯處理。kotlin里面提出協(xié)程概念,利用語法糖來解決這個問題。在javaScript里面也有async/await來使異步用起來像同步。而在java中我暫時沒有找到該特性,使得寫起來異步嵌套感覺就是地獄,像吃了屎一樣。利用這春節(jié)幾天時間,嘗試著按自己思路去解決這個問題,造個流式的輪子,于是寫了Flow小框子。
想法
從生活中思考代碼,方法嵌套和水流的原理很相似,我們把每個異步當成一個水管,水從一個個管道流過,每個管道可以對水進行加工轉(zhuǎn)換。轉(zhuǎn)換的這個過程我們當成一個事件Event。在包裝事件中,我們可以對它進行線程轉(zhuǎn)換,事件轉(zhuǎn)換,合并拆分等一系列轉(zhuǎn)換。如果碰到異常,則直接終止這個流。
功能
簡單使用
通過Flow 靜態(tài)create方法創(chuàng)建一個流,then串聯(lián)下個流,如果不需要返回Void泛型。Event有兩個泛型P、R,第一個是前個流Flow的返回值類型,第二個是當前流Flow返回類型。await exec方法是結(jié)束當前事件流,并將結(jié)果代入下個流。
打印兩句話
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();
兩數(shù)相加
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方法返回是當前流的結(jié)果,每個flow后面使用resultThen都可以獲取流的結(jié)果。如果遇到異常,可以通過flow throwException方法拋出,可以在flow后面catchThen立刻處理,也可以在最后flow catchThen處理。finallyThen是事件流結(jié)束一個通知。
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參數(shù),代表下個流切換。如果兩個Converter參數(shù),代表當前流和下個流都切換線程。當然你也可以實現(xiàn)Converter接口來實現(xiàn)其他功能。
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結(jié)果轉(zhuǎn)換成多個流
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();
多個流結(jié)果轉(zhuǎn)換成一個流
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();
條件選擇
根據(jù)條件判斷重新發(fā)起Flow流(返回參數(shù)可以不一樣)
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();
根據(jù)條件判斷執(zhí)行Flow流,可以合并到一起。(返回參數(shù)必須一致)
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方法。被觀察者必須實現(xiàn)ILifeObservable接口。
Flow.create((FirstEvent<Integer>) (flow, await) ->await.exec(0)) .watch(this).start();
總結(jié)
框子也里面提供了一些簡化的類,也可以和項目網(wǎng)絡(luò)請求框架抽象自己的Event,這樣和js的網(wǎng)絡(luò)的then就幾乎一樣了。后續(xù)根據(jù)實際需求再做調(diào)整,試驗中。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享文章:如何造個androidFlow流式響應的輪子
網(wǎng)頁鏈接:http://www.chinadenli.net/article46/iiodhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、外貿(mào)建站、網(wǎng)站導航、云服務器、網(wǎng)頁設(shè)計公司、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)