【问题标题】:RxJs Conditional Debounce and SamplingRxJs 条件去抖动和采样
【发布时间】:2019-03-31 18:27:34
【问题描述】:

根据来自其他流的值有条件地向流添加去抖动时间

const configuration$ = new Subject().asObservable();
const animation$ = new BehaviorSubject(false).asObservable;

以上来自一些服务

configuration$.pipe(debounceTime(CONSTANTS.DEBOUNCE),sample(interval(CONSTANTS.SAMPLE)));

configuration.subscribe(data=> {
   // do the stuff; 
});


如果 animation$ 具有真值,则应跳过 debounceTimesample

如何从动画$ 中提取值并应用 if else 逻辑。

如果我能做到的话

 configuration$.pipe(
    animation$ ? 
    pipe(debounceTime(CONSTANTS.DEBOUNCE),sample(interval(CONSTANTS.SAMPLE))) :
    of
);

【问题讨论】:

  • 我认为您要的是在debounceTime 之前将animation$ 添加到流中?
  • 如果animation$有真值那么就不要去抖动
  • 啊,我倒过来了。我在下面修改了我的答案

标签: angular rxjs rxjs-pipeable-operators


【解决方案1】:
configuration$.pipe(
  withLatestFrom(animation$),
  filter((stream) => !stream[1]),

  // now the rest of the stream will only execute if animation$ emits true
  debounceTime(CONSTANTS.DEBOUNCE),
  sample(interval(CONSTANTS.SAMPLE)),
  map(stream=>stream[0])
);

configuration.subscribe(data=> {
   // do the stuff; 
});

【讨论】:

  • 感谢您的回答,我编辑了您的答案以在最后使用地图。
  • 如果动画为假,则应应用去抖动,否则跳过去抖动
  • 这是不正确的,配置流应该基于animation$流去抖动
  • 那我不明白你的问题。所以 1) debounceTime 如果 animation$ 流返回 false 则不应执行 2) debounceTime 应在 animation$ 流返回 true 时执行 3) sample 应该始终执行 我理解正确吗?
  • 如果 animation$ 为 true debounceTime & sample 不应执行,如果 animation$ 为 false 则同时执行 debounceTime & sample
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 2021-02-20
  • 2021-04-14
  • 1970-01-01
相关资源
最近更新 更多