Created
July 27, 2019 15:55
-
-
Save Odonno/c69b82e03a5138bebfb0468eaa36df30 to your computer and use it in GitHub Desktop.
ngrx/signalr - connectionState$
This file contains hidden or 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 offline$ = fromEvent(window, 'offline').pipe(map(_ => false)); | |
const online$ = fromEvent(window, 'online').pipe(map(_ => true)); | |
const hasInternetConnection$ = merge( | |
of(navigator.onLine), | |
offline$, | |
online$ | |
); | |
const hubsStatuses$ = this.store.pipe( | |
select(selectHubsStatuses) | |
); | |
const connectionState$ = combineLatest(hubsStatuses$, hasInternetConnection$).pipe( | |
map(([hubsStatuses, hasInternetConnection]) => { | |
if (!hasInternetConnection) { | |
return 'RED'; | |
} | |
const allHubsConnected = hubsStatuses.every(hs => hs.state === SignalRStates.connected); | |
const anyHubDisconnected = hubsStatuses.some(hs => hs.state === SignalRStates.disconnected); | |
if (allHubsConnected) { | |
return 'GREEN'; | |
} | |
if (anyHubDisconnected) { | |
return 'RED'; | |
} | |
return undefined; | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment