.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. Kali的源得数字验证问题

    装上之后第一件事就是执行apt-get update && apt-get upgrade,结果却出现了这样的错误 我添加的是中科大的更新源,在浏览器中是可以正常打开的: deb ht ...

  2. 今日SGU 5.12

    SGU 149 题意:求每一个点的距离最远距离的点的长度 收获:次大值和最大值,dfs #include<bits/stdc++.h> #define de(x) cout<< ...

  3. 小的时候.by小雷

    小的时候,总是有很多想法.   想去做,却做不成.   因为,自己小,被父母约束着,被学校圈着,被老师教育着. 想买个小霸王游戏机,没钱.在父辈的眼中,"游戏" ,游戏室,电脑游戏 ...

  4. [Javascript] Classify JSON text data with machine learning in Natural

    In this lesson, we will learn how to train a Naive Bayes classifier and a Logistic Regression classi ...

  5. error while loading shared libraries: libpcre.so.0的解决办法

    error while loading shared libraries: libpcre.so.0的解决办法 http://blog.csdn.net/xjkwq1qq/article/detail ...

  6. 洛谷P1852 奇怪的字符串

    题目描述 输入两个01串,输出它们的最长公共子序列的长度 输入输出格式 输入格式: 一行,两个01串 输出格式: 最长公共子序列的长度 输入输出样例 输入样例#1: 复制 01010101010 00 ...

  7. golang 方法内部定义子方法及调用

    package main import ( "fmt" "reflect" ) func out(ch chan int) { <-ch fmt.Prin ...

  8. 福建省第八届 Triangles

    Problem Description This is a simple problem. Given two triangles A and B, you should determine they ...

  9. var、let和const的区别

    var 首先var有变量提升 console.log(a); // undefined var a = 1; function也存在提升现象 console.log(b); //function b( ...

  10. ubuntu-工作环境配置(van)

    我们平时用到的工具主要vim,retag_app,提交代码,以及一些常用命令,他们主要对应一下几个文件 1.vim ->.vimrc 2.代码提交->gitconfig 3.常用命令-&g ...