This commit is contained in:
Drew Ansbacher
2021-07-21 14:44:08 -06:00
committed by Aiden McClelland
parent 5699702656
commit 88084f60a2
3 changed files with 14 additions and 9 deletions

View File

@@ -2,7 +2,7 @@ import { merge, Observable, of } from 'rxjs'
import { concatMap, finalize, tap } from 'rxjs/operators'
import { Source } from './source/source'
import { Store } from './store'
import { DBCache, Http } from './types'
import { DBCache, HashMap, Http } from './types'
export class PatchDB<T> {
store: Store<T>
@@ -15,7 +15,7 @@ export class PatchDB<T> {
this.store = new Store(this.http, this.initialCache)
}
sync$ (): Observable<DBCache<T>> {
sync$ (): Observable<DBCache<HashMap>> {
return merge(...this.sources.map(s => s.watch$(this.store)))
.pipe(
tap(update => this.store.update(update)),

View File

@@ -1,4 +1,4 @@
import { DBCache, Dump, Http, Revision, Update } from './types'
import { DBCache, Dump, HashMap, Http, Revision, Update } from './types'
import { BehaviorSubject, Observable } from 'rxjs'
import { applyOperation, getValueByPointer, Operation } from './json-patch-lib'
import BTree from 'sorted-btree'
@@ -9,7 +9,7 @@ export interface StashEntry {
}
export class Store<T> {
cache: DBCache<T>
cache: DBCache<HashMap>
sequence$: BehaviorSubject<number>
private watchedNodes: { [path: string]: BehaviorSubject<any> } = { }
private stash = new BTree<number, StashEntry>()
@@ -66,6 +66,8 @@ export class Store<T> {
}
private handleDump (dump: Dump<T>): void {
Object.keys(this.cache.data).forEach(key => delete this.cache.data[key])
Object.assign(this.cache.data, dump)
this.cache.data = dump.value
this.stash.deleteRange(this.cache.sequence, dump.id, false)
this.updateWatchedNodes('')

View File

@@ -3,9 +3,9 @@ import { Operation } from './json-patch-lib'
// revise a collection of nodes.
export type Revision = { id: number, patch: Operation[], expireId: string | null }
// dump/replace the entire store with T
export type Dump<T> = { id: number, value: T, expireId: string | null }
export type Dump<T extends HashMap> = { id: number, value: T, expireId: string | null }
export type Update<T> = Revision | Dump<T>
export type Update<T extends HashMap> = Revision | Dump<T>
export enum PatchOp {
ADD = 'add',
@@ -13,17 +13,20 @@ export enum PatchOp {
REPLACE = 'replace',
}
export interface Http<T> {
export interface Http<T extends HashMap> {
getRevisions (since: number): Promise<Revision[] | Dump<T>>
getDump (): Promise<Dump<T>>
}
export interface Bootstrapper<T> {
export interface Bootstrapper<T extends HashMap> {
init (): Promise<DBCache<T>>
update (cache: DBCache<T>): Promise<void>
}
export interface DBCache<T>{
export interface DBCache<T extends HashMap>{
sequence: number,
data: T
}
export type HashMap = { [type: string]: any }