当前位置:网站首页>跟我学Rx编程——Concat
跟我学Rx编程——Concat
2022-07-28 14:39:00 【我不是码神】
业务逻辑
- 游戏场景进入后,根据具体业务场景和活动进程,可能需要弹出若干的弹框,比如邀请好友获得的奖励、离线收益、瓜分大奖等等。
- 这些弹框必须逐个弹出,不能同时弹出。
常规实现
由于弹框的逻辑是异步返回,所以免不了使用回调机制
伪代码
OpenDialog1(result1=>{
OpenDialog2(result2=>{
……
})
})当然实际代码会比这个更复杂,需要更多的判断和分叉
使用Rx实现
伪代码
let dialogs = []
dialogs.push(rx.fromDialogBox(……))
dialogs.push(rx.fromDialogBox(……))
……
rx.concat(...dialogs).subscribe(……)其中fromDialogBox是自己扩展的一个事件流(Observable)
这么做的好处有几个:
- 去除了回调,可读性提升
- 可以随意改变弹框的顺序
- 可以随意的根据需求变化而去除某个弹框逻辑
- 在这个事件流数组中,可以添加其他事件流,即其他异步操作穿插在弹框序列中
- 可以动态判断已经弹出的弹框数量来决定下一步操作(实时判断dialogs.length)
let dialogs = []
if (condition1)
dialogs.push(rx.fromDialogBox(……))
if (condition2)
dialogs.push(rx.fromDialogBox(……))
……
if (dialogs.length<2)
dialogs.push(rx.fromDialogBox(……))
rx.concat(...dialogs).subscribe(……)在上述情况下,假如使用传统方式编程,可读性会很差,也更容易出现bug,你细品
边栏推荐
- 程序员的自我修养
- 19. Channel assignment task definition
- ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching
- EasyExcel复杂表头导出(一对多)
- 4. Main program and cumulative interrupt processing routine implementation code
- subst命令将一个文件夹镜像成本地的一个磁盘
- MIT pointed out that the public pre training model should not be used indiscriminately
- Flowable workflow all business concepts
- 9、相关数据累积任务定义
- The subst command mirrors a folder to a local disk
猜你喜欢

Grpc protocol buffer
![【删除指定数字——leetcode]](/img/16/b40492d8414a363a3a24f00b4afd47.png)
【删除指定数字——leetcode]

Nftscan and nftplay have reached strategic cooperation in the field of NFT data

有奖活动分享:使用WordPress搭建一个专属自己的博客后最高可领取iPhone13

DataTables warning: table id=campaigntable - cannot reinitialize datatable. solve

最小堆提升每次排序的效率

机器学习的3大“疑难杂症”,因果学习是突破口 | 重庆大学刘礼

关于word文档中插入的图片只显示下面一部分

Problems encountered by pyppeter

Celery related
随机推荐
leetcode-括号有效性问题
软件架构与设计(一)-----关键原则
Easy start, swagger
DataTables warning: table id=campaigntable - cannot reinitialize datatable. solve
软件架构与设计(七)-----互动架构
Close independent windows and close other windows at the same time
7. Definitions of real-time data backup and real-time clock
[delete specified number leetcode]
Leetcode - random set, longest multiclass subsequence
简单入手Swagger
Summary of common redis commands (self provided)
软件架构与设计(十)-----架构技术
20. Channel allocation task implementation
Daily news on July 28, 2022: Science: AI has made another breakthrough in protein design, and can design specific functional proteins
Vs dynamic library debugging
软件架构与设计(五)-----以数据为中心的架构
String (3)
Heap operation
Ffmpeg notes
Matlab does not overwrite importing Excel