All files / src/live LiveObserveModelApi.ts

27.9% Statements 12/43
9.09% Branches 1/11
13.33% Functions 2/15
33.33% Lines 12/36

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 59 60 61 62 63 64 65 66 67 68 69 70      1x 1x 1x 1x 1x 1x   1x     1x                                   1x   1x     1x                       1x                                          
// Package: com.lightningkite.lightningdb.live
// Generated by Khrysalis - this file will be overwritten.
import { HasId } from '../HasId'
import { ListChange } from '../ListChange'
import { ObserveModelApi } from '../ObserveModelApi'
import { Query } from '../Query'
import { xListComparatorGet } from '../SortPart'
import { WebSocketIsh, multiplexedSocketReified } from './sockets'
import { Comparable, Comparator, EqualOverrideMap, compareBy, listRemoveAll, runOrNull, safeEq, xMutableMapGetOrPut } from '@lightningkite/khrysalis-runtime'
import { Observable } from 'rxjs'
import { finalize, map, publishReplay, refCount, switchMap } from 'rxjs/operators'
 
//! Declares com.lightningkite.lightningdb.live.LiveObserveModelApi
export class LiveObserveModelApi<Model extends HasId> extends ObserveModelApi<Model> {
    public constructor(public readonly openSocket: ((query: Query<Model>) => Observable<Array<Model>>)) {
        super();
        this.alreadyOpen = new EqualOverrideMap<Query<Model>, Observable<Array<Model>>>();
    }
    
    
    
    
    public readonly alreadyOpen: Map<Query<Model>, Observable<Array<Model>>>;
    
    public observe(query: Query<Model>): Observable<Array<Model>> {
        //multiplexedSocket<ListChange<Model>, Query<Model>>("$multiplexUrl?jwt=$token", path)
        return xMutableMapGetOrPut<Query<Model>, Observable<Array<Model>>>(this.alreadyOpen, query, (): Observable<Array<Model>> => this.openSocket(query).pipe(finalize((): void => {
            this.alreadyOpen.delete(query);
        })).pipe(publishReplay(1)).pipe(refCount()));
    }
}
export namespace LiveObserveModelApi {
    //! Declares com.lightningkite.lightningdb.live.LiveObserveModelApi.Companion
    export class Companion {
        private constructor() {
        }
        public static INSTANCE = new Companion();
        
        public create<Model extends HasId>(Model: Array<any>, multiplexUrl: string, token: string, path: string): LiveObserveModelApi<Model> {
            return new LiveObserveModelApi<Model>((query: Query<Model>): Observable<Array<Model>> => xObservableToListObservable<Model>(multiplexedSocketReified<ListChange<Model>, Query<Model>>([ListChange, Model], [Query, Model], `${multiplexUrl}?jwt=${token}`, path, undefined).pipe(switchMap((it: WebSocketIsh<ListChange<Model>, Query<Model>>): Observable<ListChange<Model>> => {
                it.send(query);
                return it.messages;
            })), xListComparatorGet(query.orderBy) ?? compareBy<Model>((it: Model): (Comparable<(any | null)> | null) => it._id)));
        }
    }
}
 
//! Declares com.lightningkite.lightningdb.live.toListObservable>io.reactivex.rxjava3.core.Observablecom.lightningkite.lightningdb.ListChangecom.lightningkite.lightningdb.live.toListObservable.T
export function xObservableToListObservable<T extends HasId>(this_: Observable<ListChange<T>>, ordering: Comparator<T>): Observable<Array<T>> {
    const localList = ([] as Array<T>);
    return this_.pipe(map((it: ListChange<T>): Array<T> => {
        const it_9 = it.wholeList;
        Iif (it_9 !== null) {
            localList.length = 0; localList.push(...it_9.slice().sort(ordering));
        }
        const it_11 = it._new;
        if (it_11 !== null) {
            listRemoveAll(localList, (o: T): boolean => safeEq(it_11._id, o._id));
            let index = localList.findIndex((inList: T): boolean => ordering(it_11, inList) < 0);
            Iif (index === (-1)) { index = localList.length }
            localList.splice(index, 0, it_11);
        } else {
            const it_18 = it.old;
            Iif (it_18 !== null) {
                listRemoveAll(localList, (o: T): boolean => safeEq(it_18._id, o._id));
            }
        }
        return localList;
    }));
}