0

I'm playing with redux-observable to trigger two actions. The first action was based on condition when it is true and second action should be trigger using debounce at 500 seconds. Both observables can be run parallely and not dependent on each other.

So far, i came up with the following which is not a working.

export const updateContent = (content$, { getState }) => {
    return content$.ofType(actionTypes.REQ_CHANGE_EDIT)
        .flatMap(action =>
            Observable.forkJoin(
                Observable.if(() => isChanged(getState()),
                    Observable.of(({ type: actionTypes.CHANGE_EDIT, changed: true }))),
                Observable.of({ type: actionTypes.CACHE_CONTENT, isCached: false }).debounceTime(500),
            ));

};
user1595858
  • 3,700
  • 15
  • 66
  • 109

1 Answers1

0

try use combineLatest instead of forkjoin and force emit an initial value from .if operator

export const updateContent = (content$, { getState }) => {
return content$.ofType(actionTypes.REQ_CHANGE_EDIT)
    .flatMap(action =>
        Observable.combineLatest(
            Observable.if(() => isChanged(getState()),
                Observable.of(({ type: actionTypes.CHANGE_EDIT, changed: true }))).startWith(null),
            Observable.of({ type: actionTypes.CACHE_CONTENT, isCached: false }).debounceTime(500),
        ));

};
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39
  • I need the second request emit all the time. – user1595858 Apr 15 '18 at 11:35
  • What’s the output that u get – Fan Cheung Apr 15 '18 at 12:31
  • cand you confirm if you recieve emission by attachement a .do content$.ofType(actionTypes.REQ_CHANGE_EDIT).do(console.log) – Fan Cheung Apr 15 '18 at 12:54
  • yes i see that action emitting from redux dev tools. – user1595858 Apr 15 '18 at 13:01
  • Sorry my bad. modified the answer. Combinelatest will need at least one emission from each too. – Fan Cheung Apr 15 '18 at 13:12
  • still not emitting anything – user1595858 Apr 15 '18 at 13:25
  • then the problem may be in the operators before combinelatest , checkout this fiddle http://jsfiddle.net/cy0nbs3x/651/ – Fan Cheung Apr 15 '18 at 13:26
  • i see it working with plain observables. I think i can relate to this issue to as it not sending action. https://stackoverflow.com/questions/40408041/redux-observable-epic-that-doesnt-send-any-new-actions?rq=1 – user1595858 Apr 15 '18 at 13:32
  • Haven't used redux observable before so not really sure – Fan Cheung Apr 15 '18 at 13:35
  • here is the fiddle if you going to take a extra step. http://jsbin.com/jexomi/edit?js,output – user1595858 Apr 15 '18 at 13:41
  • i added the combinelatest http://jsbin.com/nesijaceye/2/edit?js,output it seems to fire ok but i don't know what you want achieve , maybe u can start with this fiddle – Fan Cheung Apr 15 '18 at 13:51
  • the `pong` was not called after dbounce with .5 seconds – user1595858 Apr 15 '18 at 14:22
  • You get an array after combinelatest, i can only return one action so i hard coded to always return the first item. Just want to demonstrate the inner observable works that’s all. – Fan Cheung Apr 15 '18 at 14:23
  • Ok, is it possible to return two actions? First one checks a condition and emits and second emits with debounce? – user1595858 Apr 15 '18 at 14:24
  • I guess it always go to reducer which accept one action. U can do conditional checks and return whichever action is appropriate – Fan Cheung Apr 15 '18 at 14:25
  • i see what you saying. So we need to check condition before dispatching action.. Tht could be a nice idea. But for my understanding on observable, i was trying to send two actions. First action may or may not emit based on condition and second action always emit. I think observable can emit any number of actions. – user1595858 Apr 15 '18 at 14:34
  • guess you have to find another way to solve your use case, make it a linear decision, and use a default value if first obserbale don't emit. – Fan Cheung Apr 15 '18 at 14:36