Last active
July 22, 2019 11:16
-
-
Save praveen001/c6b0b6eb24254829fdc2299e3c7ad69e to your computer and use it in GitHub Desktop.
Redux Observable WebSocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const connectEpic = (action$, state$: StateObservable<IState>) => | |
action$ | |
.ofType(WebsocketActionTypes.CONNECT) | |
.switchMap((action: IConnectWebsocketAction) => | |
connectSocket(state$.value.config.webSocketURL) | |
.map(data => receiveMessageFromWebSocket(data)) | |
.catch(e => of(disconnect(true))) // <-- Handle 1st case by using catch operator | |
); | |
const connectedEpic = action$ => | |
action$.ofType(WebsocketActionTypes.CONNECT).switchMap(() => | |
onOpenSubject.map(() => { | |
onCloseSubject.map(() => disconnect(true)); // <-- Handle 2nd case by listening to onCloseSubject | |
return connected(); | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment