Skip to content

Instantly share code, notes, and snippets.

@Odonno
Created July 27, 2019 15:55
Show Gist options
  • Save Odonno/c69b82e03a5138bebfb0468eaa36df30 to your computer and use it in GitHub Desktop.
Save Odonno/c69b82e03a5138bebfb0468eaa36df30 to your computer and use it in GitHub Desktop.
ngrx/signalr - connectionState$
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