.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()的更多相关文章

  1. Angular学习笔记—RxJS与Observable(转载)

    1. Observable与观察者模式的关系 其实这里讲的Observable就是一种观察者模式,只不过RxJS把Observable结合了迭代模式以及附件了很多的operator,让他变得很强大,也 ...

  2. [RxJS] Creating Observable From Scratch

    Get a better understanding of the RxJS Observable by implementing one that's similar from the ground ...

  3. [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 ...

  4. 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( ...

  5. [RxJS] Sharing Streams with Share

    A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...

  6. Rxjs 修改Observable 里的值

    有这么一个对象c$: Observable<any> 修改里边的值: 声明一个subject subject: Subject<any>; 在ngOnInit()中进行初始化 ...

  7. RxJS——可观察的对象(Observable)

    可观察的(Observable) 可观察集合(Observables)是多值懒推送集合.它们填补了下面表格的空白: SINGLE MULTIPLE Pull Function Iterator Pus ...

  8. angular2 学习笔记 ( rxjs 流 )

    RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅,  ...

  9. RxJS速成 (上)

    What is RxJS? RxJS是ReactiveX编程理念的JavaScript版本.ReactiveX是一种针对异步数据流的编程.简单来说,它将一切数据,包括HTTP请求,DOM事件或者普通数 ...

随机推荐

  1. ATcoderARC100D Equal Cut

    ARC100 D - Equal Cut Description: 给出长度为n的序列A,把这个序列分成连续的四段,最小化极差. \(4≤n≤2×10^5,4≤n≤2×10^5\) Solution: ...

  2. PatentTips - Transparent unification of virtual machines

    BACKGROUND Virtualization technology enables a single host computer running a virtual machine monito ...

  3. 洛谷 P1358 扑克牌

    P1358 扑克牌 题目描述 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的存在.计数以及构造等方面的问题.组合数学的主要内容有组合计数.组合设 ...

  4. [Python] Python's namedtuples can be a great alternative to defining a class manually

    # Why Python is Great: Namedtuples # Using namedtuple is way shorter than # defining a class manuall ...

  5. js32---CommonUtil.js

    // BH 命名空间 namespace var BH = {} ; BH.Interface = function(name,methods){ //Interface是类.方法的名字,以后用BH. ...

  6. STM32介绍以及与通常ARM的区别

    ARM是英国的芯片设计公司,其最成功的莫过于32位嵌入式CPU核----ARM系列,最常用的是ARM7和ARM9,ARM公司主要提供IP核,就是CPU的内核结构,只包括最核心的部分,并不是完整的处理器 ...

  7. mahout-distribution-0.9.tar.gz的安装的与配置、启动与运行自带的mahout算法

    不多说,直接上干货! 首先,别在windows下搭建什么,安装什么Cygwin啊!直接在linux,对于企业里推荐用CentOS6.5,在学校里用Ubuntu. Mahout安装所需软件清单: 软件 ...

  8. BZOJ3672: [Noi2014]购票(CDQ分治,点分治)

    Description  今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会.        全国的城市构成了一棵以SZ市为根的有根树 ...

  9. ios 获取手机信息(UIDevice、NSBundle、NSLocale)

    iOS的SDK中提供了UIDevice.NSBundle,NSLocale. UIDevice        UIDevice提供了多种属性.类函数及状态通知,帮助我们全方位了解设备状况. 从检測电池 ...

  10. C语言速度优化之指针赋值与if推断

    近期在写的一个项目须要优化处理速度,我写了一下程序来測试指针赋值与指针推断的速度比較.结果让我大吃一惊. #include <stdio.h> #include <stdlib.h& ...