多个 RXJS BehaviorSubjects 触发函数调用
Multiple RXJS BehaviorSubjects to trigger function call
我想要 运行 一个依赖于 3 个 behaviorSubjects 的最新值的计算密集型函数。
有时所有科目同时更改,我不想 运行 计算 3 次。
这就是我目前的实现方式。
this.subscription = merge(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$)
.pipe(throttleTime(300)) //this is to avoid running the function 3 times when all subjects change simultaneously
.subscribe(() => {
const answer = getSolution(subject1$.getValue(), subject2$.getValue(), subject3$.getValue());
});
我不确定这是否是最好的解决方法。非常感谢任何帮助
在这种情况下最好使用 combineLatest() 而不是 merge()
:
this.subscription = combineLatest([behaviorSubject1$, behaviorSubject2$, behaviorSubject3$])
.pipe(throttleTime(300))
.subscribe(([value1, value2, value3]) => {
const answer = getSolution(value1, value2, value3);
});
取决于你想做什么
要根据最新值计算吗?
那么你的方法没问题,但是改用 combineLatest,任何发射都会导致计算。
this.subscription = combineLatest(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$)
.pipe(throttleTime(300))
.subscribe(([o1, o2, o3]) => {
const answer = getSolution(o1, o2, o3);
});
你想一一计算吗?
使用 concatMap。
this.subscription = combineLatest(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$).pipe(
concatMap(([o1, o2, o3]) => getSolution(o1, o2, o3)), // getSolution should be an observable.
)
.subscribe(solution => {
});
你想在计算过程中忽略发射吗?
使用 exhaustMap。
this.subscription = combineLatest(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$).pipe(
exhaustMap(([o1, o2, o3]) => getSolution(o1, o2, o3)), // getSolution should be an observable.
)
.subscribe(solution => {
});
是否要计算所有 3 个都已更改的时间?
使用 zip.
this.subscription = zip(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$)
.subscribe(([o1, o2, o3]) => {
const answer = getSolution(o1, o2, o3);
});
我想要 运行 一个依赖于 3 个 behaviorSubjects 的最新值的计算密集型函数。 有时所有科目同时更改,我不想 运行 计算 3 次。 这就是我目前的实现方式。
this.subscription = merge(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$)
.pipe(throttleTime(300)) //this is to avoid running the function 3 times when all subjects change simultaneously
.subscribe(() => {
const answer = getSolution(subject1$.getValue(), subject2$.getValue(), subject3$.getValue());
});
我不确定这是否是最好的解决方法。非常感谢任何帮助
在这种情况下最好使用 combineLatest() 而不是 merge()
:
this.subscription = combineLatest([behaviorSubject1$, behaviorSubject2$, behaviorSubject3$])
.pipe(throttleTime(300))
.subscribe(([value1, value2, value3]) => {
const answer = getSolution(value1, value2, value3);
});
取决于你想做什么
要根据最新值计算吗? 那么你的方法没问题,但是改用 combineLatest,任何发射都会导致计算。
this.subscription = combineLatest(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$)
.pipe(throttleTime(300))
.subscribe(([o1, o2, o3]) => {
const answer = getSolution(o1, o2, o3);
});
你想一一计算吗? 使用 concatMap。
this.subscription = combineLatest(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$).pipe(
concatMap(([o1, o2, o3]) => getSolution(o1, o2, o3)), // getSolution should be an observable.
)
.subscribe(solution => {
});
你想在计算过程中忽略发射吗? 使用 exhaustMap。
this.subscription = combineLatest(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$).pipe(
exhaustMap(([o1, o2, o3]) => getSolution(o1, o2, o3)), // getSolution should be an observable.
)
.subscribe(solution => {
});
是否要计算所有 3 个都已更改的时间? 使用 zip.
this.subscription = zip(behaviorSubject1$, behaviorSubject2$, behaviorSubject3$)
.subscribe(([o1, o2, o3]) => {
const answer = getSolution(o1, o2, o3);
});