RxJS shares the observed sequence with multiple outputs

Is it possible to divide one observable stream into several other observables?

My use case is a form that a user can submit. The submit action is processed by the observable, and the validator is listened to on this action.

submitAction.forEach(validate) 

I want to bind actions to success or failure validation validation.

 validationFailure.forEach(outputErrors) validationSuccess.forEach(goToPage) 

I'm not sure how such cases are handled in reactive programming - perhaps separating the observable - this is simply not the right solution to solve this problem.

In any case, how would you deal with such a case?

+6
source share
1 answer

Can you just use map and filter , possibly with share , to avoid re-executing the validation logic?

 var submitAction = // some Rx.Observable var validationResult = submitAction.map(validate).share(); var success = validationResult.filter(function (r) { return !!r; }); var failure = validationResult.filter(function (r) { return !r; }); success.subscribe(goToPage); failure.subscribe(outputErrors); 
+9
source

Source: https://habr.com/ru/post/971819/


All Articles