remove bootstraop and drop alias for dump

This commit is contained in:
Matt Hill
2024-06-03 23:25:46 -06:00
parent 88a804f56f
commit 875c6dbc84
3 changed files with 21 additions and 38 deletions

View File

@@ -1,4 +1,4 @@
import { DBCache, PatchOp } from './types'
import { Dump, PatchOp } from './types'
export interface BaseOperation {
path: string
@@ -37,10 +37,10 @@ export function getValueByPointer<T extends Record<string, T>>(
}
export function applyOperation<T>(
doc: DBCache<Record<string, any>>,
doc: Dump<Record<string, any>>,
{ path, op, value }: Operation<T> & { value?: T },
) {
doc.data = recursiveApply(doc.data, arrayFromPath(path), op, value)
doc.value = recursiveApply(doc.value, arrayFromPath(path), op, value)
}
export function arrayFromPath(path: string): string[] {
@@ -61,7 +61,7 @@ export function pathFromArray(args: Array<string | number>): string {
args
.map(a =>
String(a)
// do not change order, "~" needs to be replaced first
// do not change order, "~" needs to be replaced first
.replace(new RegExp('~', 'g'), '~0')
.replace(new RegExp('/', 'g'), '~1'),
)

View File

@@ -1,4 +1,4 @@
import { Bootstrapper, DBCache, Dump, Revision, Update } from './types'
import { Dump, Revision, Update } from './types'
import {
BehaviorSubject,
filter,
@@ -24,24 +24,20 @@ export class PatchDB<T extends { [key: string]: any }> {
}
} = {}
readonly cache$ = new BehaviorSubject<DBCache<T>>({
sequence: 0,
data: {} as T,
readonly cache$ = new BehaviorSubject<Dump<T>>({
id: 0,
value: {} as T,
})
constructor(private readonly source$: Observable<Update<T>[]>) {}
start(bootstrapper: Bootstrapper<T>) {
start() {
if (this.sub) return
const initialCache = bootstrapper.init()
this.cache$.next(initialCache)
this.sub = this.source$
.pipe(withLatestFrom(this.cache$))
.subscribe(([updates, cache]) => {
this.proccessUpdates(updates, cache)
bootstrapper.update(cache)
})
}
@@ -52,7 +48,7 @@ export class PatchDB<T extends { [key: string]: any }> {
this.watchedNodes = {}
this.sub.unsubscribe()
this.sub = null
this.cache$.next({ sequence: 0, data: {} as T })
this.cache$.next({ id: 0, value: {} as T })
}
watch$(): Observable<T>
@@ -131,14 +127,13 @@ export class PatchDB<T extends { [key: string]: any }> {
>
watch$(...args: (string | number)[]): Observable<any> {
return this.cache$.pipe(
filter(({ sequence }) => !!sequence),
filter(({ id }) => !!id),
take(1),
switchMap(({ data }) => {
switchMap(({ value }) => {
const path = pathFromArray(args)
if (!this.watchedNodes[path]) {
const value = getValueByPointer(data, path)
this.watchedNodes[path] = {
subject: new BehaviorSubject(value),
subject: new BehaviorSubject(getValueByPointer(value, path)),
pathArr: arrayFromPath(path),
}
}
@@ -147,21 +142,21 @@ export class PatchDB<T extends { [key: string]: any }> {
)
}
proccessUpdates(updates: Update<T>[], cache: DBCache<T>) {
proccessUpdates(updates: Update<T>[], cache: Dump<T>) {
updates.forEach(update => {
if (this.isRevision(update)) {
const expected = cache.sequence + 1
const expected = cache.id + 1
if (update.id < expected) return
this.handleRevision(update, cache)
} else {
this.handleDump(update, cache)
}
cache.sequence = update.id
cache.id = update.id
})
this.cache$.next(cache)
}
private handleRevision(revision: Revision, cache: DBCache<T>): void {
private handleRevision(revision: Revision, cache: Dump<T>): void {
// apply opperations
revision.patch.forEach(op => {
applyOperation(cache, op)
@@ -172,14 +167,14 @@ export class PatchDB<T extends { [key: string]: any }> {
const arr = arrayFromPath(path)
return startsWith(pathArr, arr) || startsWith(arr, pathArr)
})
if (match) this.updateWatchedNode(watchedPath, cache.data)
if (match) this.updateWatchedNode(watchedPath, cache.value)
})
}
private handleDump(dump: Dump<T>, cache: DBCache<T>): void {
cache.data = { ...dump.value }
private handleDump(dump: Dump<T>, cache: Dump<T>): void {
cache.value = { ...dump.value }
Object.keys(this.watchedNodes).forEach(watchedPath => {
this.updateWatchedNode(watchedPath, cache.data)
this.updateWatchedNode(watchedPath, cache.value)
})
}

View File

@@ -1,12 +1,10 @@
import { Operation } from './json-patch-lib'
// revise a collection of nodes.
export type Revision = {
id: number
patch: Operation<unknown>[]
}
// dump/replace the entire store with T
export type Dump<T> = { id: number; value: T }
export type Update<T> = Revision | Dump<T>
@@ -16,13 +14,3 @@ export enum PatchOp {
REMOVE = 'remove',
REPLACE = 'replace',
}
export interface Bootstrapper<T extends Record<string, any>> {
init(): DBCache<T>
update(cache: DBCache<T>): void
}
export interface DBCache<T extends Record<string, any>> {
sequence: number
data: T
}