mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 02:11:54 +00:00
add connection monitoring
This commit is contained in:
committed by
Aiden McClelland
parent
4fedd74b26
commit
eb4a24e797
@@ -1,34 +1,40 @@
|
||||
import { Observable } from 'rxjs'
|
||||
import { BehaviorSubject, Observable } from 'rxjs'
|
||||
import { webSocket, WebSocketSubject, WebSocketSubjectConfig } from 'rxjs/webSocket'
|
||||
import { Update } from '../types'
|
||||
import { ConnectionStatus, Update } from '../types'
|
||||
import { Source } from './source'
|
||||
|
||||
export class WebsocketSource<T> implements Source<T> {
|
||||
private websocket$: WebSocketSubject<Update<T>>
|
||||
connectionStatus$ = new BehaviorSubject(ConnectionStatus.Initializing)
|
||||
private websocket$: WebSocketSubject<Update<T>> | undefined
|
||||
|
||||
constructor (
|
||||
readonly url: string,
|
||||
private readonly url: string,
|
||||
) {
|
||||
}
|
||||
|
||||
watch$ (): Observable<Update<T>> {
|
||||
const fullConfig: WebSocketSubjectConfig<Update<T>> = {
|
||||
url,
|
||||
url: this.url,
|
||||
openObserver: {
|
||||
next: () => {
|
||||
console.log('WebSocket connection open')
|
||||
this.websocket$.next('open message' as any)
|
||||
this.connectionStatus$.next(ConnectionStatus.Connected)
|
||||
this.websocket$!.next('open message' as any)
|
||||
},
|
||||
},
|
||||
closeObserver: {
|
||||
next: () => {
|
||||
this.connectionStatus$.next(ConnectionStatus.Disconnected)
|
||||
console.log('WebSocket connection closed')
|
||||
// @TODO re-open websocket on retry loop
|
||||
},
|
||||
},
|
||||
closingObserver: {
|
||||
next: () => console.log('Websocket subscription cancelled, websocket closing'),
|
||||
next: () => {
|
||||
console.log('Websocket subscription cancelled, websocket closing')
|
||||
},
|
||||
},
|
||||
}
|
||||
this.websocket$ = webSocket(fullConfig)
|
||||
return this.websocket$
|
||||
}
|
||||
|
||||
watch$ (): Observable<Update<T>> { return this.websocket$.asObservable() }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user