mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 10:21:53 +00:00
chore: add prettier and reformat lib
This commit is contained in:
6
client/.prettierrc
Normal file
6
client/.prettierrc
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"singleQuote": true,
|
||||||
|
"semi": false,
|
||||||
|
"arrowParens": "avoid",
|
||||||
|
"trailingComma": "all"
|
||||||
|
}
|
||||||
@@ -18,9 +18,15 @@ export interface ReplaceOperation<T> extends BaseOperation {
|
|||||||
value: T
|
value: T
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Operation<T> = AddOperation<T> | RemoveOperation | ReplaceOperation<T>
|
export type Operation<T> =
|
||||||
|
| AddOperation<T>
|
||||||
|
| RemoveOperation
|
||||||
|
| ReplaceOperation<T>
|
||||||
|
|
||||||
export function getValueByPointer<T extends Record<string, T>> (data: T, pointer: string): any {
|
export function getValueByPointer<T extends Record<string, T>>(
|
||||||
|
data: T,
|
||||||
|
pointer: string,
|
||||||
|
): any {
|
||||||
if (pointer === '/') return data
|
if (pointer === '/') return data
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -30,37 +36,38 @@ export function getValueByPointer<T extends Record<string, T>> (data: T, pointer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyOperation<T> (
|
export function applyOperation<T>(
|
||||||
doc: DBCache<Record<string, any>>,
|
doc: DBCache<Record<string, any>>,
|
||||||
{ path, op, value }: Operation<T> & { value?: T },
|
{ path, op, value }: Operation<T> & { value?: T },
|
||||||
): Operation<T> | null {
|
): Operation<T> | null {
|
||||||
const current = getValueByPointer(doc.data, path)
|
const current = getValueByPointer(doc.data, path)
|
||||||
const remove = { op: PatchOp.REMOVE, path} as const
|
const remove = { op: PatchOp.REMOVE, path } as const
|
||||||
const add = { op: PatchOp.ADD, path, value: current} as const
|
const add = { op: PatchOp.ADD, path, value: current } as const
|
||||||
const replace = { op: PatchOp.REPLACE, path, value: current } as const
|
const replace = { op: PatchOp.REPLACE, path, value: current } as const
|
||||||
|
|
||||||
doc.data = recursiveApply(doc.data, jsonPathToKeyArray(path), op, value)
|
doc.data = recursiveApply(doc.data, jsonPathToKeyArray(path), op, value)
|
||||||
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case PatchOp.REMOVE:
|
case PatchOp.REMOVE:
|
||||||
return current === undefined
|
return current === undefined ? null : add
|
||||||
? null
|
|
||||||
: add
|
|
||||||
case PatchOp.REPLACE:
|
case PatchOp.REPLACE:
|
||||||
case PatchOp.ADD:
|
case PatchOp.ADD:
|
||||||
return current === undefined
|
return current === undefined ? remove : replace
|
||||||
? remove
|
|
||||||
: replace
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function recursiveApply<T extends Record<string, any> | any[]> (data: T, path: readonly string[], op: PatchOp, value?: any): T {
|
function recursiveApply<T extends Record<string, any> | any[]>(
|
||||||
|
data: T,
|
||||||
|
path: readonly string[],
|
||||||
|
op: PatchOp,
|
||||||
|
value?: any,
|
||||||
|
): T {
|
||||||
if (!path.length) return value
|
if (!path.length) return value
|
||||||
|
|
||||||
// object
|
// object
|
||||||
if (isObject(data)) {
|
if (isObject(data)) {
|
||||||
return recursiveApplyObject(data, path, op, value)
|
return recursiveApplyObject(data, path, op, value)
|
||||||
// array
|
// array
|
||||||
} else if (Array.isArray(data)) {
|
} else if (Array.isArray(data)) {
|
||||||
return recursiveApplyArray(data, path, op, value)
|
return recursiveApplyArray(data, path, op, value)
|
||||||
} else {
|
} else {
|
||||||
@@ -68,7 +75,12 @@ function recursiveApply<T extends Record<string, any> | any[]> (data: T, path: r
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function recursiveApplyObject<T extends Record<string, any>> (data: T, path: readonly string[], op: PatchOp, value?: any): T {
|
function recursiveApplyObject<T extends Record<string, any>>(
|
||||||
|
data: T,
|
||||||
|
path: readonly string[],
|
||||||
|
op: PatchOp,
|
||||||
|
value?: any,
|
||||||
|
): T {
|
||||||
const updated = recursiveApply(data[path[0]], path.slice(1), op, value)
|
const updated = recursiveApply(data[path[0]], path.slice(1), op, value)
|
||||||
const result = {
|
const result = {
|
||||||
...data,
|
...data,
|
||||||
@@ -82,7 +94,12 @@ function recursiveApplyObject<T extends Record<string, any>> (data: T, path: rea
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
function recursiveApplyArray<T extends any[]> (data: T, path: readonly string[], op: PatchOp, value?: any): T {
|
function recursiveApplyArray<T extends any[]>(
|
||||||
|
data: T,
|
||||||
|
path: readonly string[],
|
||||||
|
op: PatchOp,
|
||||||
|
value?: any,
|
||||||
|
): T {
|
||||||
const index = parseInt(path[0])
|
const index = parseInt(path[0])
|
||||||
|
|
||||||
const updated = recursiveApply(data[index], path.slice(1), op, value)
|
const updated = recursiveApply(data[index], path.slice(1), op, value)
|
||||||
@@ -98,12 +115,10 @@ function recursiveApplyArray<T extends any[]> (data: T, path: readonly string[],
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
function isObject (val: any): val is Record<string, unknown> {
|
function isObject(val: any): val is Record<string, unknown> {
|
||||||
return typeof val === 'object' && val !== null && !Array.isArray(val)
|
return typeof val === 'object' && val !== null && !Array.isArray(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
function jsonPathToKeyArray (path: string): string[] {
|
function jsonPathToKeyArray(path: string): string[] {
|
||||||
return path.split('/').slice(1)
|
return path.split('/').slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,30 +11,34 @@ export class PatchDB<T> {
|
|||||||
public rpcError$ = new Subject<RPCError>()
|
public rpcError$ = new Subject<RPCError>()
|
||||||
public cache$ = new ReplaySubject<DBCache<T>>(1)
|
public cache$ = new ReplaySubject<DBCache<T>>(1)
|
||||||
|
|
||||||
private sub = this.sources$.pipe(
|
private sub = this.sources$
|
||||||
switchMap(sources => merge(...sources.map(s => s.watch$(this.store))).pipe(
|
.pipe(
|
||||||
catchError(e => {
|
switchMap(sources =>
|
||||||
this.connectionError$.next(e)
|
merge(...sources.map(s => s.watch$(this.store))).pipe(
|
||||||
|
catchError(e => {
|
||||||
|
this.connectionError$.next(e)
|
||||||
|
|
||||||
return EMPTY
|
return EMPTY
|
||||||
}),
|
}),
|
||||||
)),
|
),
|
||||||
).subscribe((res) => {
|
),
|
||||||
if ('result' in res) {
|
)
|
||||||
this.store.update(res.result)
|
.subscribe(res => {
|
||||||
this.cache$.next(this.store.cache)
|
if ('result' in res) {
|
||||||
} else {
|
this.store.update(res.result)
|
||||||
this.rpcError$.next(res)
|
this.cache$.next(this.store.cache)
|
||||||
}
|
} else {
|
||||||
})
|
this.rpcError$.next(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
constructor (
|
constructor(
|
||||||
private readonly sources$: Observable<Source<T>[]>,
|
private readonly sources$: Observable<Source<T>[]>,
|
||||||
private readonly http: Http<T>,
|
private readonly http: Http<T>,
|
||||||
private readonly initialCache: DBCache<T>,
|
private readonly initialCache: DBCache<T>,
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
clean () {
|
clean() {
|
||||||
this.sub.unsubscribe()
|
this.sub.unsubscribe()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import { Source } from './source'
|
|||||||
import { RPCResponse } from './ws-source'
|
import { RPCResponse } from './ws-source'
|
||||||
|
|
||||||
export class MockSource<T> implements Source<T> {
|
export class MockSource<T> implements Source<T> {
|
||||||
constructor (private readonly seed: Observable<Update<T>>) { }
|
constructor(private readonly seed: Observable<Update<T>>) {}
|
||||||
|
|
||||||
watch$ (): Observable<RPCResponse<Update<T>>> {
|
watch$(): Observable<RPCResponse<Update<T>>> {
|
||||||
return this.seed.pipe(map((result) => ({ result, jsonrpc: '2.0' })))
|
return this.seed.pipe(map(result => ({ result, jsonrpc: '2.0' })))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
import { BehaviorSubject, concat, from, Observable, of } from 'rxjs'
|
import { BehaviorSubject, concat, from, Observable, of } from 'rxjs'
|
||||||
import { concatMap, delay, map, skip, switchMap, take, tap } from 'rxjs/operators'
|
import {
|
||||||
|
concatMap,
|
||||||
|
delay,
|
||||||
|
map,
|
||||||
|
skip,
|
||||||
|
switchMap,
|
||||||
|
take,
|
||||||
|
tap,
|
||||||
|
} from 'rxjs/operators'
|
||||||
import { Store } from '../store'
|
import { Store } from '../store'
|
||||||
import { Http, Update } from '../types'
|
import { Http, Update } from '../types'
|
||||||
import { Source } from './source'
|
import { Source } from './source'
|
||||||
@@ -10,17 +18,15 @@ export type PollConfig = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class PollSource<T> implements Source<T> {
|
export class PollSource<T> implements Source<T> {
|
||||||
|
constructor(
|
||||||
constructor (
|
|
||||||
private readonly pollConfig: PollConfig,
|
private readonly pollConfig: PollConfig,
|
||||||
private readonly http: Http<T>,
|
private readonly http: Http<T>,
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
watch$ (store: Store<T>): Observable<RPCResponse<Update<T>>> {
|
watch$(store: Store<T>): Observable<RPCResponse<Update<T>>> {
|
||||||
const polling$ = new BehaviorSubject('')
|
const polling$ = new BehaviorSubject('')
|
||||||
|
|
||||||
const updates$ = of({ })
|
const updates$ = of({}).pipe(
|
||||||
.pipe(
|
|
||||||
concatMap(_ => store.sequence$),
|
concatMap(_ => store.sequence$),
|
||||||
concatMap(seq => this.http.getRevisions(seq)),
|
concatMap(seq => this.http.getRevisions(seq)),
|
||||||
take(1),
|
take(1),
|
||||||
@@ -43,8 +49,7 @@ export class PollSource<T> implements Source<T> {
|
|||||||
return of(res) // takes Dump<T> and converts it into Observable<Dump<T>>
|
return of(res) // takes Dump<T> and converts it into Observable<Dump<T>>
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
map(result => ({ result,
|
map(result => ({ result, jsonrpc: '2.0' })),
|
||||||
jsonrpc: '2.0' })),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,5 @@ import { Update } from '../types'
|
|||||||
import { RPCResponse } from './ws-source'
|
import { RPCResponse } from './ws-source'
|
||||||
|
|
||||||
export interface Source<T> {
|
export interface Source<T> {
|
||||||
watch$ (store?: Store<T>): Observable<RPCResponse<Update<T>>>
|
watch$(store?: Store<T>): Observable<RPCResponse<Update<T>>>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
import { Observable } from 'rxjs'
|
import { Observable } from 'rxjs'
|
||||||
import { webSocket, WebSocketSubject, WebSocketSubjectConfig } from 'rxjs/webSocket'
|
import {
|
||||||
|
webSocket,
|
||||||
|
WebSocketSubject,
|
||||||
|
WebSocketSubjectConfig,
|
||||||
|
} from 'rxjs/webSocket'
|
||||||
import { Update } from '../types'
|
import { Update } from '../types'
|
||||||
import { Source } from './source'
|
import { Source } from './source'
|
||||||
|
|
||||||
export class WebsocketSource<T> implements Source<T> {
|
export class WebsocketSource<T> implements Source<T> {
|
||||||
private websocket$: WebSocketSubject<RPCResponse<Update<T>>> | undefined
|
private websocket$: WebSocketSubject<RPCResponse<Update<T>>> | undefined
|
||||||
|
|
||||||
constructor (
|
constructor(private readonly url: string) {}
|
||||||
private readonly url: string,
|
|
||||||
) { }
|
|
||||||
|
|
||||||
watch$ (): Observable<RPCResponse<Update<T>>> {
|
watch$(): Observable<RPCResponse<Update<T>>> {
|
||||||
const fullConfig: WebSocketSubjectConfig<RPCResponse<Update<T>>> = {
|
const fullConfig: WebSocketSubjectConfig<RPCResponse<Update<T>>> = {
|
||||||
url: this.url,
|
url: this.url,
|
||||||
openObserver: {
|
openObserver: {
|
||||||
@@ -48,7 +50,7 @@ class RpcError {
|
|||||||
message: string
|
message: string
|
||||||
details: string
|
details: string
|
||||||
|
|
||||||
constructor (e: RPCError['error']) {
|
constructor(e: RPCError['error']) {
|
||||||
this.code = e.code
|
this.code = e.code
|
||||||
this.message = e.message
|
this.message = e.message
|
||||||
this.details = e.data.details
|
this.details = e.data.details
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ export interface StashEntry {
|
|||||||
export class Store<T extends { [key: string]: any }> {
|
export class Store<T extends { [key: string]: any }> {
|
||||||
cache: DBCache<T>
|
cache: DBCache<T>
|
||||||
sequence$: BehaviorSubject<number>
|
sequence$: BehaviorSubject<number>
|
||||||
private watchedNodes: { [path: string]: ReplaySubject<any> } = { }
|
private watchedNodes: { [path: string]: ReplaySubject<any> } = {}
|
||||||
private stash = new BTree<number, StashEntry>()
|
private stash = new BTree<number, StashEntry>()
|
||||||
|
|
||||||
constructor (
|
constructor(
|
||||||
private readonly http: Http<T>,
|
private readonly http: Http<T>,
|
||||||
private readonly initialCache: DBCache<T>,
|
private readonly initialCache: DBCache<T>,
|
||||||
) {
|
) {
|
||||||
@@ -22,14 +22,81 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
this.sequence$ = new BehaviorSubject(initialCache.sequence)
|
this.sequence$ = new BehaviorSubject(initialCache.sequence)
|
||||||
}
|
}
|
||||||
|
|
||||||
watch$ (): Observable<T>
|
watch$(): Observable<T>
|
||||||
watch$<P1 extends keyof T> (p1: P1): Observable<NonNullable<T[P1]>>
|
watch$<P1 extends keyof T>(p1: P1): Observable<NonNullable<T[P1]>>
|
||||||
watch$<P1 extends keyof T, P2 extends keyof NonNullable<T[P1]>> (p1: P1, p2: P2): Observable<NonNullable<NonNullable<T[P1]>[P2]>>
|
watch$<P1 extends keyof T, P2 extends keyof NonNullable<T[P1]>>(
|
||||||
watch$<P1 extends keyof T, P2 extends keyof NonNullable<T[P1]>, P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>> (p1: P1, p2: P2, p3: P3): Observable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>>
|
p1: P1,
|
||||||
watch$<P1 extends keyof T, P2 extends keyof NonNullable<T[P1]>, P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>, P4 extends keyof NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>> (p1: P1, p2: P2, p3: P3, p4: P4): Observable<NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>>
|
p2: P2,
|
||||||
watch$<P1 extends keyof T, P2 extends keyof NonNullable<T[P1]>, P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>, P4 extends keyof NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>, P5 extends keyof NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>> (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5): Observable<NonNullable<NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>[P5]>>
|
): Observable<NonNullable<NonNullable<T[P1]>[P2]>>
|
||||||
watch$<P1 extends keyof T, P2 extends keyof NonNullable<T[P1]>, P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>, P4 extends keyof NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>, P5 extends keyof NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>, P6 extends keyof NonNullable<NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>[P5]>> (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6): Observable<NonNullable<NonNullable<NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>[P5]>[P6]>>
|
watch$<
|
||||||
watch$ (...args: (string | number)[]): Observable<any> {
|
P1 extends keyof T,
|
||||||
|
P2 extends keyof NonNullable<T[P1]>,
|
||||||
|
P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>,
|
||||||
|
>(
|
||||||
|
p1: P1,
|
||||||
|
p2: P2,
|
||||||
|
p3: P3,
|
||||||
|
): Observable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>>
|
||||||
|
watch$<
|
||||||
|
P1 extends keyof T,
|
||||||
|
P2 extends keyof NonNullable<T[P1]>,
|
||||||
|
P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>,
|
||||||
|
P4 extends keyof NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>,
|
||||||
|
>(
|
||||||
|
p1: P1,
|
||||||
|
p2: P2,
|
||||||
|
p3: P3,
|
||||||
|
p4: P4,
|
||||||
|
): Observable<
|
||||||
|
NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>
|
||||||
|
>
|
||||||
|
watch$<
|
||||||
|
P1 extends keyof T,
|
||||||
|
P2 extends keyof NonNullable<T[P1]>,
|
||||||
|
P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>,
|
||||||
|
P4 extends keyof NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>,
|
||||||
|
P5 extends keyof NonNullable<
|
||||||
|
NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]
|
||||||
|
>,
|
||||||
|
>(
|
||||||
|
p1: P1,
|
||||||
|
p2: P2,
|
||||||
|
p3: P3,
|
||||||
|
p4: P4,
|
||||||
|
p5: P5,
|
||||||
|
): Observable<
|
||||||
|
NonNullable<
|
||||||
|
NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>[P5]
|
||||||
|
>
|
||||||
|
>
|
||||||
|
watch$<
|
||||||
|
P1 extends keyof T,
|
||||||
|
P2 extends keyof NonNullable<T[P1]>,
|
||||||
|
P3 extends keyof NonNullable<NonNullable<T[P1]>[P2]>,
|
||||||
|
P4 extends keyof NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>,
|
||||||
|
P5 extends keyof NonNullable<
|
||||||
|
NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]
|
||||||
|
>,
|
||||||
|
P6 extends keyof NonNullable<
|
||||||
|
NonNullable<NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]>[P5]
|
||||||
|
>,
|
||||||
|
>(
|
||||||
|
p1: P1,
|
||||||
|
p2: P2,
|
||||||
|
p3: P3,
|
||||||
|
p4: P4,
|
||||||
|
p5: P5,
|
||||||
|
p6: P6,
|
||||||
|
): Observable<
|
||||||
|
NonNullable<
|
||||||
|
NonNullable<
|
||||||
|
NonNullable<
|
||||||
|
NonNullable<NonNullable<NonNullable<T[P1]>[P2]>[P3]>[P4]
|
||||||
|
>[P5]
|
||||||
|
>[P6]
|
||||||
|
>
|
||||||
|
>
|
||||||
|
watch$(...args: (string | number)[]): Observable<any> {
|
||||||
const path = `/${args.join('/')}`
|
const path = `/${args.join('/')}`
|
||||||
if (!this.watchedNodes[path]) {
|
if (!this.watchedNodes[path]) {
|
||||||
this.watchedNodes[path] = new ReplaySubject(1)
|
this.watchedNodes[path] = new ReplaySubject(1)
|
||||||
@@ -38,7 +105,7 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
return this.watchedNodes[path].asObservable()
|
return this.watchedNodes[path].asObservable()
|
||||||
}
|
}
|
||||||
|
|
||||||
update (update: Update<T>): void {
|
update(update: Update<T>): void {
|
||||||
if (this.isRevision(update)) {
|
if (this.isRevision(update)) {
|
||||||
// if old or known, return
|
// if old or known, return
|
||||||
if (update.id <= this.cache.sequence || this.stash.get(update.id)) return
|
if (update.id <= this.cache.sequence || this.stash.get(update.id)) return
|
||||||
@@ -48,24 +115,24 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reset (): void {
|
reset(): void {
|
||||||
Object.values(this.watchedNodes).forEach(node => node.complete())
|
Object.values(this.watchedNodes).forEach(node => node.complete())
|
||||||
this.watchedNodes = { }
|
this.watchedNodes = {}
|
||||||
this.stash.clear()
|
this.stash.clear()
|
||||||
this.sequence$.next(0)
|
this.sequence$.next(0)
|
||||||
this.cache = {
|
this.cache = {
|
||||||
sequence: 0,
|
sequence: 0,
|
||||||
data: { } as any,
|
data: {} as any,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateValue (path: string): void {
|
private updateValue(path: string): void {
|
||||||
const value = getValueByPointer(this.cache.data, path)
|
const value = getValueByPointer(this.cache.data, path)
|
||||||
|
|
||||||
this.watchedNodes[path].next(value)
|
this.watchedNodes[path].next(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleRevision (revision: Revision): void {
|
private handleRevision(revision: Revision): void {
|
||||||
// stash the revision
|
// stash the revision
|
||||||
this.stash.set(revision.id, { revision, undo: [] })
|
this.stash.set(revision.id, { revision, undo: [] })
|
||||||
|
|
||||||
@@ -77,7 +144,7 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
this.processStashed(revision.id)
|
this.processStashed(revision.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleDump ({ value, id }: Dump<T>): void {
|
private handleDump({ value, id }: Dump<T>): void {
|
||||||
this.cache.data = { ...value }
|
this.cache.data = { ...value }
|
||||||
this.stash.deleteRange(this.cache.sequence, id, false)
|
this.stash.deleteRange(this.cache.sequence, id, false)
|
||||||
this.updateWatchedNodes('')
|
this.updateWatchedNodes('')
|
||||||
@@ -85,12 +152,12 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
this.processStashed(id + 1)
|
this.processStashed(id + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
private processStashed (id: number): void {
|
private processStashed(id: number): void {
|
||||||
this.undoRevisions(id)
|
this.undoRevisions(id)
|
||||||
this.applyRevisions(id)
|
this.applyRevisions(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
private undoRevisions (id: number): void {
|
private undoRevisions(id: number): void {
|
||||||
let stashEntry = this.stash.get(this.stash.maxKey() as number)
|
let stashEntry = this.stash.get(this.stash.maxKey() as number)
|
||||||
|
|
||||||
while (stashEntry && stashEntry.revision.id > id) {
|
while (stashEntry && stashEntry.revision.id > id) {
|
||||||
@@ -99,7 +166,7 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private applyRevisions (id: number): void {
|
private applyRevisions(id: number): void {
|
||||||
let revision = this.stash.get(id)?.revision
|
let revision = this.stash.get(id)?.revision
|
||||||
while (revision) {
|
while (revision) {
|
||||||
let undo: Operation<unknown>[] = []
|
let undo: Operation<unknown>[] = []
|
||||||
@@ -136,7 +203,7 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
this.stash.deleteRange(0, this.cache.sequence, false)
|
this.stash.deleteRange(0, this.cache.sequence, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateWatchedNodes (revisionPath: string) {
|
private updateWatchedNodes(revisionPath: string) {
|
||||||
const kill = (path: string) => {
|
const kill = (path: string) => {
|
||||||
this.watchedNodes[path].complete()
|
this.watchedNodes[path].complete()
|
||||||
delete this.watchedNodes[path]
|
delete this.watchedNodes[path]
|
||||||
@@ -151,12 +218,12 @@ export class Store<T extends { [key: string]: any }> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateSequence (sequence: number): void {
|
private updateSequence(sequence: number): void {
|
||||||
this.cache.sequence = sequence
|
this.cache.sequence = sequence
|
||||||
this.sequence$.next(sequence)
|
this.sequence$.next(sequence)
|
||||||
}
|
}
|
||||||
|
|
||||||
private isRevision (update: Update<T>): update is Revision {
|
private isRevision(update: Update<T>): update is Revision {
|
||||||
return 'patch' in update
|
return 'patch' in update
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
import { Operation } from './json-patch-lib'
|
import { Operation } from './json-patch-lib'
|
||||||
|
|
||||||
// revise a collection of nodes.
|
// revise a collection of nodes.
|
||||||
export type Revision = { id: number, patch: Operation<unknown>[], expireId: string | null }
|
export type Revision = {
|
||||||
|
id: number
|
||||||
|
patch: Operation<unknown>[]
|
||||||
|
expireId: string | null
|
||||||
|
}
|
||||||
|
|
||||||
// dump/replace the entire store with T
|
// dump/replace the entire store with T
|
||||||
export type Dump<T> = { id: number, value: T, expireId: string | null }
|
export type Dump<T> = { id: number; value: T; expireId: string | null }
|
||||||
|
|
||||||
export type Update<T> = Revision | Dump<T>
|
export type Update<T> = Revision | Dump<T>
|
||||||
|
|
||||||
@@ -14,16 +19,16 @@ export enum PatchOp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Http<T> {
|
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> {
|
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 Record<string, any>>{
|
export interface DBCache<T extends Record<string, any>> {
|
||||||
sequence: number,
|
sequence: number
|
||||||
data: T
|
data: T
|
||||||
}
|
}
|
||||||
|
|||||||
2397
client/package-lock.json
generated
2397
client/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -7,7 +7,8 @@
|
|||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc"
|
"build": "tsc",
|
||||||
|
"check": "tsc --noEmit --skipLibCheck"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"sorted-btree": "1.5.0",
|
"sorted-btree": "1.5.0",
|
||||||
@@ -19,8 +20,20 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "16.4.13",
|
"@types/node": "16.4.13",
|
||||||
"@types/uuid": "8.3.1",
|
"@types/uuid": "8.3.1",
|
||||||
|
"husky": "^4.3.8",
|
||||||
|
"lint-staged": "^12.3.7",
|
||||||
|
"prettier": "^2.6.1",
|
||||||
"ts-node": "10.2.0",
|
"ts-node": "10.2.0",
|
||||||
"tslint": "6.1.3",
|
"tslint": "6.1.3",
|
||||||
"typescript": "4.3.5"
|
"typescript": "4.3.5"
|
||||||
|
},
|
||||||
|
"husky": {
|
||||||
|
"hooks": {
|
||||||
|
"pre-commit": "lint-staged && npm run check"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"lint-staged": {
|
||||||
|
"**/*.{js,ts,html,md,less,json}": "prettier --write",
|
||||||
|
"*.ts": "tslint --fix"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,9 @@
|
|||||||
"rules": {
|
"rules": {
|
||||||
"no-unused-variable": true,
|
"no-unused-variable": true,
|
||||||
"no-unused-expression": true,
|
"no-unused-expression": true,
|
||||||
"space-before-function-paren": true,
|
"semicolon": [true, "never"],
|
||||||
"semicolon": [
|
|
||||||
true,
|
|
||||||
"never"
|
|
||||||
],
|
|
||||||
"no-trailing-whitespace": true,
|
"no-trailing-whitespace": true,
|
||||||
"indent": [
|
"indent": [true, "spaces", 2],
|
||||||
true,
|
|
||||||
"spaces",
|
|
||||||
2
|
|
||||||
],
|
|
||||||
"whitespace": [
|
"whitespace": [
|
||||||
true,
|
true,
|
||||||
"check-branch",
|
"check-branch",
|
||||||
@@ -24,8 +16,7 @@
|
|||||||
"check-type",
|
"check-type",
|
||||||
"check-typecast",
|
"check-typecast",
|
||||||
"check-type-operator",
|
"check-type-operator",
|
||||||
"check-preblock",
|
"check-preblock"
|
||||||
"check-postbrace"
|
|
||||||
],
|
],
|
||||||
"trailing-comma": [
|
"trailing-comma": [
|
||||||
true,
|
true,
|
||||||
@@ -39,9 +30,6 @@
|
|||||||
"singleline": "never"
|
"singleline": "never"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"quotemark": [
|
"quotemark": [true, "single"]
|
||||||
true,
|
|
||||||
"single"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user