mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 02:11:54 +00:00
feat(client): remove data mutation (#32)
* feat(client): remove data mutation * chore: address comments and fix some other issues * chore: send the most recent cache upon subscription
This commit is contained in:
@@ -5,7 +5,7 @@ import BTree from 'sorted-btree'
|
||||
|
||||
export interface StashEntry {
|
||||
revision: Revision
|
||||
undo: Operation[]
|
||||
undo: Operation<unknown>[]
|
||||
}
|
||||
|
||||
export class Store<T extends { [key: string]: any }> {
|
||||
@@ -70,20 +70,12 @@ export class Store<T extends { [key: string]: any }> {
|
||||
this.processStashed(revision.id)
|
||||
}
|
||||
|
||||
private handleDump (dump: Dump<T>): void {
|
||||
Object.keys(this.cache.data).forEach(key => {
|
||||
if (dump.value[key] === undefined) {
|
||||
delete this.cache.data[key]
|
||||
}
|
||||
})
|
||||
|
||||
Object.entries(dump.value).forEach(([key, val]) => {
|
||||
(this.cache.data as any)[key] = val
|
||||
})
|
||||
this.stash.deleteRange(this.cache.sequence, dump.id, false)
|
||||
private handleDump ({ value, id }: Dump<T>): void {
|
||||
this.cache.data = { ...value }
|
||||
this.stash.deleteRange(this.cache.sequence, id, false)
|
||||
this.updateWatchedNodes('')
|
||||
this.updateSequence(dump.id)
|
||||
this.processStashed(dump.id + 1)
|
||||
this.updateSequence(id)
|
||||
this.processStashed(id + 1)
|
||||
}
|
||||
|
||||
private processStashed (id: number): void {
|
||||
@@ -95,9 +87,7 @@ export class Store<T extends { [key: string]: any }> {
|
||||
let stashEntry = this.stash.get(this.stash.maxKey() as number)
|
||||
|
||||
while (stashEntry && stashEntry.revision.id > id) {
|
||||
stashEntry.undo.forEach(u => {
|
||||
applyOperation(this.cache.data, u)
|
||||
})
|
||||
stashEntry.undo.forEach(u => applyOperation(this.cache, u))
|
||||
stashEntry = this.stash.nextLowerPair(stashEntry.revision.id)?.[1]
|
||||
}
|
||||
}
|
||||
@@ -105,19 +95,17 @@ export class Store<T extends { [key: string]: any }> {
|
||||
private applyRevisions (id: number): void {
|
||||
let revision = this.stash.get(id)?.revision
|
||||
while (revision) {
|
||||
let undo: Operation[] = []
|
||||
|
||||
let undo: Operation<unknown>[] = []
|
||||
let success = false
|
||||
|
||||
try {
|
||||
revision.patch.forEach(op => {
|
||||
const u = applyOperation(this.cache.data, op)
|
||||
const u = applyOperation(this.cache, op)
|
||||
if (u) undo.push(u)
|
||||
})
|
||||
success = true
|
||||
} catch (e) {
|
||||
undo.forEach(u => {
|
||||
applyOperation(this.cache.data, u)
|
||||
})
|
||||
undo.forEach(u => applyOperation(this.cache, u))
|
||||
undo = []
|
||||
}
|
||||
|
||||
@@ -163,6 +151,6 @@ export class Store<T extends { [key: string]: any }> {
|
||||
}
|
||||
|
||||
private isRevision (update: Update<T>): update is Revision {
|
||||
return !!(update as Revision).patch
|
||||
return 'patch' in update
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user