return whole doc, better unsubscribe

This commit is contained in:
Matt Hill
2021-07-20 11:23:06 -06:00
committed by Aiden McClelland
parent 6c7e934b50
commit 7fb8e9b6da
2 changed files with 12 additions and 10 deletions

View File

@@ -25,6 +25,7 @@ export type Doc = { [key: string]: any }
export type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> export type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any>
export function getValueByPointer (document: any, pointer: string): any { export function getValueByPointer (document: any, pointer: string): any {
if (pointer === '/') return document
const pathArr = pointer.split('/') const pathArr = pointer.split('/')
pathArr.shift() pathArr.shift()
try { try {

View File

@@ -1,6 +1,5 @@
import { DBCache, Dump, Http, Revision, Update } from './types' import { DBCache, Dump, Http, Revision, Update } from './types'
import { BehaviorSubject, Observable } from 'rxjs' import { BehaviorSubject, Observable } from 'rxjs'
import { finalize } from 'rxjs/operators'
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'
@@ -34,9 +33,6 @@ export class Store<T> {
const path = `/${args.join('/')}` const path = `/${args.join('/')}`
if (!this.watchedNodes[path]) { if (!this.watchedNodes[path]) {
this.watchedNodes[path] = new BehaviorSubject(getValueByPointer(this.cache.data, path)) this.watchedNodes[path] = new BehaviorSubject(getValueByPointer(this.cache.data, path))
this.watchedNodes[path].pipe(
finalize(() => delete this.watchedNodes[path]),
)
} }
return this.watchedNodes[path].asObservable() return this.watchedNodes[path].asObservable()
} }
@@ -134,14 +130,19 @@ export class Store<T> {
} }
private updateWatchedNodes (revisionPath: string) { private updateWatchedNodes (revisionPath: string) {
const kill = (path: string) => {
this.watchedNodes[path].complete()
delete this.watchedNodes[path]
}
Object.keys(this.watchedNodes).forEach(path => { Object.keys(this.watchedNodes).forEach(path => {
if (this.watchedNodes[path].observers.length === 0) return kill(path)
if (path.includes(revisionPath) || revisionPath.includes(path)) { if (path.includes(revisionPath) || revisionPath.includes(path)) {
const val = getValueByPointer(this.cache.data, path) const val = getValueByPointer(this.cache.data, path)
if (val !== undefined) { if (val === undefined) return kill(path)
this.watchedNodes[path].next(val)
} else { this.watchedNodes[path].next(val)
this.watchedNodes[path].complete()
}
} }
}) })
} }
@@ -154,4 +155,4 @@ export class Store<T> {
private isRevision (update: Update<T>): update is Revision { private isRevision (update: Update<T>): update is Revision {
return !!(update as Revision).patch return !!(update as Revision).patch
} }
} }