chore: add prettier and reformat lib

This commit is contained in:
waterplea
2022-07-24 17:53:24 +03:00
parent a39f777cb0
commit 897f791a72
12 changed files with 2600 additions and 112 deletions

6
client/.prettierrc Normal file
View File

@@ -0,0 +1,6 @@
{
"singleQuote": true,
"semi": false,
"arrowParens": "avoid",
"trailingComma": "all"
}

View File

@@ -18,9 +18,15 @@ export interface ReplaceOperation<T> extends BaseOperation {
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
try {
@@ -30,31 +36,32 @@ export function getValueByPointer<T extends Record<string, T>> (data: T, pointer
}
}
export function applyOperation<T> (
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 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, jsonPathToKeyArray(path), op, value)
switch (op) {
case PatchOp.REMOVE:
return current === undefined
? null
: add
return current === undefined ? null : add
case PatchOp.REPLACE:
case PatchOp.ADD:
return current === undefined
? remove
: replace
return current === undefined ? 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
// object
@@ -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 result = {
...data,
@@ -82,7 +94,12 @@ function recursiveApplyObject<T extends Record<string, any>> (data: T, path: rea
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 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
}
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)
}
function jsonPathToKeyArray (path: string): string[] {
function jsonPathToKeyArray(path: string): string[] {
return path.split('/').slice(1)
}

View File

@@ -11,15 +11,19 @@ export class PatchDB<T> {
public rpcError$ = new Subject<RPCError>()
public cache$ = new ReplaySubject<DBCache<T>>(1)
private sub = this.sources$.pipe(
switchMap(sources => merge(...sources.map(s => s.watch$(this.store))).pipe(
private sub = this.sources$
.pipe(
switchMap(sources =>
merge(...sources.map(s => s.watch$(this.store))).pipe(
catchError(e => {
this.connectionError$.next(e)
return EMPTY
}),
)),
).subscribe((res) => {
),
),
)
.subscribe(res => {
if ('result' in res) {
this.store.update(res.result)
this.cache$.next(this.store.cache)
@@ -28,13 +32,13 @@ export class PatchDB<T> {
}
})
constructor (
constructor(
private readonly sources$: Observable<Source<T>[]>,
private readonly http: Http<T>,
private readonly initialCache: DBCache<T>,
) { }
) {}
clean () {
clean() {
this.sub.unsubscribe()
}
}

View File

@@ -5,9 +5,9 @@ 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,5 +1,13 @@
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 { Http, Update } from '../types'
import { Source } from './source'
@@ -10,17 +18,15 @@ export type PollConfig = {
}
export class PollSource<T> implements Source<T> {
constructor (
constructor(
private readonly pollConfig: PollConfig,
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 updates$ = of({ })
.pipe(
const updates$ = of({}).pipe(
concatMap(_ => store.sequence$),
concatMap(seq => this.http.getRevisions(seq)),
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>>
}
}),
map(result => ({ result,
jsonrpc: '2.0' })),
map(result => ({ result, jsonrpc: '2.0' })),
)
}
}

View File

@@ -4,5 +4,5 @@ import { Update } from '../types'
import { RPCResponse } from './ws-source'
export interface Source<T> {
watch$ (store?: Store<T>): Observable<RPCResponse<Update<T>>>
watch$(store?: Store<T>): Observable<RPCResponse<Update<T>>>
}

View File

@@ -1,16 +1,18 @@
import { Observable } from 'rxjs'
import { webSocket, WebSocketSubject, WebSocketSubjectConfig } from 'rxjs/webSocket'
import {
webSocket,
WebSocketSubject,
WebSocketSubjectConfig,
} from 'rxjs/webSocket'
import { Update } from '../types'
import { Source } from './source'
export class WebsocketSource<T> implements Source<T> {
private websocket$: WebSocketSubject<RPCResponse<Update<T>>> | undefined
constructor (
private readonly url: string,
) { }
constructor(private readonly url: string) {}
watch$ (): Observable<RPCResponse<Update<T>>> {
watch$(): Observable<RPCResponse<Update<T>>> {
const fullConfig: WebSocketSubjectConfig<RPCResponse<Update<T>>> = {
url: this.url,
openObserver: {
@@ -48,7 +50,7 @@ class RpcError {
message: string
details: string
constructor (e: RPCError['error']) {
constructor(e: RPCError['error']) {
this.code = e.code
this.message = e.message
this.details = e.data.details

View File

@@ -11,10 +11,10 @@ export interface StashEntry {
export class Store<T extends { [key: string]: any }> {
cache: DBCache<T>
sequence$: BehaviorSubject<number>
private watchedNodes: { [path: string]: ReplaySubject<any> } = { }
private watchedNodes: { [path: string]: ReplaySubject<any> } = {}
private stash = new BTree<number, StashEntry>()
constructor (
constructor(
private readonly http: Http<T>,
private readonly initialCache: DBCache<T>,
) {
@@ -22,14 +22,81 @@ export class Store<T extends { [key: string]: any }> {
this.sequence$ = new BehaviorSubject(initialCache.sequence)
}
watch$ (): Observable<T>
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]>, 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> {
watch$(): Observable<T>
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]>,
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('/')}`
if (!this.watchedNodes[path]) {
this.watchedNodes[path] = new ReplaySubject(1)
@@ -38,7 +105,7 @@ export class Store<T extends { [key: string]: any }> {
return this.watchedNodes[path].asObservable()
}
update (update: Update<T>): void {
update(update: Update<T>): void {
if (this.isRevision(update)) {
// if old or known, 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())
this.watchedNodes = { }
this.watchedNodes = {}
this.stash.clear()
this.sequence$.next(0)
this.cache = {
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)
this.watchedNodes[path].next(value)
}
private handleRevision (revision: Revision): void {
private handleRevision(revision: Revision): void {
// stash the revision
this.stash.set(revision.id, { revision, undo: [] })
@@ -77,7 +144,7 @@ export class Store<T extends { [key: string]: any }> {
this.processStashed(revision.id)
}
private handleDump ({ value, id }: Dump<T>): void {
private handleDump({ value, id }: Dump<T>): void {
this.cache.data = { ...value }
this.stash.deleteRange(this.cache.sequence, id, false)
this.updateWatchedNodes('')
@@ -85,12 +152,12 @@ export class Store<T extends { [key: string]: any }> {
this.processStashed(id + 1)
}
private processStashed (id: number): void {
private processStashed(id: number): void {
this.undoRevisions(id)
this.applyRevisions(id)
}
private undoRevisions (id: number): void {
private undoRevisions(id: number): void {
let stashEntry = this.stash.get(this.stash.maxKey() as number)
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
while (revision) {
let undo: Operation<unknown>[] = []
@@ -136,7 +203,7 @@ export class Store<T extends { [key: string]: any }> {
this.stash.deleteRange(0, this.cache.sequence, false)
}
private updateWatchedNodes (revisionPath: string) {
private updateWatchedNodes(revisionPath: string) {
const kill = (path: string) => {
this.watchedNodes[path].complete()
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.sequence$.next(sequence)
}
private isRevision (update: Update<T>): update is Revision {
private isRevision(update: Update<T>): update is Revision {
return 'patch' in update
}
}

View File

@@ -1,9 +1,14 @@
import { Operation } from './json-patch-lib'
// 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
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>
@@ -14,16 +19,16 @@ export enum PatchOp {
}
export interface Http<T> {
getRevisions (since: number): Promise<Revision[] | Dump<T>>
getDump (): Promise<Dump<T>>
getRevisions(since: number): Promise<Revision[] | Dump<T>>
getDump(): Promise<Dump<T>>
}
export interface Bootstrapper<T> {
init (): Promise<DBCache<T>>
update (cache: DBCache<T>): Promise<void>
init(): Promise<DBCache<T>>
update(cache: DBCache<T>): Promise<void>
}
export interface DBCache<T extends Record<string, any>>{
sequence: number,
export interface DBCache<T extends Record<string, any>> {
sequence: number
data: T
}

2397
client/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,8 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc"
"build": "tsc",
"check": "tsc --noEmit --skipLibCheck"
},
"dependencies": {
"sorted-btree": "1.5.0",
@@ -19,8 +20,20 @@
"devDependencies": {
"@types/node": "16.4.13",
"@types/uuid": "8.3.1",
"husky": "^4.3.8",
"lint-staged": "^12.3.7",
"prettier": "^2.6.1",
"ts-node": "10.2.0",
"tslint": "6.1.3",
"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"
}
}

View File

@@ -2,17 +2,9 @@
"rules": {
"no-unused-variable": true,
"no-unused-expression": true,
"space-before-function-paren": true,
"semicolon": [
true,
"never"
],
"semicolon": [true, "never"],
"no-trailing-whitespace": true,
"indent": [
true,
"spaces",
2
],
"indent": [true, "spaces", 2],
"whitespace": [
true,
"check-branch",
@@ -24,8 +16,7 @@
"check-type",
"check-typecast",
"check-type-operator",
"check-preblock",
"check-postbrace"
"check-preblock"
],
"trailing-comma": [
true,
@@ -39,9 +30,6 @@
"singleline": "never"
}
],
"quotemark": [
true,
"single"
]
"quotemark": [true, "single"]
}
}