mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-31 04:13:39 +00:00
chore: refactor rxjs
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
import { BehaviorSubject, concat, from, Observable, of } from 'rxjs'
|
|
||||||
import {
|
import {
|
||||||
concatMap,
|
BehaviorSubject,
|
||||||
delay,
|
concat,
|
||||||
map,
|
from,
|
||||||
skip,
|
ignoreElements,
|
||||||
switchMap,
|
Observable,
|
||||||
take,
|
of,
|
||||||
tap,
|
repeatWhen,
|
||||||
} from 'rxjs/operators'
|
timer,
|
||||||
|
} from 'rxjs'
|
||||||
|
import { concatMap, map, switchMap, take, tap } from 'rxjs/operators'
|
||||||
import { Store } from '../store'
|
import { Store } from '../store'
|
||||||
import { Http, Update } from '../types'
|
import { Http, Update } from '../types'
|
||||||
import { Source } from './source'
|
import { Source } from './source'
|
||||||
@@ -23,33 +24,14 @@ export class PollSource<T> implements Source<T> {
|
|||||||
private readonly http: Http<T>,
|
private readonly http: Http<T>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
watch$(store: Store<T>): Observable<RPCResponse<Update<T>>> {
|
watch$({ sequence$ }: Store<T>): Observable<RPCResponse<Update<T>>> {
|
||||||
const polling$ = new BehaviorSubject('')
|
return sequence$.pipe(
|
||||||
|
|
||||||
const updates$ = of({}).pipe(
|
|
||||||
concatMap(_ => store.sequence$),
|
|
||||||
concatMap(seq => this.http.getRevisions(seq)),
|
concatMap(seq => this.http.getRevisions(seq)),
|
||||||
take(1),
|
take(1),
|
||||||
)
|
// Revision[] converted it into Observable<Revision>, Dump<T> into Observable<Dump<T>>
|
||||||
|
concatMap(res => (Array.isArray(res) ? from(res) : of(res))),
|
||||||
const delay$ = of([]).pipe(
|
map(result => ({ result, jsonrpc: '2.0' as const })),
|
||||||
delay(this.pollConfig.cooldown),
|
repeatWhen(() => timer(this.pollConfig.cooldown)),
|
||||||
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>>
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
map(result => ({ result, jsonrpc: '2.0' })),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,24 @@
|
|||||||
import { Observable } from 'rxjs'
|
import { Observable } from 'rxjs'
|
||||||
import {
|
import { webSocket, WebSocketSubject } from 'rxjs/webSocket'
|
||||||
webSocket,
|
|
||||||
WebSocketSubject,
|
|
||||||
WebSocketSubjectConfig,
|
|
||||||
} from 'rxjs/webSocket'
|
|
||||||
import { Update } from '../types'
|
import { Update } from '../types'
|
||||||
import { Source } from './source'
|
import { Source } from './source'
|
||||||
|
|
||||||
export class WebsocketSource<T> implements Source<T> {
|
export class WebsocketSource<T> implements Source<T> {
|
||||||
private websocket$: WebSocketSubject<RPCResponse<Update<T>>> | undefined
|
private websocket$: WebSocketSubject<RPCResponse<Update<T>>> = webSocket({
|
||||||
|
url: this.url,
|
||||||
|
openObserver: {
|
||||||
|
next: () => {
|
||||||
|
this.websocket$.next(this.document.cookie as any)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
constructor(private readonly url: string) {}
|
constructor(
|
||||||
|
private readonly url: string,
|
||||||
|
private readonly document: Document,
|
||||||
|
) {}
|
||||||
|
|
||||||
watch$(): Observable<RPCResponse<Update<T>>> {
|
watch$(): Observable<RPCResponse<Update<T>>> {
|
||||||
const fullConfig: WebSocketSubjectConfig<RPCResponse<Update<T>>> = {
|
|
||||||
url: this.url,
|
|
||||||
openObserver: {
|
|
||||||
next: () => {
|
|
||||||
this.websocket$!.next(document.cookie as any)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
this.websocket$ = webSocket(fullConfig)
|
|
||||||
return this.websocket$
|
return this.websocket$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,18 +9,11 @@ export interface StashEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Store<T extends { [key: string]: any }> {
|
export class Store<T extends { [key: string]: any }> {
|
||||||
cache: DBCache<T>
|
readonly sequence$ = new BehaviorSubject(this.cache.sequence)
|
||||||
sequence$: BehaviorSubject<number>
|
|
||||||
private watchedNodes: { [path: string]: ReplaySubject<any> } = {}
|
private watchedNodes: { [path: string]: ReplaySubject<any> } = {}
|
||||||
private stash = new BTree<number, StashEntry>()
|
private stash = new BTree<number, StashEntry>()
|
||||||
|
|
||||||
constructor(
|
constructor(private readonly http: Http<T>, public cache: DBCache<T>) {}
|
||||||
private readonly http: Http<T>,
|
|
||||||
private readonly initialCache: DBCache<T>,
|
|
||||||
) {
|
|
||||||
this.cache = this.initialCache
|
|
||||||
this.sequence$ = new BehaviorSubject(initialCache.sequence)
|
|
||||||
}
|
|
||||||
|
|
||||||
watch$(): Observable<T>
|
watch$(): Observable<T>
|
||||||
watch$<P1 extends keyof T>(p1: P1): Observable<NonNullable<T[P1]>>
|
watch$<P1 extends keyof T>(p1: P1): Observable<NonNullable<T[P1]>>
|
||||||
|
|||||||
Reference in New Issue
Block a user