【发布时间】:2018-04-30 02:52:03
【问题描述】:
我有一对事件:add1/add2/etc 和 remove1/remove2/etc。我想要以下内容:
- 当
add1在流上发出时- 如果
DELAY没有新的add*排放- 发射
remove1
- 发射
- 如果发出
add*- 立即为
add1发出remove1 - 在
DELAY之后为add*发出remove*
- 立即为
- 如果
对于流中所有add* 的发射,这应该会继续。
这是我为这个案例使用 RxJS 大理石测试编写的一个测试:
import test from 'tape'
import { set, lensPath } from 'ramda'
import { TestScheduler } from 'rxjs/testing'
import hideAfterDelay from '../another/file'
import { actionCreators } from '../another/dir'
const prefix = 'epics -> notifications'
test(`${prefix} -> hideAfterDelay`, t => {
t.plan(1)
const scheduler = new TestScheduler(t.deepEqual)
const actionMap = {
a: createAddAction('hello!'),
b: createAddAction('goodbye!'),
x: actionCreators.notifications.remove('hello!'),
y: actionCreators.notifications.remove('goodbye!')
}
scheduler.run(({ cold, expectObservable }) => {
const actionStream = cold('a-------a-b-a------', actionMap)
const expected = '-----x-----x-y----x'
const actual = hideAfterDelay(5)(actionStream)
expectObservable(actual).toBe(expected, actionMap)
})
})
function createAddAction (name) {
const action = actionCreators.notifications.add(name)
const lens = lensPath(['payload', 'id'])
return set(lens, name, action)
}
我认为该测试代表了我上面描述的以及我想要的行为。
我怎样才能写出这个 observable?我尝试过使用timer 和race,但我无法让它工作......
这是一部使用redux-observable 的史诗,顺便说一句。
使用 RxJS v6
【问题讨论】:
标签: rxjs redux-observable