It's not clear whether you mean you want them both debounced, but independently, or if you only want one of them to have debouncing at all. I've included answers to both:
2+ buttons independently debounced
I highly advise having them emit different action types, if they have different intents. If the intents are the same, e.g. the same button repeated in list items, then one way to handle this would be to use groupBy
to group each button independently by some unique key that button always includes.
const epicRequest = action$ =>
action$
.ofType(MY_ACTION_TYPE)
.groupBy(action => action.somethingUnique)
.mergeMap(action$ =>
action$
.debounceTime(500)
.switchMap(action => {
// Doing Ajax
})
);
This example assumes that every button includes something that uniquely identifies it, included as somethingUnique
.
Some resources on groupBy
WARNING: by default groupBy
never complete()'s or otherwise cleans up every group Observable it creates, which means this potentially is a memory leak. Whether that matters in your app or not, only you can know. If it does, groupBy
supports a third argument duration selector that you can use to decide when to stop tracking a particular group--that doesn't mean it create a new one if its needed later, just that the current Observable representing it is complete()'d. Often this is tough to do cleanly in practice without some sort of explicit teardown signal, like a route navigation action away from the page.
See the docs for more info: http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-groupBy
durationSelector: function(grouped: GroupedObservable<K, R>): Observable<any>
1 button debounced, another button never
This means they have different intents. One is intended to be debounced, the other not, even if after the debouncing their actions are the same.
The action that needs debouncing can emit a totally different action like MY_ACTION_TYPE_DEBOUNCED
, then after debouncing emit the normal MY_ACTION_TYPE
const myActionTypeDebouncedEpic = action$ =>
action$
.ofType(MY_ACTION_TYPE_DEBOUNCED)
.debounceTime(500)
.map(action => ({
...action,
type: MY_ACTION_TYPE
}));
const myActionTypeEpic = action$ =>
action$
.ofType(MY_ACTION_TYPE)
.switchMap(action => {
// Doing Ajax
});