mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-27 02:41:55 +00:00
move client from separate repo
This commit is contained in:
56
client/lib/source/poll-source.ts
Normal file
56
client/lib/source/poll-source.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { BehaviorSubject, concat, from, Observable, of } from 'rxjs'
|
||||
import { catchError, concatMap, delay, skip, switchMap, take, tap } from 'rxjs/operators'
|
||||
import { Http } from '../patch-db'
|
||||
import { UpdateReal } from '../sequence-store'
|
||||
import { Source } from './source'
|
||||
|
||||
export type PollConfig = {
|
||||
cooldown: number
|
||||
}
|
||||
|
||||
export class PollSource<T> implements Source<T> {
|
||||
|
||||
constructor (
|
||||
private readonly pollConfig: PollConfig,
|
||||
private readonly http: Http<T>,
|
||||
) { }
|
||||
|
||||
watch$ (sequence$: Observable<number>): Observable<UpdateReal<T>> {
|
||||
console.log('POLL_SOURCE - watch$()')
|
||||
|
||||
const polling$ = new BehaviorSubject('')
|
||||
|
||||
const updates$ = of('').pipe(
|
||||
concatMap(_ => sequence$),
|
||||
take(1),
|
||||
tap(_ => console.log('making request')),
|
||||
concatMap(seq => this.http.getRevisions(seq)),
|
||||
catchError(e => {
|
||||
console.error(e)
|
||||
return of([])
|
||||
}),
|
||||
tap(_ => console.log('request complete')),
|
||||
)
|
||||
|
||||
const delay$ = of([]).pipe(
|
||||
tap(_ => console.log('starting cooldown')),
|
||||
delay(this.pollConfig.cooldown),
|
||||
tap(_ => console.log('cooldown finished')),
|
||||
tap(_ => polling$.next('')),
|
||||
skip(1),
|
||||
)
|
||||
|
||||
const poll$ = concat(updates$, delay$)
|
||||
|
||||
return polling$.pipe(
|
||||
switchMap(_ => poll$),
|
||||
concatMap(res => {
|
||||
if (Array.isArray(res)) {
|
||||
return from(res) // takes Revision[] and converts it into Observable<Revision>
|
||||
} else {
|
||||
return of(res) // takes Dump<T> and converts it into Observable<Dump<T>>
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
6
client/lib/source/source.ts
Normal file
6
client/lib/source/source.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Observable } from 'rxjs'
|
||||
import { Update } from '../sequence-store'
|
||||
|
||||
export interface Source<T> {
|
||||
watch$ (sequence$?: Observable<number>): Observable<Update<T>>
|
||||
}
|
||||
28
client/lib/source/ws-source.ts
Normal file
28
client/lib/source/ws-source.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Observable } from 'rxjs'
|
||||
import { webSocket, WebSocketSubject, WebSocketSubjectConfig } from 'rxjs/webSocket'
|
||||
import { UpdateReal } from '../sequence-store'
|
||||
import { Source } from './source'
|
||||
|
||||
export class WebsocketSource<T> implements Source<T> {
|
||||
private websocket$: WebSocketSubject<UpdateReal<T>>
|
||||
|
||||
constructor (
|
||||
readonly url: string,
|
||||
) {
|
||||
const fullConfig: WebSocketSubjectConfig<UpdateReal<T>> = {
|
||||
url,
|
||||
openObserver: {
|
||||
next: () => console.log('WebSocket connection open'),
|
||||
},
|
||||
closeObserver: {
|
||||
next: () => console.log('WebSocket connection closed'),
|
||||
},
|
||||
closingObserver: {
|
||||
next: () => console.log('Websocket subscription cancelled, websocket closing'),
|
||||
},
|
||||
}
|
||||
this.websocket$ = webSocket(fullConfig)
|
||||
}
|
||||
|
||||
watch$ (): Observable<UpdateReal<T>> { return this.websocket$.asObservable() }
|
||||
}
|
||||
Reference in New Issue
Block a user