在 RxJS 中合并主题,类似于 zip

Luk*_*sen 5 javascript rxjs typescript angular

我想以与 zip 类似的方式组合两个主题,但有以下重要区别。

\n

我想要一个以这种方式结合两个主题的可观察量。当两者都有发射值时,发射它们的最新值。然后,两者都需要至少再次发射一次,然后发射它们最新的发射值。ETC。

\n

两个科目:

\n

1 ---------- 2 ----- 3 -- 4 ---------------------- 5 \xe2\x80\x94\xe2\ x80\x94

\n

------- a ---------------------- b --- c \xe2\x80\x94 d \xe2\x80\x94-\xe2\x80 \x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94

\n

可观察的目标:

\n

------- 1a ----------------- 4b ---\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\ x80\x94-5d\xe2\x80\x94-

\n

简单的例子:subject1 发射 5 次,之后 subject2 发射一次。zip 的第一个发射值是对象的两个第一个发射值的对。我想要(subj1-emit5,subj2-emit1)。

\n

Nar*_*ali -1

怎么样withLatestFrom

import './style.css';

import { rx, of, tap, interval, withLatestFrom } from 'rxjs';
const obs1$ = interval(1000);
const obs2$ = interval(5000);

obs2$
  .pipe(
    withLatestFrom(obs1$),
    tap(([first, second]) => {
      console.log(`second Source (5s): ${first} first Source (1s): ${second}`);
    })
  )
  .subscribe();

// Open the console in the bottom right to see results.
Run Code Online (Sandbox Code Playgroud)

堆栈闪电战