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
{ | |
"editor.multiCursorModifier": "ctrlCmd", | |
"editor.snippetSuggestions": "top", | |
"gitlens.advanced.messages": { | |
"suppressShowKeyBindingsNotice": true, | |
"suppressWelcomeNotice": true | |
}, | |
"window.zoomLevel": 0, | |
"editor.tabSize": 2, | |
"editor.wordWrap": "off", |
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
let webSocketSubject: WebSocketSubject<{}>; | |
let onOpenSubject = new Subject(); | |
let onCloseSubject = new Subject(); | |
const connectSocket = websocketUrl => { | |
onOpenSubject = new Subject(); | |
onCloseSubject = new Subject(); | |
webSocketSubject = webSocket({ | |
url: websocketUrl, | |
openObserver: onOpenSubject, |
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
let webSocketSubject: WebSocketSubject<{}>; | |
let onOpenSubject = new Subject(); | |
let onCloseSubject = new Subject(); | |
// Create the websocket subject | |
const createSocket = websocketUrl => { | |
onOpenSubject = new Subject(); | |
onCloseSubject = new Subject(); | |
webSocketSubject = webSocket({ | |
url: websocketUrl, |
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$ => |
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 sendMessageEpic = action$ => | |
action$.ofType(WebsocketActionTypes.SEND_MESSAGE).map(action => { | |
webSocketSubject.next(action.payload); | |
return sentMessage(); | |
}); |
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 disconnectEpic = (action$: ActionsObservable<PlaylistAction>) => | |
action$.ofType(WebsocketActionTypes.DISCONNECT).map(() => { | |
onCloseSubject.complete(); | |
webSocketSubject.complete(); | |
return disconnected(); | |
}); |
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
import { combineEpics, StateObservable } from 'redux-observable'; | |
import { of } from 'rxjs'; | |
import { Subject } from 'rxjs/Subject'; | |
import { webSocket, WebSocketSubject } from 'rxjs/webSocket'; | |
import { | |
connected, | |
connect, | |
disconnected, | |
disconnect, | |
IConnectWebsocketAction, |
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
// Detect successfull connection by listening to onOpenSubject | |
const connectedEpic = action$ => | |
action$.ofType(WebsocketActionTypes.CONNECT).switchMap(() => | |
onOpenSubject.map(() => { | |
return connected(); | |
}) | |
); |
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.eventConfig.webSocketURL) | |
.map(data => receiveMessageFromWebSocket(data)) // <-- Map and fire receive action | |
); |
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 disconnectEpic = (action$, state$: StateObservable<IState>) => | |
action$ | |
.ofType(WebsocketActionTypes.DISCONNECT) | |
.mergeMap((action: IDisconnectFromWebsocketAction) => { | |
if (action.payload.retry) { | |
return of(connect(state$.value.config.webSocketURL)) // <-- reconnect | |
.delay(5000) | |
.startWith(disconnected()); | |
} | |
onCloseSubject.complete(); |
OlderNewer