我想组合两个观察量,取每一个的最新值(combineLatest),并根据哪个是最新emits 的观察量应用不同的函数.大概是这样的:

public obs1: BehaviorSubject<int> = ...;
public obs2: Observable<string> = ...;

(...)

this.desiredObservable = combineLatest([this.obs1, this.obs2],
  (myInt, myString) => {
    if (this.obs1 was the latest to emit) {
        return func1(myInt, myString);
    }  else {
        return func2(myInt, myString);
    }
});

我找到了this possible solution个,但我想知道有没有更简单的方法.

推荐答案

timestamp operator是实现这一目标的最佳 Select :

import { combineLatest, timer, timestamp } from 'rxjs';

const obs1$ = timer(0, 100).pipe(timestamp());
const obs2$ = timer(0, 10).pipe(timestamp());

const obs$ = combineLatest([obs1$, obs2$], (ts1, ts2) => {
  if (ts1.timestamp > ts2.timestamp) {
    ts1.value; // obs1$ is more recent
  } else {
    ts2.value; // obs2$ is more recent
  }
});

Angular相关问答推荐

根据Angular中的构建来卸载独立组件

停靠的Angular 应用程序的Nginx反向代理无法加载assets资源

Rxjs重置执行后的可观察延迟并重新调度Angular

更改物料设计中的复选框勾选 colored颜色

成Angular 的嵌套router-outlet

SignalR:协商有效,但消息未发送(.NET/Angular)

路由链接不在 Ionic 中执行

需要做什么才能使 ErrorHandler 正常工作?

如何更改 Angular material 按钮中的内部 RippleColor 和 UnboundedRipple 效果?

如何访问父组件中提供的 TemplateRef 内的服务?

刷新页面导致 401 错误-Angular 8

如何以像素为单位将属性绑定到 style.width?

在 Ionic 2 中使用 NodeJS.Timer 时找不到命名空间 NodeJS

如何测试 Angular2 的 router.navigate?

如何在angular 5的组件中格式化日期

如何手动延迟加载模块?

`[(ngModel)]` vs `[(value)]`

Angular 6在输入键上添加输入

@angular/platform-b​​rowser 与 @angular/platform-b​​rowser-dynamic

Angular 5 中 value 和 ngValue 的区别