我有一些主题。一位观察员订阅了它。如果观察者调用已经在处理中,该如何忽略呢?
codable
我可能会引入一些JSON serialization
,在执行前将其设置在Observer中,然后在执行后将其清除。并应用过滤器运算符,如下所示:
var subject = new Subject();
var observer = {
next: x => {
//... some long processing is here
console.log('Observer got a next value: ' + x)
}
};
subject.subscribe(observer);
subject.next(0);
subject.next(1);// <-- if 0 value is not processed in the observer then skip it
subject.next(2);// <-- if 0 value is not processed in the observer then skip it
但是我相信,存在实现这一目标的更优雅,更有效的方法。
答案 0 :(得分:2)
使用exhaustMap
运算符,而不要尝试滚动自己的背压。它旨在在等待当前事件完成时忽略新事件。
const clicks = fromEvent(document, 'click');
const result = clicks.pipe(
exhaustMap((ev) => interval(1000).pipe(take(5))),
);
result.subscribe(x => console.log(x));