Skip to content

Commit

Permalink
Web: auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 15, 2022
1 parent 687621b commit 72203af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Binary file modified hostmonitor
Binary file not shown.
15 changes: 14 additions & 1 deletion web/client/src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { map, Observable, scan } from 'rxjs'
import { fromEvent, map, mergeMap, Observable, retryWhen, scan, take, timer } from 'rxjs'
import { WebSocketSubject } from 'rxjs/webSocket'

export type Stream = Observable<Stream.Item>

export namespace Stream {
const RETRY_DELAY = 5000
export interface Item {
id: string
inProgress: boolean
Expand All @@ -14,6 +15,18 @@ export namespace Stream {
export const create = () => {
const sock = new WebSocketSubject<Stream.Item>(`ws://${location.host}/ws`)
return sock.pipe(
retryWhen(errors =>
errors.pipe(
mergeMap(() => {
if (window.navigator.onLine) {
console.warn(`Retrying in ${RETRY_DELAY}ms.`)
return timer(RETRY_DELAY)
} else {
return fromEvent(window, 'online').pipe(take(1))
}
})
)
),
scan<Stream.Item, Map<string, Stream.Item>>(
(a, c) => (a.set(c.id, c), a),
new Map<string, Stream.Item>()
Expand Down

0 comments on commit 72203af

Please sign in to comment.