mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 10:21:53 +00:00
simplify websocket opening and response types from BE
This commit is contained in:
@@ -1,54 +1,12 @@
|
||||
import { Observable, timeout } from 'rxjs'
|
||||
import { webSocket, WebSocketSubject } from 'rxjs/webSocket'
|
||||
import { webSocket } from 'rxjs/webSocket'
|
||||
import { Update } from '../types'
|
||||
import { Source } from './source'
|
||||
|
||||
export class WebsocketSource<T> implements Source<T> {
|
||||
constructor(
|
||||
private readonly url: string,
|
||||
// TODO: Remove fallback after client app is updated
|
||||
private readonly document: Document = document,
|
||||
) {}
|
||||
constructor(private readonly url: string) {}
|
||||
|
||||
watch$(): Observable<RPCResponse<Update<T>>> {
|
||||
const stream$: WebSocketSubject<RPCResponse<Update<T>>> = webSocket({
|
||||
url: this.url,
|
||||
openObserver: {
|
||||
next: () => stream$.next(this.document.cookie as any),
|
||||
},
|
||||
})
|
||||
|
||||
return stream$.pipe(timeout({ first: 60000 }))
|
||||
}
|
||||
}
|
||||
|
||||
interface RPCBase {
|
||||
jsonrpc: '2.0'
|
||||
}
|
||||
|
||||
export interface RPCSuccess<T> extends RPCBase {
|
||||
result: T
|
||||
}
|
||||
|
||||
export interface RPCError extends RPCBase {
|
||||
error: {
|
||||
code: number // 34 means unauthenticated
|
||||
message: string
|
||||
data: {
|
||||
details: string
|
||||
}
|
||||
}
|
||||
}
|
||||
export type RPCResponse<T> = RPCSuccess<T> | RPCError
|
||||
|
||||
class RpcError {
|
||||
code: number
|
||||
message: string
|
||||
details: string
|
||||
|
||||
constructor(e: RPCError['error']) {
|
||||
this.code = e.code
|
||||
this.message = e.message
|
||||
this.details = e.data.details
|
||||
watch$(): Observable<Update<T>> {
|
||||
return webSocket<Update<T>>(this.url).pipe(timeout({ first: 60000 }))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user