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

88.88% Statements 72/81
80% Branches 24/30
100% Functions 9/9
88.88% Lines 72/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 174x 174x 1x 1x 1x 1x 1x 1x 1x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 30x 174x 174x 174x 1x 1x 148x 148x 148x 30x 30x 30x 30x 30x 30x 148x     148x 148x 148x 30x 30x 981x 981x 981x 2769x 2769x 24x 24x 24x 24x 360x 24x         981x     238x 238x 32x 32x   96x 981x 981x 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}`,
            );
    }
}