我需要帮助才能在窗口加密事件后发送一个动作
() => Observable.fromEvent(window, 'keyup').map(event => ({type: 'KEY_PRESSED', key: event.key, event}));
谢谢你
答案 0 :(得分:4)
如果你总是希望在这些keyup
事件上倾听和调度动作,那么Epic非常简单:
const windowKeyUpEpic = () =>
Observable.fromEvent(window, 'keyup')
.map(event => ({
type: 'KEY_UP',
key: event.key,
event
}));
但这可能效率低下,因为你可能只想在某些特定情况下倾听。
您可以通过某种方式启用/禁用此全局侦听器:
const windowKeyUpEpic = action$ =>
action$.ofType('START_LISTEN_FOR_KEYUP')
.switchMap(() =>
Observable.fromEvent(window, 'keyup')
.map(event => ({
type: 'KEY_UP',
key: event.key,
event
}))
.takeUntil(action$.ofType('STOP_LISTEN_FOR_KEYUP'))
);