mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 02:11:54 +00:00
polling working without refresh
This commit is contained in:
committed by
Aiden McClelland
parent
84cbed52c2
commit
e2a51475a7
@@ -1,26 +1,46 @@
|
||||
import { merge, Observable, of } from 'rxjs'
|
||||
import { concatMap, finalize, tap } from 'rxjs/operators'
|
||||
import { Source } from './source/source'
|
||||
import { merge, Observable, Subject, Subscription } from 'rxjs'
|
||||
import { Store } from './store'
|
||||
import { DBCache, Http } from './types'
|
||||
import { RPCError } from './source/ws-source'
|
||||
import { Source } from './source/source'
|
||||
|
||||
export class PatchDB<T> {
|
||||
store: Store<T>
|
||||
public store: Store<T> = new Store(this.http, this.initialCache)
|
||||
public connectionError$ = new Subject<Error>()
|
||||
public rpcError$ = new Subject<RPCError>()
|
||||
public cache$ = new Subject<DBCache<T>>()
|
||||
|
||||
private updatesSub?: Subscription
|
||||
private sourcesSub = this.sources$.subscribe(sources => {
|
||||
this.updatesSub = merge(...sources.map(s => s.watch$(this.store))).subscribe({
|
||||
next: (res) => {
|
||||
console.log('PatchDB -> subscribedChanges -> JCWM:', { res })
|
||||
if ('result' in res) {
|
||||
this.store.update(res.result)
|
||||
this.cache$.next(this.store.cache)
|
||||
}
|
||||
else {
|
||||
this.rpcError$.next(res)
|
||||
}
|
||||
},
|
||||
error: (e) => {
|
||||
this.connectionError$.next(e)
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
constructor (
|
||||
private readonly sources: Source<T>[],
|
||||
private readonly sources$: Observable<Source<T>[]>,
|
||||
private readonly http: Http<T>,
|
||||
private readonly initialCache: DBCache<T>,
|
||||
) {
|
||||
this.store = new Store(this.http, this.initialCache)
|
||||
console.log('STARTING PATCH 1')
|
||||
}
|
||||
|
||||
sync$ (): Observable<DBCache<T>> {
|
||||
return merge(...this.sources.map(s => s.watch$(this.store)))
|
||||
.pipe(
|
||||
tap(update => this.store.update(update)),
|
||||
concatMap(() => of(this.store.cache)),
|
||||
finalize(() => this.store.reset()),
|
||||
)
|
||||
clean () {
|
||||
this.sourcesSub.unsubscribe()
|
||||
if (this.updatesSub) {
|
||||
this.updatesSub.unsubscribe()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user