[RxJS] Hot Observable, by .share()
.share() is an alias for .publish().refCount().
So if the source is not yet completed, no matter how many subscribers subscribe to the source, they share the same source.
const clock$ = Rx.Observable.interval().share().take(); const randomNum$ = clock$
.map(i => Math.random() * ).share(); const smallNum$ = randomNum$
.filter(x => x <= )
.toArray(); const largeNum$ = randomNum$
.filter(x => x > )
.toArray(); randomNum$.subscribe(x => console.log('random: ' + x));
smallNum$.subscribe(x => console.log('small:', x));
largeNum$.subscribe(x => console.log('large:', x)); /* Console Run Clear
"random: 49.87840398986816"
"random: 75.01024609865293"
"random: 32.59613439667008"
"random: 63.4234109489461"
"random: 35.58020574147034"
"random: 74.94599860014348"
"small:"
[49.87840398986816, 32.59613439667008, 35.58020574147034]
"large:"
[75.01024609865293, 63.4234109489461, 74.94599860014348]
*/
It is important to know share the same source is only before the source stream completed, if it is already completed, then the new subscribers will trigger another source running.
For example, in the code, we change largeNum$ happens after 4s of first subscriber.
setTimeout(() => largeNum$.subscribe(x => console.log('large:', x)), )
Because source will complete after 3s, therefor it will trigger a new source:
/*
"random: 74.91154828671043"
"random: 10.964684522348733"
"random: 29.076967396825903"
"random: 20.070493440627235"
"random: 22.44421045844409"
"random: 14.233614544120798"
"small:"
[10.964684522348733, 29.076967396825903, 20.070493440627235, 22.44421045844409, 14.233614544120798]
"large:"
[93.04544926644354, 65.4090612653734, 67.15475480984114]
*/
As we can see, in the large array, all the numbers are not from random we log out before.
[RxJS] Hot Observable, by .share()的更多相关文章
- Angular学习笔记—RxJS与Observable(转载)
1. Observable与观察者模式的关系 其实这里讲的Observable就是一种观察者模式,只不过RxJS把Observable结合了迭代模式以及附件了很多的operator,让他变得很强大,也 ...
- [RxJS] Creating Observable From Scratch
Get a better understanding of the RxJS Observable by implementing one that's similar from the ground ...
- [RxJS] Using Observable.create for fine-grained control
Sometimes, the helper methods that RxJS ships with such as fromEvent, fromPromise etc don't always p ...
- ng-packagr 打包报错 Public property X of exported class has or is using name 'Observable' from external module “/rxjs/internal/Observable” but cannot be named
old import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; @Injectable( ...
- [RxJS] Sharing Streams with Share
A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...
- Rxjs 修改Observable 里的值
有这么一个对象c$: Observable<any> 修改里边的值: 声明一个subject subject: Subject<any>; 在ngOnInit()中进行初始化 ...
- RxJS——可观察的对象(Observable)
可观察的(Observable) 可观察集合(Observables)是多值懒推送集合.它们填补了下面表格的空白: SINGLE MULTIPLE Pull Function Iterator Pus ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- RxJS速成 (上)
What is RxJS? RxJS是ReactiveX编程理念的JavaScript版本.ReactiveX是一种针对异步数据流的编程.简单来说,它将一切数据,包括HTTP请求,DOM事件或者普通数 ...
随机推荐
- 今日SGU 5.9
SGU 297 题意:就是求余数 收获:无 #include<bits/stdc++.h> #define de(x) cout<<#x<<"=" ...
- Fragment-按返回键程序退出
今天在做fragment的时候,发现一个问题,当我的界面停留在fragment的时候,按物理返回键,这时候会推出整个应用.这当然不是我们期望的,我们期望按返回键以后,应用界面返回添加fragment之 ...
- element-UI实现el-table-column百分比自定义分配
1.把el-table-column的属性width换位min-width就支持百分比显示了.
- erroe:plot.new() : figure margins too large
使用R时多次出现这个错误,plot.new() : figure margins too large,提示图片边界太大 解决方法,win.graph(width=4.875, height=2.5,p ...
- vue中添加favicon
基于vue-cli 2 首先将favicon.ico图片放在根目录下,通过以下两种方法使其显示正确. 方法一:修改index.html文件 <link rel="shortcut ic ...
- linux创建新用户并给予root权限
root比windows的系统管理员的能力更大,足以把整个系统的大部分文件删掉,导致系统完全毁坏,不能再次使用.所以,用root进行不当的操作是相当危险的,轻微的可以死机,严重的甚至不能开机.所以,在 ...
- windows服务插件利器-新生命组件XAgent使用心得
1.简单介绍 XAgent为大石头带领下的新生命团队自己开发的一个.Net下的常用的Windows服务管理组件利器,通过在控制台中简单的输入1,2,3,4,5等数字可以实现一步安装.卸载Windows ...
- windows系统自带工具
辅助功能向导:单击"開始→执行",在弹出的对话框中输入:accwiz 计算器:单击"開始→执行",在弹出的对话框中输入:calc 字符影射表:单击"開 ...
- amazeui学习笔记二(进阶开发2)--Web组件简介Web Component
amazeui学习笔记二(进阶开发2)--Web组件简介Web Component 一.总结 1.amaze ui:amaze ui是一个web 组件, 由模板(hbs).样式(LESS).交互(JS ...
- 6 Spring Boot 静态资源处理
转自:https://blog.csdn.net/catoop/article/details/50501706