[英]How can I buffer an observable by a simple in Rxjs?
I would like to buffer an observable by some arbitrary (but simple) criterion. 我想通过一些任意(但简单)的准则来缓冲一个可观察对象。 I've set up a simple example here:
我在这里建立了一个简单的示例:
const observable = Rx.Observable.from([1,2,3])
const filtered = observable.filter((n) => n === 3);
observable
.buffer(filtered)
.subscribe((n) => {
// Why is this empty?
console.log(n);
});
Trying to do this with filter
only produces an empty array. 尝试使用
filter
只能产生一个空数组。 I am expecting an array of [1,2,3]
, but that seems to be not how it works. 我期望一个
[1,2,3]
数组,但这似乎不是它的工作方式。 All the documentation for buffer uses asynchronous events like timer, but this isn't what I want. 所有有关缓冲区的文档都使用异步事件(如计时器),但这不是我想要的。 I would simply like to take the last n items based on some arbitrary criterion that I decide.
我只想根据我决定的任意标准来获取最后n个项目。
Help is much appreciated! 非常感谢帮助!
Your subscription comes after the values are emitted. 您的订阅是在值发出之后进行的。 If you want it to work:
如果您希望它起作用:
const observable = new Rx.Subject()
const filtered = observable.filter((n) => n === 3);
filtered
.buffer(filtered)
.subscribe((n) => {
console.log(n);
});
Rx.Observable.from([1,2,3]).subscribe(observable);
根据olsn的评论,看来这是Rxjs 5中的错误。更改为v4可以有效解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.