首页 > 其他分享 >[RxJS] ShareReplay vs share

[RxJS] ShareReplay vs share

时间:2022-09-26 13:33:25浏览次数:41  
标签:const log ShareReplay share ajax vs shareReplay console

ShareReplay is using ReplaySubject. It will reply the messages to later subscribers. It turns unicast observable to multicase observable.

shareReplay(1, 2000): 1: replay lastest value, 2000: 2 second, replay value only if in 2s

import { fromEvent } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { shareReplay, mergeMapTo, share } from 'rxjs/operators';

const observer = {
  next: val => console.log('next', val),
  error: err => console.log('error', err),
  complete: () => console.log('complete')
};

const click$ = fromEvent(document, 'click');
const ajax$ = ajax('https://api.github.com/users/octocat');

/*
 * shareReplay turns a unicast observable into multicasted
 * observable, utilizing a ReplaySubject behind the scenes.
 * 
 * In this example, we are mapping any clicks to an ajax
 * request, sharing the response.
 */
const sharedClick$ = click$.pipe(
  mergeMapTo(ajax$),
  /*
   * By default shareReplay shares all old values, like
   * a standard ReplaySubject. In this case, we only want
   * to share the most recent response.
   */
  shareReplay(1)
);

sharedClick$.subscribe(observer)

/*
 * Late subscribers will be replayed old value(s).
 */
setTimeout(() => {
  console.log('subscribing');
  sharedClick$.subscribe(observer);
}, 5000);

 

If you don't need replay functionality, you can use share() instead

标签:const,log,ShareReplay,share,ajax,vs,shareReplay,console
From: https://www.cnblogs.com/Answer1215/p/16730560.html

相关文章

  • [转]Dockerfile:ADD VS COPY (结论:建议都使用COPY)
    这篇博文将帮助您理解两个类似的Dockerfile指令(ADD和COPY)之间的区别,以及它们如何成为现在的样子,以及我们对您应该使用哪条指令的建议。(提示:不是ADD)从Dockerfile构建Docke......
  • vscode 打包报错 error code ELIFECYCLE error This is probably not a problem wi
    执行命令:npmrunbuild详细报错信息:0infoitworkedifitendswithok1verbosecli[1verbosecli'C:\\ProgramFiles(x86)\\nodejs\\node.exe',1verbose......
  • texLive+VsCode
    首先进入镜像网站下载texLive卸载方式(C:/texlive/2022/tlpkg/pkg/installer/uninst.bat管理员身份运行)下载:运行驱动中的install-tl-windows.bat,可以修改安装位置,其他默......
  • 更便捷的VsCode
    所有都可以在设置中的键盘快捷方式查看查看->编辑器布局->拆分:可以同时对同一个文件进行编辑,用于文件较长不便查看时使用。同时对相同的名字更改多处位置:选中一......
  • R、01 VSCODE 配置 R 环境快速指南、4.2.1版本
    安装最新版R-4.2.1R:TheRProjectforStatisticalComputing(r-project.org)有大量镜像供选择下载,找中国地区镜像下载会快一点。安装一口气Next到底。https://cran......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 实验2_Open vSwitch虚拟交换机实践
    一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的Open......
  • 实验2:Open vSwitch虚拟交换机实践
    一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的......
  • 实验2:Open vSwitch虚拟交换机实践
    (一)基本要求a)/home/用户名/学号/lab2/目录下执行ovs-vsctlshow命令、以及p0和p1连通性测试的执行结果截图;b)/home/用户名/学号/lab2/目录下开启MininetCLI并执行pi......
  • 实验2:Open vSwitch虚拟交换机实践
    三、实验报告3.1基础要求提交a)/home/用户名/学号/lab2/目录下执行ovs-vsctlshow命令、以及p0和p1连通性测试的执行结果截图;b)/home/用户名/学号/lab2/目录下开启M......