chore: address comments and fix some other issues

This commit is contained in:
waterplea
2022-05-09 16:50:43 +03:00
parent 4becce1084
commit f3bc3832a6
7 changed files with 33 additions and 39 deletions

View File

@@ -1,9 +1,5 @@
import { DBCache, PatchOp } from './types'
export interface Validator<T> {
(operation: Operation, index: number, doc: T, existingPathFragment: string): void
}
export interface BaseOperation {
path: string
}
@@ -15,7 +11,6 @@ export interface AddOperation<T> extends BaseOperation {
export interface RemoveOperation extends BaseOperation {
op: PatchOp.REMOVE
value?: undefined
}
export interface ReplaceOperation<T> extends BaseOperation {
@@ -23,25 +18,28 @@ export interface ReplaceOperation<T> extends BaseOperation {
value: T
}
export type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any>
export type Operation<T> = AddOperation<T> | RemoveOperation | ReplaceOperation<T>
export function getValueByPointer (doc: Record<string, any>, pointer: string): any {
if (pointer === '/') return doc
export function getValueByPointer<T extends Record<string, T>> (data: T, pointer: string): any {
if (pointer === '/') return data
try {
return pointer.split('/').slice(1).reduce((acc, next) => acc[next], doc)
return jsonPathToKeyArray(pointer).reduce((acc, next) => acc[next], data)
} catch (e) {
return undefined
}
}
export function applyOperation (doc: DBCache<Record<string, any>>, { path, op, value }: Operation): Operation | null {
export function applyOperation<T> (
doc: DBCache<Record<string, any>>,
{ path, op, value }: Operation<T> & { value?: T },
): Operation<T> | null {
const current = getValueByPointer(doc.data, path)
const remove = { op: PatchOp.REMOVE, path} as const
const add = { op: PatchOp.ADD, path, value: current} as const
const replace = { op: PatchOp.REPLACE, path, value: current } as const
doc.data = recursiveApply(doc.data, path.split('/').slice(1), value)
doc.data = recursiveApply(doc.data, jsonPathToKeyArray(path), value)
switch (op) {
case PatchOp.REMOVE:
@@ -76,8 +74,12 @@ function recursiveApply<T extends Record<string, T>> (data: T, path: readonly st
return result
}
function isObject (val: any): val is Record<string, any> {
function isObject (val: any): val is Record<string, unknown> {
return typeof val === 'object' && !Array.isArray(val) && val !== null
}
function jsonPathToKeyArray (path: string): string[] {
return path.split('/').slice(1)
}

View File

@@ -1,13 +1,13 @@
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { Update } from "../types";
import { Source } from "./source";
import { RPCResponse } from "./ws-source";
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { Update } from '../types'
import { Source } from './source'
import { RPCResponse } from './ws-source'
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>>> {
return this.seed.pipe(map((result) => ({ result, jsonrpc: "2.0" })));
watch$ (): Observable<RPCResponse<Update<T>>> {
return this.seed.pipe(map((result) => ({ result, jsonrpc: '2.0' })))
}
}

View File

@@ -1,4 +1,4 @@
import { BehaviorSubject, concat, from, Observable, of, Subject } from 'rxjs'
import { BehaviorSubject, concat, from, Observable, of } from 'rxjs'
import { concatMap, delay, map, skip, switchMap, take, tap } from 'rxjs/operators'
import { Store } from '../store'
import { Http, Update } from '../types'

View File

@@ -43,14 +43,6 @@ export interface RPCError extends RPCBase {
}
export type RPCResponse<T> = RPCSuccess<T> | RPCError
function isRpcError<Error, Result> (arg: { error: Error } | { result: Result}): arg is { error: Error } {
return !!(arg as any).error
}
function isRpcSuccess<Error, Result> (arg: { error: Error } | { result: Result}): arg is { result: Result } {
return !!(arg as any).result
}
class RpcError {
code: number
message: string
@@ -61,4 +53,4 @@ class RpcError {
this.message = e.message
this.details = e.data.details
}
}
}

View File

@@ -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 }> {
@@ -95,7 +95,7 @@ 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 {

View File

@@ -1,7 +1,7 @@
import { Operation } from './json-patch-lib'
// revise a collection of nodes.
export type Revision = { id: number, patch: Operation[], expireId: string | null }
export type Revision = { id: number, patch: Operation<unknown>[], expireId: string | null }
// dump/replace the entire store with T
export type Dump<T> = { id: number, value: T, expireId: string | null }

View File

@@ -383,9 +383,9 @@
}
},
"node_modules/minimist": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz",
"integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==",
"dev": true
},
"node_modules/mkdirp": {
@@ -905,9 +905,9 @@
}
},
"minimist": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz",
"integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==",
"dev": true
},
"mkdirp": {