All files / blong-gogo/src Realm.ts

87.65% Statements 71/81
72.22% Branches 26/36
100% Functions 10/10
87.65% Lines 71/81

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 71 72 73 74 75 76 77 78 79 80 81 821x 1x 1x 1x 1x 1x 1x 1x 1x 63x 63x 1x 6x 6x 6x 6x 6x 6x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 5x 5x 5x 5x 5x 52x 52x 52x 5x           52x   21x 21x 21x 63x 21x 63x 273x 273x 273x 813x 813x 813x 813x 624x 78x 78x 78x 78x 813x 273x 273x 273x     10x 10x 2x 2x     273x 273x 1x  
import type {IAdapterRegistry, ILog, IRegistry} from '@feasibleone/blong/types';
 
export interface IRealm {
    addModule: (name: string, mod: IRegistry) => void;
    addLayer: (name: string, layer: IRealm) => void;
}
 
export default class RealmImpl implements IRealm {
    #registry: IRegistry;
    #log: ILog;
    #logger: ReturnType<ILog['logger']>;
    #config: {
        realm?: {logLevel?: Parameters<ILog['logger']>[0]};
        name: string;
        pkg: {name: string; version: string};
    };
 
    public constructor(
        config: {
            server: {realm?: {logLevel: Parameters<ILog['logger']>[0]}};
            browser: {realm?: {logLevel: Parameters<ILog['logger']>[0]}};
            name: string;
            pkg: {name: string; version: string};
        },
        {log, registry}: {log?: ILog; registry?: IRegistry},
        platform: 'server' | 'browser',
    ) {
        this.#config = config;
        this.#registry = registry!;
        this.#log = log!;
        this.#logger = this.#log?.logger(config[platform]?.realm?.logLevel ?? 'info', {
            name: config.name,
            context: `${platform}`,
        });
    }
 
    private _addModuleInternal(name: string | symbol, mod: IRegistry): void {
        let module = this.#registry.modules.get(name);
        if (!module) {
            module = [];
            this.#registry.modules.set(name, module);
        }
        module.push(mod);
    }

    public addModule(name: string | symbol, mod: IRegistry): void {
        this.#logger?.debug?.(
            {$meta: {mtid: 'event', method: 'module.add'}},
            `${this.#config.name}.${String(name)}`,
        );
        this._addModuleInternal(name, mod);
    }
 
    public addLayer(layerName: string, layer: IRealm): void {
        // this._addModuleInternal(this.#config.name, layer);
        const source: string[] = [];
        Object.entries(layer).forEach(([itemName, item]) => {
            if (item.source) source.push(item.source);
            const id = `${this.#config.name}.${itemName}`;
            if (typeof item === 'object' && 'port' in item)
                this.#registry.ports.set(id, item.port as unknown as IAdapterRegistry);
            else if (typeof item === 'object' && 'methods' in item) {
                const methods = this.#registry.methods.get(id);
                if (methods) methods.push(...item.methods);
                else this.#registry.methods.set(id, item.methods);
            }
        });
        if (source.length === 1)
            // this.#logger?.debug?.(`Layer ${this.#config.name}.${layerName} ${source[0]}`);
            this.#logger?.debug?.(
                {$meta: {mtid: 'event', method: 'layer.add'}},
                `${this.#config.name}.${layerName} ${source[0]}`,
            );
        else if (!source.length) this.#logger?.debug?.(`Layer ${this.#config.name}.${layerName}`);
        else
            this.#logger?.debug?.(
                {$meta: {mtid: 'event', method: 'layer.add'}},
                `${this.#config.name}.${layerName} ${source}`,
            );
    }
}