add mock source

This commit is contained in:
Matt Hill
2021-11-30 21:00:33 -07:00
committed by Aiden McClelland
parent a724d6ec6b
commit 678d7b3322
3 changed files with 23 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
export * from './lib/source/mock-source'
export * from './lib/source/poll-source' export * from './lib/source/poll-source'
export * from './lib/source/ws-source' export * from './lib/source/ws-source'
export * from './lib/source/source' export * from './lib/source/source'

View File

@@ -1,3 +1,5 @@
import { PatchOp } from './types'
export interface Validator<T> { export interface Validator<T> {
(operation: Operation, index: number, doc: T, existingPathFragment: string): void (operation: Operation, index: number, doc: T, existingPathFragment: string): void
} }
@@ -7,16 +9,16 @@ export interface BaseOperation {
} }
export interface AddOperation<T> extends BaseOperation { export interface AddOperation<T> extends BaseOperation {
op: 'add' op: PatchOp.ADD
value: T value: T
} }
export interface RemoveOperation extends BaseOperation { export interface RemoveOperation extends BaseOperation {
op: 'remove' op: PatchOp.REMOVE
} }
export interface ReplaceOperation<T> extends BaseOperation { export interface ReplaceOperation<T> extends BaseOperation {
op: 'replace' op: PatchOp.REPLACE
value: T value: T
} }
@@ -55,13 +57,13 @@ export function applyOperation (doc: Doc, op: Operation): Operation | null {
node[key] = op.value node[key] = op.value
if (curVal) { if (curVal) {
undo = { undo = {
op: 'replace', op: PatchOp.REPLACE,
path: op.path, path: op.path,
value: curVal, value: curVal,
} }
} else { } else {
undo = { undo = {
op: 'remove', op: PatchOp.REMOVE,
path: op.path, path: op.path,
} }
} }
@@ -69,7 +71,7 @@ export function applyOperation (doc: Doc, op: Operation): Operation | null {
delete node[key] delete node[key]
if (curVal) { if (curVal) {
undo = { undo = {
op: 'add', op: PatchOp.ADD,
path: op.path, path: op.path,
value: curVal, value: curVal,
} }

View File

@@ -0,0 +1,14 @@
import { Observable } from 'rxjs'
import { Update } from '../types'
import { Source } from './source'
export class MockSource<T> implements Source<T> {
constructor (
private readonly seed: Observable<Update<T>>,
) { }
watch$ (): Observable<Update<T>> {
return this.seed
}
}