[rxjs] Demystifying Cold and Hot Observables in RxJS
Cold:
console.clear(); var Observable = Rx.Observable; var clock = Observable.interval(1000).take(10).map((i) => `${i}!`); clock.subscribe((x) => { console.log(` a ${x}`); }); setTimeout(function(){ clock.subscribe((x) => { console.log(` b ${x}`); }); }, 3500);
Results:
/* " a 0!" " a 1!" " a 2!" " a 3!" " b 0!" " a 4!" " b 1!" " a 5!" " b 2!" " a 6!" " b 3!" " a 7!" " b 4!" " a 8!" " b 5!" " a 9!" " b 6!" " b 7!" " b 8!" " b 9!" */
As you can see, 'a' and 'b' all start from '0'. They are independent. As youtube vedio, you can open the same vedio in tow tabs. When you click play, those two vedio will play independently.
Hot: publish().refCount();
Hot Observables are like 'live' youtube video, everyone watch the same vedio at the same pace.
As I wrote in previous article about publish(); you can use this with connect() funciton, but there is problem, we will miss the very first event.
RefCount and a hot observable is analogous to a live video of a band playing at a concert, but the band doesn't start playing if there isn't anyone in the audience. That would be a waste, right? So, why play if there is no one watching?
RefCount tells the band to play when there is at least one person in the audience, in other words, when the number of observers goes from zero to one.
console.clear(); var Observable = Rx.Observable; var clock = Observable.interval(1000).take(10).map((i) => `${i}!`).publish().refCount(); clock.subscribe((x) => { console.log(` a ${x}`); }); setTimeout(function(){ clock.subscribe((x) => { console.log(` b ${x}`); }); }, 3500);
Results:
/*" a 0!" " a 1!" " a 2!" " a 3!" " b 3!" " a 4!" " b 4!" " a 5!" " b 5!" " a 6!" " b 6!" " a 7!" " b 7!" " a 8!" " b 8!" " a 9!" " b 9!" */
相关文章
- 快速搭建Fabric测试网络(Docker in Ubantu 18.04 TLS)
- How to Use the Stdin, Stderr, and Stdout Streams in Bash
- 把Transformer当通用计算机用,还能执行in-context learning算法,这项研究脑洞大开
- ORA-01719: outer join operator (+) not allowed in operand of OR or IN ORACLE 报错 故障修复 远程处理
- ORA-01878: specified field not found in datetime or interval ORACLE 报错 故障修复 远程处理
- ORA-29529: invalid function or method call string in trigger string ORACLE 报错 故障修复 远程处理
- ORA-30948: Illegal in-place XML schema evolution operation: string ORACLE 报错 故障修复 远程处理
- ORA-00244: concurrent control file backup operation in progress ORACLE 报错 故障修复 远程处理
- ORA-01719: outer join operator (+) not allowed in operand of OR or IN ORACLE 报错 故障修复 远程处理
- ORA-02482: Syntax error in event specification (string) ORACLE 报错 故障修复 远程处理
- ORA-06595: REF CURSOR parameters are not supported in forwarded RPC calls ORACLE 报错 故障修复 远程处理
- ORA-14283: UNIQUE constraints mismatch in ALTER TABLE EXCHANGE SUBPARTITION ORACLE 报错 故障修复 远程处理
- ORA-16659: failover operation in progress ORACLE 报错 故障修复 远程处理
- MySQL删除IN操作: 简易指南(mysql删除in)
- MySQL OR和IN:比较和选择(mysqlor和in)
- Unlocking the Power of cflags in Linux Systems(cflagslinux)
- MySQL存储过程使程序开发变得更快更简单(mysql存储过程in)
- Linux 中 in 命令操作指南(linuxin命令使用)
- MySQL中的IN运算符技巧(mysql查in)
- 方法掌握MySQL中IN方法的威力(mysql的in)
- Bulk Renaming Files Easily In Linux(批量重命名linux)
- 深入Linux内核:IN后缀操作系统之旅(linux系统in后缀)
- 查询解锁Oracle多条件In查询的机密(oracle多条件in)
- 的运算符mySQL中常用运算符in的变体not inanysomeall,操作需要注意(mySQL中与in相似)
- MySQL使用IN查询时数量超限问题的解决方法(mysql中in超过数量)
- MySQL中的IN查询的性能优化(mysql中in性能)
- 条件在Oracle中使用IN和OR查询条件的巧妙之处(oracle中in跟or)
- istsoracle中使用in和exists查询语句的比较(oracle in和ex)
- acle joinOracle IN与Oracle Join的差异探讨(oracle in与or)
- 遭遇php的in_array低性能问题