Javascript 如何缓冲最新的值,直到rxjs以另一个序列到达另一个值?

Javascript 如何缓冲最新的值,直到rxjs以另一个序列到达另一个值?,javascript,typescript,rxjs5,Javascript,Typescript,Rxjs5,我试图在我的项目中使用rxjs。我有以下序列,我所期望的是1rd序列只有在一个值到达另一个序列后才会被处理,并且只有第一个序列中的最新值才会被保留。有什么建议吗 s1$ |a---b----c- s2$ |------o---- 预期结果: s3$ |------b--c- 最后一次+吃一次就行了 以下是一个例子: let one$ = Rx.Observable.interval(1000); let two$ = Rx.Observable.timer(5000, 1000).mapT

我试图在我的项目中使用rxjs。我有以下序列,我所期望的是1rd序列只有在一个值到达另一个序列后才会被处理,并且只有第一个序列中的最新值才会被保留。有什么建议吗

s1$ |a---b----c-

s2$ |------o----
预期结果:

s3$ |------b--c-

最后一次+吃一次就行了

以下是一个例子:

let one$ = Rx.Observable.interval(1000);
let two$ = Rx.Observable.timer(5000, 1000).mapTo('stop');

one$
  .takeUntil(two$)
  .last()
  .subscribe(
     x=>console.log(x),
     err =>console.error(err),
     ()=>console.log('done')
  );

我估计我会使用
ReplaySubject

const subject$=new Rx.ReplaySubject(1)
常数一$=可观测的接收间隔(1000)
常数二$=可观测接收间隔(2500)
一美元。订阅(主题美元)
常数三$=2$
.采取(1)
.flatMap(()=>subject$)
//一美元--0--1--2--3--4---
//两美元|--------0--------1---
//三美元------------------1-2--3--4---
我会将已经非常类似于您所需和所需的功能组合起来

这将输出以
2
开头的数字

source  0-----1-----2-----3-----4
trigger ---------------X---------
output  ---------------2--3-----4
带有时间戳的控制台输出:

2535 2
3021 3
4024 4
5028 5
或者,您可以使用
switchMap()
ReplaySubject
,但这可能不像前面的示例那样明显,您需要两个主题

const start = Scheduler.async.now();
const trigger = new Subject();

const source = Observable
    .timer(0, 1000)
    .share();

const replayedSubject = new ReplaySubject(1);
source.subscribe(replayedSubject);

trigger
    .switchMap(() => replayedSubject)
    .subscribe(val => console.log(Scheduler.async.now() - start, val));

setTimeout(() => {
    trigger.next();
}, 2500);

输出完全相同。

看起来像是您想要的,但我不理解您的描述。您无法获得预期的
----|b-c-
,因为完整信号始终是最后一个。换句话说,在发送完完整的信号后,您不能发出更多的值。@martin我的描述造成了一些混乱,对此表示抱歉。这并不意味着完全信号,我更新了我的描述。我是rxjs的新手。如果我使用takeUtil,我想我会丢失b,而s1.subcribe只会在c到达后激发,对吗?流结束,不接受最新的值。
const start = Scheduler.async.now();
const trigger = new Subject();

const source = Observable
    .timer(0, 1000)
    .share();

const replayedSubject = new ReplaySubject(1);
source.subscribe(replayedSubject);

trigger
    .switchMap(() => replayedSubject)
    .subscribe(val => console.log(Scheduler.async.now() - start, val));

setTimeout(() => {
    trigger.next();
}, 2500);