All files / blong-gogo/src Local.ts

79.03% Statements 49/62
87.5% Branches 7/8
66.66% Functions 4/6
79.03% Lines 49/62

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 631x 1x 1x 1x 1x 1x 238x 238x 238x 238x 238x 238x 28x 238x 210x 210x 238x 1x 1x 184x 184x 184x 184x 184x 184x 184x 184x                   184x 184x 238x 238x 238x 238x 238x 238x 238x 238x 184x 184x 1x 1x     1x 1x     1x 1x 552x 552x 552x 1x  
import {type ILocal, Internal} from '@feasibleone/blong/types';
 
export default class Local extends Internal implements ILocal {
    #mapLocal: Record<string, {method: (...params: unknown[]) => Promise<unknown[]>}> = {};
 
    private _localRegister(
        namespace: string,
        name: string,
        method: (...params: unknown[]) => Promise<unknown[]>,
    ): void {
        const local = this.#mapLocal[namespace + '.' + name];
        if (local) {
            local.method = method;
        } else {
            this.#mapLocal[namespace + '.' + name] = {method};
        }
    }
 
    public register(
        methods:
            | Record<string, (...params: unknown[]) => Promise<unknown>>
            | Array<(...params: unknown[]) => Promise<unknown>>,
        namespace: string,
        // reply: boolean,
        // pkg: {version: string},
    ): void {
        if (methods instanceof Array) {
            methods.forEach(fn => {
                if (fn instanceof Function && fn.name) {
                    this._localRegister(
                        namespace,
                        fn.name,
                        fn as (...params: unknown[]) => Promise<unknown[]>,
                    );
                }
            });
        } else {
            Object.keys(methods).forEach(key => {
                if ((methods as Record<string, unknown>)[key] instanceof Function) {
                    this._localRegister(
                        namespace,
                        key,
                        methods[key].bind(methods) as (...params: unknown[]) => Promise<unknown[]>,
                    );
                }
            });
        }
    }
 
    private _localUnregister(namespace: string, name: string): void {
        delete this.#mapLocal[namespace + '.' + name];
    }
 
    public unregister(methods: string[], namespace: string): void {
        methods.forEach(fn => this._localUnregister(namespace, fn));
    }
 
    public get(name: string): ReturnType<ILocal['get']> {
        // if (!this.#mapLocal[name]) console.log({name, local: this.#mapLocal});
        return this.#mapLocal[name] as ReturnType<ILocal['get']>;
    }
}