"use strict"; /** * @typedef {import("../Application/AnP.ecma.js").AnP} AnP */ import {Common} from "../Utils/Common.ecma"; import {Check} from "../Utils/Check.ecma"; /** * @abstract * @class BaseAbstract * @constructor * @param {!any} base * @param {!AnP} anp * @param {!string} key * @param {?(Object.|Array.)} [inputs = null] * @param {!Object.} [print_options = {}] * @returns {void} * @access public * @static */ export const BaseAbstract = (function(){ /** * @callable base_abstract_callback_end * @param {!boolean} ok * @returns {boolean} */ /** * @callable base_abstract_get_value * @param {!(string|Array.)} keys * @param {?(Object.|Array.)} [inputs = null] * @param {?any} [default_value = null] * @returns {any|null} */ /** * @constructs BaseAbstract * @param {!any} base * @param {!AnP} anp * @param {?(Object.|Array.)} [inputs = null] * @param {!string} key * @param {!Object.} [print_options = {}] * @returns {void} * @access private * @static */ const BaseAbstract = function(base, anp, key, inputs = null, print_options = {}){ /** @type {BaseAbstract} */ const self = this; /** * @returns {boolean} * @access public */ this._build = () => true; /** * @param {!string} type * @param {!(string|Array.)} message * @param {?(Object.|Array.)} [inputs = null] * @param {!number} [i = 0] * @returns {void} * @access public */ this.print = (type, message, inputs = null, i = 0) => { self.allow_print.message === false || anp.print(type, key + "_" + message, [print_options, inputs], i + 1); }; /** * @returns {void} * @private */ const constructor = () => { /** @type {base_abstract_get_value} */ const get_value = anp.settings ? anp.settings.get : Common.get_value; /** @type {Object.} */ this.allow_print = [ "building", "built", "starting", "started", "already_started", "closing", "closed", "already_closed" ].reduce((item, action) => { item[key] = get_value(key + "_print_" + action + "_message", inputs, true); return item; }, {}); self.print("info", "building"); this.key = key; /** @type {boolean} */ this.started = false; /** @type {boolean} */ this.closed = true; self.extends(base); self._build(); self.print("ok", "built"); }; /** * @param {!any} item * @returns {void} * @access public */ this.extends = item => { Common.extends(self, item); }; /** * @param {?base_abstract_callback_end} [callback = null] * @returns {boolean} * @access public */ this._start = (callback = null) => Check.is_function(callback) ? callback(true) : true; /** * @param {?base_abstract_callback_end} [callback = null] * @returns {boolean} * @access public */ this.start = (callback = null) => { /** @type {base_abstract_callback_end} */ const end = ok => Check.is_function(callback) ? callback(ok) : ok; self.print("info", "starting"); if(self.started){ self.print("warn", "already_started"); return end(false); }; self.started = true; return self._start(ok => { self.closed = false; self.print("ok", "started"); return end(ok); }); }; /** * @param {?base_abstract_callback_end} [callback = null] * @returns {boolean} * @access public */ this._close = (callback = null) => Check.is_function(callback) ? callback(true) : true; /** * @param {?base_abstract_callback_end} [callback = null] * @returns {boolean} * @access public */ this.close = (callback = null) => { /** @type {base_abstract_callback_end} */ const end = ok => Check.is_function(callback) ? callback(ok) : ok; self.print("info", "closing"); if(self.closed){ self.print("warn", "already_closed"); return end(false); }; self.closed = true; return self._close(ok => { self.started = false; self.print("ok", "closed"); return end(ok); }); }; constructor(); }; return BaseAbstract; })();