All files / src/mock MockTable.ts

25% Statements 6/24
0% Branches 0/9
0% Functions 0/10
27.27% Lines 6/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58        1x   1x 1x 1x 1x     1x                                                                                          
// Package: com.lightningkite.lightningdb.mock
// Generated by Khrysalis - this file will be overwritten.
import { Condition } from '../Condition'
import { HasId } from '../HasId'
import { SignalData } from '../SignalData'
import { UUIDFor } from '../UUIDFor'
import { EqualOverrideMap, runOrNull } from '@lightningkite/khrysalis-runtime'
import { execPipe, filter, toArray } from 'iter-tools-es'
import { Observable, Subject } from 'rxjs'
import { map } from 'rxjs/operators'
 
//! Declares com.lightningkite.lightningdb.mock.MockTable
export class MockTable<Model extends HasId> {
    public constructor() {
        this.data = new EqualOverrideMap([]);
        this.signals = new Subject();
    }
    
    
    public readonly data: Map<UUIDFor<Model>, Model>;
    public readonly signals: Subject<SignalData<Model>>;
    
    public observe(condition: Condition<Model>): Observable<Array<Model>> {
        return this.signals.pipe(map((it: SignalData<Model>): Array<Model> => execPipe(this.data.values(), filter((it: Model): boolean => condition.invoke(it)), toArray)));
    }
    
    public getItem(id: UUIDFor<Model>): (Model | null) {
        return (this.data.get(id) ?? null);
    }
    
    public asList(): Array<Model> {
        return toArray(this.data.values());
    }
    
    public addItem(item: Model): Model {
        this.data.set(item._id, item);
        this.signals.next(new SignalData<Model>(item, true, false));
        return item;
    }
    
    public replaceItem(item: Model): Model {
        this.data.set(item._id, item);
        this.signals.next(new SignalData<Model>(item, false, false));
        return item;
    }
    
    public deleteItem(item: Model): void {
        this.deleteItemById(item._id);
    }
    
    public deleteItemById(id: UUIDFor<Model>): void {
        const item_16 = (this.data.get(id) ?? null);
        Iif (item_16 !== null) {
            this.data.delete(id);
            this.signals.next(new SignalData<Model>(item_16, false, true));
        }
    }
}