Henrik
Henrik

Reputation: 9915

redux-observables mergeMap w/ two outputs

I have an epic like this

/**
 * Request a new account from the API
 */
export function signupApiRequest(action$: Observable, store: Store) {
  return action$.ofType(SIGNUP)
    .map(action => [ action, store.getState().accounts.signup ])
    .mergeMap(([ action, signup ]) =>
      sendCommand('/api/accounts/signup', {
          name: signup.name,
          email: signup.email
        })
        .map(response => [ response, signup.email ])
     )
    .mergeMap(([ response, email ]) => {
      switch (response.status) {
        case 409:
          return [existingUser(email)];

        case 201:
          const succ = { type: successOf(SIGNUP) },
                user = mapTokenToUser(response.body.data),
                created = userCreated(user);
          console.log('signup success', [succ, created]);
          return [succ, created];

        default:
          throw new Error(`Unknown response code ${response.code}`);
      }
    });
}

When signing up, 'signup success' is printed and both actions are logged as they should.

However, only the first of the actions is outputted from the epic. Redux actions for realz

Why and what am I doing wrong?

Changing the order in the array makes the other value get outputted.

Upvotes: 0

Views: 460

Answers (1)

jayphelps
jayphelps

Reputation: 15401

We've discovered this appears to be a bug in RxJS itself starting in 5.1.0. There's an exception being thrown in your reducer but it's being swallowed silently by RxJS.

https://github.com/redux-observable/redux-observable/issues/263#issuecomment-310180351

How it's interacting with redux/redux-observable is a little complicated, but ultimately not related as this can be reproduced with:

https://jsbin.com/dukixu/edit?html,js,output

const input$ = new Rx.Subject();

input$
  .mergeMap(() => Rx.Observable.of(1, 2))
  .subscribe(d => {
    throw new Error('some error');
  });

input$.next();

Upvotes: 1

Related Questions