Sawtaytoes
12/12/2019 - 8:55 AM

WebSocket cancellation at all levels

const connectToServerEpic = (
  action$,
) => (
  action$
  .pipe(
    ofType(CONNECT_TO_SERVER),
    switchMap(({
      reconnectionTimeout,
      // ...
    }) => (
      action$
      .pipe(
        ofType(RECONNECT_TO_SERVER),
        takeUntil(
          action$
          .pipe(
            ofType(DISCONNECT_FROM_SERVER),
          )
        ),
        // ...
        switchMap((
          webSocketConnection$,
        ) => (
          webSocketConnection$
          .pipe(
            takeUntil(
              action$
              .pipe(
                ofType(
                  RECONNECT_TO_SERVER,
                  DISCONNECT_FROM_SERVER,
                ),
              )
            ),
            catchError(() => (
              timer(
                reconnectionTimeout,
              )
              .pipe(
                takeUntil(
                  action$
                  .pipe(
                    ofType(
                      RECONNECT_TO_SERVER,
                      DISCONNECT_FROM_SERVER,
                    ),
                  )
                ),
                // ...
              )
            )),
            // ...
          )),
        )),
      )
    )),
  )
)