I am trying to implement or operator with rxjs. is Race function is for OR operator. code exmaple:
export function saveComment(action$) {
return race(
action$.ofType(COMMENT.SAVE),
action$.ofType(COMMENT.DELETE)
)
.map((action) => action.payload)
.flatMap((data)=> commentService.saveComment(data.leadId, data.comment))
.map((resp)=> SaveCommentSucceed(resp.leadId, resp.comments))
.do((res)=>new Comments().injectComments(res.payload.leadId, res.payload.comments));
}
It's a little difficult to tell based on the question, but it looks like you are trying to emit values from each observable and continue receiving emissions, with no preference for the order either emissions occur?
I suggest looking at the merge operator. It will act similarly to logical OR, emitting whenever one OR the other observables passed to it emits.
Race will emit only the first emission it receives from the observables it was passed, and then it will complete.
If you want to keep listening for emitted values and don't care about the order, use merge. The code would look similar to this:
export function saveComment(action$) {
return Observable
.merge(
action$.ofType(COMMENT.SAVE),
action$.ofType(COMMENT.DELETE)
)
.map((action) => action.payload)
.flatMap((data) => commentService.saveComment(data.leadId, data.comment))
.map((resp) => SaveCommentSucceed(resp.leadId, resp.comments))
.do((res) => new Comments().injectComments(res.payload.leadId, res.payload.comments));
}
If you are using a version of Rxjs (>= 5.5) that contains pipeable operators, the code would look like this:
export function saveComment(action$) {
return merge(
action$.ofType(COMMENT.SAVE),
action$.ofType(COMMENT.DELETE)
)
.pipe(
map((action) => action.payload),
flatMap((data) => commentService.saveComment(data.leadId, data.comment)),
map((resp) => SaveCommentSucceed(resp.leadId, resp.comments)),
tap((res) => new Comments().injectComments(res.payload.leadId, res.payload.comments))
);
}
If you would like to stop listening after one of the observables emits, keep using race.
You can find out more about Rxjs Observables and Operators at learn-rxjs.