core_base.js

/**
 * @file src/core/base.js
 * @copyright @spmhome @_2025
 * @author Scott Meesseman @spmeesseman
 *//** */

const globalEnv = require("../utils/global");
const { spmhUtilsCmn } = require("@spmhome/cmn-utils");
const { spmhUtilsType } = require("@spmhome/type-utils");
const { SpmhMessageUtils } = require("@spmhome/log-utils");


/**
 * @implements {IDisposable}
 */
class WpwBase
{
    /**
     * @protected
     * @type {boolean}
     */
    disposed = false;
    /**
     * @protected
     * @type {IDisposable[]}
     */
    disposables = [];
    /**
     * @protected
     * @type {any}
     * @description cloned initial configuration object prior to any runtime modifications
     */
    initialConfig;
    /**
     * @protected
     * @type {string}
     * @description module name
     */
    name;
    /**
     * @protected
     * @type {string}
     * @description url/path friendly module name
     */
    slug;


    /**
     * @param {WpwBaseModuleConfig} [config]
     */
	constructor(config)
    {
        this.name = this.constructor.name;
        this.slug = config?.slug || this.name.toLowerCase();
        this.initialConfig = this._obj_.apply({}, this._obj_.pickNot(config || {}, "build"));
    }


    dispose()
    {
        if (!this.disposed)
        {   this.disposables.reverse().splice(0).forEach((d) => d.dispose());
            this.disposed = true;
        }
    }


    /**
     * @public
     */
    get global() { return globalEnv; }


    /**
     * @protected
     */
    get Msg() { return SpmhMessageUtils; }
    /**
     * @protected
     */
    get MsgCode() { return SpmhMessageUtils.Code; }
    /**
     * @protected
     */
    get ErrorCode() { return SpmhMessageUtils.ErrorCode; }
    /**
     * @protected
     */
    get InfoCode() { return SpmhMessageUtils.ErrorCode; }
    /**
     * @protected
     */
    get WarningCode() { return SpmhMessageUtils.ErrorCode; }

    //
    //
    /**
     * Top level export of spmh shared library packages ...{ cls, type, util }...
     *
     * @protected
     */
    get spmh() {
        return {
            cmn: spmhUtilsCmn,
            type: spmhUtilsType
        };
    }

    //
    // package: @spmhome/cmn-utils
    // ____  ____  ____  ____  ____  ____  ____  ____  ____
    // ||c ||||m ||||n ||||- ||||u ||||t ||||i ||||l ||||s ||
    // ||__||||__||||__||||__||||__||||__||||__||||__||||__||
    // |/__\||/__\||/__\||/__\||/__\||/__\||/__\||/__\||/__\|
    //

    /**
     * Utility shortcut to `@spmhome/cmn-utils/path` set of functions
     *
     * @protected
     */
    get _async_() { return this.spmh.cmn.async; }
    /**
     * Utility shortcut to `@spmhome/cmn-utils/path` set of functions
     *
     * @protected
     */
    get _cmn_() { return this.spmh.cmn.gen; }

    /**
     * Utility shortcut to `@spmhome/cmn-utils/path` set of functions
     *
     * @protected
     */
    get _path_() { return this.spmh.cmn.path; }
    /**
     * Utility shortcut to _`@spmhome/cmn-utils/fs`_ set of functions
     *
     * @protected
     */
    get _fs_() { return this.spmh.cmn.fs; }
    /**
     * Utility shortcut to `@spmhome/cmn-utils/node` set of functions
     *
     * @protected
     */
    get _node_() { return this.spmh.cmn.node; }
    /**
     * Utility shortcut to `@spmhome/cmn-utils/ts` set of functions
     *
     * @protected
     */
    get _ts_() { return this.spmh.cmn.ts; }
    /**
     * Utility shortcut to `@spmhome/cmn-utils/version` set of functions
     *
     * @protected
     */
    get _ver_() { return this.spmh.cmn.ver; }
    /**
     * Utility shortcut to `@spmhome/cmn-utils/fetch` set of functions
     *
     * @protected
     */
    get _www_() { return this.spmh.cmn.fetch; }

    //
    // package: @spmhome/type-utils
    // ____  ____  ____  ____  ____  ____  ____  ____  ____  ____
    // ||t ||||y ||||p ||||e ||||- ||||u ||||t ||||i ||||l ||||s ||
    // ||__||||__||||__||||__||||__||||__||||__||||__||||__||||__||
    // |/__\||/__\||/__\||/__\||/__\||/__\||/__\||/__\||/__\||/__\|
    //

    /**
     * Utility shortcut to _`@spmhome/type-utils/array`_ set of functions
     *
     * @protected
     */
    get _arr_() { return this.spmh.type.arr; }
    /**
     * Utility shortcut to `@spmhome/type-utils/crypto` set of functions
     *
     * @protected
     */
    get _crypto_() { return this.spmh.type.crypto; }
    /**
     * Utility shortcut to `@spmhome/type-utils/date` set of functions
     *
     * @protected
     */
    get _date_() { return this.spmh.type.dt; }
    /**
     * Utility shortcut to _`@spmhome/cmn-utils/io`_ set of functions
     *
     * @protected
     */
    get _io_() { return this.spmh.type.io; }
    /**
     * Utility shortcut to `@spmhome/type-utils/json` set of functions
     *
     * @protected
     */
    get _json_() { return this.spmh.type.json; }
    /**
     * Utility shortcut to `@spmhome/type-utils/mime` set of functions
     *
     * @protected
     */
    get _mime_() { return this.spmh.type.mime; }
    /**
     * Utility shortcut to `@spmhome/type-utils/number` set of functions
     *
     * @protected
     */
    get _num_() { return this.spmh.type.num; }
    /**
     * Utility shortcut to `@spmhome/type-utils/object` set of functions
     *
     * @protected
     */
    get _obj_() { return this.spmh.type.obj; }
    /**
     * Utility shortcut to `@spmhome/type-utils/promise` set of functions
     *
     * @protected
     */
    get _pms_() { return this.spmh.type.pms; }
    /**
     * Utility shortcut to `@spmhome/type-utils/regex` set of functions
     *
     * @protected
     */
    get _rgx_() { return this.spmh.type.rgx; }
    /**
     * Utility shortcut to `@spmhome/type-utils/string` set of functions
     *
     * @protected
     */
    get _str_() { return this.spmh.type.str; }
    /**
     * Utility shortcut to `@spmhome/type-utils/type` set of functions
     *
     * @protected
     */
    get _types_() { return this.spmh.type.type; }
}


module.exports = WpwBase;