All files / core/blong-gogo/src Local.ts

66.12% Statements 41/62
75% Branches 9/12
75% Functions 6/8
66.12% Lines 41/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 6x 6x 6x 6x 759x 759x 30x 30x 30x 30x 30x 30x 30x 564x 564x 564x 564x 564x 564x 564x 564x                   564x 564x 759x 759x 759x 759x 759x 759x 759x 759x                         3137x 3137x 3137x 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']>;
    }
}