Redux Observable的Rx动作序列

时间:2019-01-02 02:55:28

标签: rxjs6 redux-observable

这些史诗是由用户在字段中键入触发的。 SET_TODO触发一条消息,提示用户正在输入。 然后,如果键入停止一秒钟,则表明用户没有在键入消息,然后两秒钟后,消息为空字符串。

几个问题,

一个:有没有办法对一部自成一体的史诗做这件事?

二:我使用了虚拟动作(Redux reducer中没有的动作),这是一个不好的技巧吗?

欢呼

import { concatMap, debounce, delay, mapTo } from 'rxjs/operators'
import { combineEpics, ofType } from 'redux-observable'
import { of, timer } from 'rxjs'

const enteringTextEpic = action$ =>
  action$.pipe(
    ofType('SET_TODO'),
    delay(200),
    concatMap(action =>
      of(
        { type: 'SET_MESSAGE', text: 'You are entering text' },
        { type: 'NOT' }
      )
    )
  )

const notEnteringTextEpic = action$ =>
  action$.pipe(
    ofType('NOT'),
    debounce(() => timer(1000)),
    concatMap(action =>
      of(
        { type: 'SET_MESSAGE', text: 'Now you are not entering text' },
        { type: 'FINISHED' }
      )
    )
  )
/**The actions NOT and FINISHED are dummy actions and are not in the reducer */
const finishedEnteringTextEpic = action$ =>
  action$.pipe(
    ofType('FINISHED'),
    debounce(() => timer(2000)),
    mapTo({ type: 'SET_MESSAGE', text: '' })
  )

const epica = combineEpics(
  enteringTextEpic,
  finishedEnteringTextEpic,
  notEnteringTextEpic
)

export default epica

0 个答案:

没有答案