mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 02:11:54 +00:00
removes temps, massive refactor
This commit is contained in:
committed by
Aiden McClelland
parent
0dcbb3956e
commit
950773e542
@@ -1,17 +1,17 @@
|
||||
import { from, Observable } from 'rxjs'
|
||||
import { applyPatch, Operation } from 'fast-json-patch'
|
||||
import { observable, runInAction } from 'mobx'
|
||||
import { from, Observable, of } from 'rxjs'
|
||||
import { toStream } from 'mobx-utils'
|
||||
import { DBCache, Dump, Revision, Update } from './types'
|
||||
import { applyPatch } from 'fast-json-patch'
|
||||
|
||||
export class Store<T extends object> {
|
||||
private o: { data: T }
|
||||
export class Store<T extends { }> {
|
||||
cache: DBCache<T>
|
||||
|
||||
constructor (data: T) {
|
||||
this.o = observable({ data })
|
||||
constructor (
|
||||
readonly initialCache: DBCache<T>,
|
||||
) {
|
||||
this.cache = initialCache
|
||||
}
|
||||
|
||||
get peek (): T { return this.o.data }
|
||||
|
||||
watch$ (): Observable<T>
|
||||
watch$<P1 extends keyof T> (p1: P1): Observable<T[P1]>
|
||||
watch$<P1 extends keyof T, P2 extends keyof T[P1]> (p1: P1, p2: P2): Observable<T[P1][P2]>
|
||||
@@ -20,29 +20,38 @@ export class Store<T extends object> {
|
||||
watch$<P1 extends keyof T, P2 extends keyof T[P1], P3 extends keyof T[P1][P2], P4 extends keyof T[P1][P2][P3], P5 extends keyof T[P1][P2][P3][P4]> (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5): Observable<T[P1][P2][P3][P4][P5]>
|
||||
watch$<P1 extends keyof T, P2 extends keyof T[P1], P3 extends keyof T[P1][P2], P4 extends keyof T[P1][P2][P3], P5 extends keyof T[P1][P2][P3][P4], P6 extends keyof T[P1][P2][P3][P4][P5]> (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6): Observable<T[P1][P2][P3][P4][P5][P6]>
|
||||
watch$ (...args: (string | number)[]): Observable<any> {
|
||||
return from(toStream(() => this.peekAccess(...args), true))
|
||||
return from(toStream(() => this.peekNode(...args), true))
|
||||
}
|
||||
|
||||
set (data: T): void {
|
||||
runInAction(() => this.o.data = data)
|
||||
watchAll$ (): Observable<DBCache<T>> {
|
||||
return of(this.cache)
|
||||
}
|
||||
|
||||
applyPatchDocument (patch: Operation[]): { oldDocument: T, newDocument: T } {
|
||||
const oldDocument = this.o.data
|
||||
const newDocument = patchDocument(patch, oldDocument)
|
||||
this.set(newDocument)
|
||||
return { oldDocument, newDocument }
|
||||
update$ (update: Update<T>): Observable<DBCache<T>> {
|
||||
console.log('UPDATE:', update)
|
||||
if ((update as Revision).patch) {
|
||||
if (this.cache.sequence + 1 !== update.id) throw new Error(`Outdated sequence: current: ${this.cache.sequence}, new: ${update.id}`)
|
||||
applyPatch(this.cache.data, (update as Revision).patch, true, true)
|
||||
} else {
|
||||
this.cache.data = (update as Dump<T>).value
|
||||
}
|
||||
|
||||
this.cache.sequence = update.id
|
||||
return of(this.cache)
|
||||
}
|
||||
|
||||
private peekAccess (...args: (string | number)[]): any {
|
||||
reset (): void {
|
||||
this.cache = {
|
||||
sequence: 0,
|
||||
data: { },
|
||||
}
|
||||
}
|
||||
|
||||
private peekNode (...args: (string | number)[]): any {
|
||||
try {
|
||||
return args.reduce((acc, next) => (acc as any)[`${next}`], this.o.data)
|
||||
return args.reduce((acc, next) => (acc as any)[`${next}`], this.cache.data)
|
||||
} catch (e) {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function patchDocument<T> (patch: Operation[], doc: T): T {
|
||||
return applyPatch(doc, patch, true, false).newDocument
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user