我对RxJs很陌生,并没有阅读过这方面的解决方案。更详细的说明在评论中,但基本上我想处理一个键组合(我认为缓冲区会这样做)当按下一个特定键时(如按下“o”将等待一小段时间按下其他键) ,但是否则立即处理键输入(如果没有按下“o”,则除“o”以外的任何内容,或者“o”的“超时”已经过去。)
stargazer( dejure, defacto, JI, type="text", out="reg_EPI_JI_70.txt")
由于
答案 0 :(得分:1)
您有一个非常常见的情况需要处理,即一个事件,其关联操作取决于某些控制状态(即您有一个基本状态机)。基本上,如果您考虑这两种控制状态:SIMPLE_KEY
,COMBO
和这两个事件:keyup
和keyupTimer
,那么:
SIMPLE_KEY
状态
o
,则切换到COMBO
州o
,那么我们保持相同状态,并将密钥传递给下游COMBO
状态
SIMPLE_KEY
州因此,要将事件映射到某个操作,您需要知道您所处的控制状态。操作符scan
允许您根据累积状态决定如何处理事件。
可能是这样的(https://jsfiddle.net/cbhmxaas/):
function getKeyFromEvent(ev) {return ev.key}
function isKeyPressedIsO(ev) {return getKeyFromEvent(ev) === 'o'}
const KEY = 'key';
const TIMER = 'timer';
const COMBO = 'combo';
const SIMPLE_KEY = 'whatever';
const timeSpanInMs = 1000;
const keyup$ = Rx.Observable.fromEvent(document, 'keyup')
.map(ev => ({event: KEY, data: getKeyFromEvent(ev)}));
const keyupTimer$ = keyup$.flatMapLatest(eventStruct =>
Rx.Observable.of(eventStruct)
// .tap(console.warn.bind(console, 'timer event'))
.delay(timeSpanInMs)
.map(() => ({event : TIMER, data: null}))
);
Rx.Observable.merge(keyup$, keyupTimer$)
// .tap(console.warn.bind(console))
.scan((acc, eventStruct) => {
if (acc.control === SIMPLE_KEY) {
if (eventStruct.event === KEY) {
if (eventStruct.data === `o`) {
return {control: COMBO, keyOrKeys : []}
}
else {
return {control: SIMPLE_KEY, keyOrKeys : eventStruct.data}
}
}
else {
// TIMER event
return {control: SIMPLE_KEY, keyOrKeys : null}
}
}
else {
// we only have two states, so it is COMBO state here
if (eventStruct.event === KEY) {
return {control: COMBO, keyOrKeys : acc.keyOrKeys.concat([eventStruct.data])}
}
else {
// this is a TIMER event, we only have two events
return {control: SIMPLE_KEY, keyOrKeys : acc.keyOrKeys}
}
}
}, {control: SIMPLE_KEY, keyOrKeys : null})
// .tap(console.warn.bind(console))
.filter(state => state.keyOrKeys) // TIMER event in SIMPLE_KEY state
.map (({control, keyOrKeys}) => {
// Here you associate your action
// if keyOrKeys is an array, then you have a combo
// else you have a single key
console.log('key(s) pressed', keyOrKeys)
return keyOrKeys
})
.subscribe (console.log.bind(console))