This commit is contained in:
Matt Hill
2021-07-21 15:17:16 -06:00
committed by Aiden McClelland
parent 56d7cae16d
commit 3e2e1c92dd
4 changed files with 18 additions and 17 deletions

View File

@@ -2,9 +2,9 @@ import { merge, Observable, of } from 'rxjs'
import { concatMap, finalize, tap } from 'rxjs/operators' import { concatMap, finalize, tap } from 'rxjs/operators'
import { Source } from './source/source' import { Source } from './source/source'
import { Store } from './store' import { Store } from './store'
import { DBCache, HashMap, Http } from './types' import { DBCache, Http } from './types'
export class PatchDB<T extends HashMap> { export class PatchDB<T> {
store: Store<T> store: Store<T>
constructor ( constructor (

View File

@@ -1,7 +1,7 @@
import { Observable } from 'rxjs' import { Observable } from 'rxjs'
import { Store } from '../store' import { Store } from '../store'
import { HashMap, Update } from '../types' import { Update } from '../types'
export interface Source<T extends HashMap> { export interface Source<T> {
watch$ (store?: Store<T>): Observable<Update<T>> watch$ (store?: Store<T>): Observable<Update<T>>
} }

View File

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

View File

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