当前位置:网站首页>Rxjs TakeUntil 操作符的学习笔记
Rxjs TakeUntil 操作符的学习笔记
2022-06-25 09:35:00 【51CTO】
TakeUntil 的官方文档对这个操作符的解释是:
Emit values until provided observable emits.
即它可以被赋予另一个起锚定作用的 Observable,当该锚定 Observable emit 值时,原始的 Observable 就停止发射值,进入 complete 操作。
看一个实际的例子:
source Observable 每个1秒的时间间隔,发射一个从 0 开始递增间隔为 1 的整数序列。
我们构造了一个 timer Observable,超时时间间隔为 5 秒,也就是说,在第五秒时,该 Observable 会发送一个值。这个 timer Observable 传入 takeUntil,作为一个 notification Observable,五秒钟之后,source Observable 就会停止发射整数。
最后上述程序执行的输出:4 秒之内打印 0~4,然后结束。

另一个例子:
这个例子里,interval 作为原始的 Observable,clicks 作为 notification Observable,整个程序的表现形式是,每个 1 秒有一个递增1的整数序列打印,直至页面发生点击事件时,原始 interval Observable 终止。
看另一个例子:
我们逐行分析这个例子的逻辑:
产生一个每隔1秒发射一个偶数的 Observable.
对产生的偶数的个数进行累加。
当产生的偶数个数大于 5 时,发射值。这个 Observable 作为 takeUntil 的 notification Observable 使用。
- 使用 eventSource 和 eventNumberCount,通过
withLatestFrom将两个 Observable 进行连接,从而在 map Operator 里,可以同时打印出当前发射的偶数值和偶数总量。通过 takeUntil 传入一个只有在偶数总数个数大于 5 时才发射值的 Observable,可以做到偶数总数大于 5 之后,让 interval 停止值的发送。
最后的执行效果:

边栏推荐
- [zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
- sklearn PolynomialFeatures的具体用法
- Explanation of assertions in JMeter
- Matplotlib axvline() and axhline() functions in Matplotlib
- 在指南针上面开户好不好,安不安全?
- 匯付國際為跨境電商賦能:做合規的跨境支付平臺!
- Fcpx quickly add subtitles | Final Cut Pro import fcpxml subtitle file does not match the video time? I got it in code
- Is it safe to open a stock account through the account opening QR code of the account manager?
- How do dating applets make millions a year? What is the profit model?
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
猜你喜欢

Voiceprint Technology (VI): other applications of voiceprint Technology

neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
![[final review notes] digital logic](/img/57/4431208b1c8b653f5c3f4ff4212cc3.png)
[final review notes] digital logic

Explanation of assertions in JMeter

Summarize two methods of configuring pytorch GPU environment

Compile time annotations for custom annotations (retentionpolicy.class)

Mapping mode of cache

Specific usage of sklearn polynomialfeatures

Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.

Atguigu---17-life cycle
随机推荐
How do dating applets make millions a year? What is the profit model?
2021mathorcupc topic optimal design of heat dissipation for submarine data center
Abbreviations of common English terms for IC R & D
[opencv] - input and output XML and yaml files
Creating a binary tree (binary linked list) from a generalized table
neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
Socket programming -- poll model
Analysis on the bottom calling process of micro service calling component ribbon
CSV parameterization in JMeter
微服务调用组件Ribbon底层调用流程分析
Reasons for Meiye to choose membership system
Is it safe to open an account with Great Wall Securities by mobile phone?
[buuctf.reverse] 121-125
Is the client that gets the scanning code wechat or Alipay
On the underlying index principle of MySQL
puzzle(019.2)六边锁
Test Development Engineer
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
2022 meisai question a idea sharing
Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?