gpt4 book ai didi

javascript - 如何根据 rxjs 中的缓冲区内容缓冲可观察值?

转载 作者:行者123 更新时间:2023-11-28 05:09:07 28 4
gpt4 key购买 nike

我想根据缓冲区的内容刷新缓冲的可观察对象,但如何实现这一点?我想做的事情的简化示例:

observable.buffer(() => {
// Filter based on the buffer content.
// Assuming an observable here because buffer()
// needs to return an observable.
return buffer.filter(...);
})

以下是我尝试对关键事件执行的更具体操作 (bin here ):

const handledKeySequences = ['1|2']

// Mock for document keydown event
keyCodes = Rx.Observable.from([1,2,3,4])

keyCodes
.buffer(() => {
/*
The following doesn't work because it needs to be an
observable, but how to observe what is in the buffer?
Also would like to not duplicate the join and includes
if possible
return function (buffer) {
return handledKeySequences.includes(buffer.join('|'));
};
*/

// Returning an empty subject to flush the buffer
// every time to prevent an error, but this is not
// what I want.
return new Rx.Subject();
})
.map((buffer) => {
return buffer.join('|')
})
.filter((sequenceId) => {
return handledKeySequences.includes(sequenceId);
})
.subscribe((sequenceId) => {
// Expecting to be called once with 1|2
console.log("HANDLING", sequenceId)
})

我觉得我的方法是错误的,但我不知道什么是正确的方法。我尝试过使用 scan,但这会扫描可观察对象中的所有事件,这不是我想要的。

感谢您的帮助!

最佳答案

这应该可以通过 bufferWithCount 实现:

const handledKeySequences = ['1|2']

// Mock for document keydown event
keyCodes = Rx.Observable.from([0,1,2,3,4]);

const buffer$ = keyCodes
.bufferWithCount(2, 1) // first param = longest possible sequence, second param = param1 - 1
.do(console.log)
.map((buffer) => {
return buffer.join('|')
})
.filter((sequenceId) => {
return handledKeySequences.includes(sequenceId);

});

buffer$.subscribe((sequenceId) => {
console.log("HANDLING", sequenceId)
});

观看直播here .

另请查看this问题。

关于javascript - 如何根据 rxjs 中的缓冲区内容缓冲可观察值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com