148 lines
3.3 KiB
JavaScript
148 lines
3.3 KiB
JavaScript
"use strict";
|
|
|
|
import {Check} from "../Utils/Checks.ecma.js";
|
|
import {Common} from "../Utils/Common.ecma.js";
|
|
|
|
/**
|
|
* @typedef {import("../Application/AnP.ecma.js").AnP} AnP
|
|
*/
|
|
|
|
/**
|
|
* @class ModelsManager
|
|
* @constructor
|
|
* @param {!AnP} anp
|
|
* @returns {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
export const ModelsManager = (function(){
|
|
|
|
/**
|
|
* @callback default_callback
|
|
* @return {void}
|
|
*/
|
|
|
|
/**
|
|
* @callback continue_callback
|
|
* @param {!boolean} ok
|
|
* @return {boolean}
|
|
*/
|
|
|
|
/**
|
|
* @constructs ModelsManager
|
|
* @param {!AnP} anp
|
|
* @returns {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const ModelsManager = function(anp){
|
|
|
|
/** @type {ModelsManager} */
|
|
const self = this,
|
|
/** @type {Object.<string, Object.<string, any|null>|Function>} */
|
|
models = {};
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const constructor = () => {};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.update = (callback = null) => {
|
|
|
|
Common.execute(callback);
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.reset = (callback = null) => {
|
|
|
|
Common.clear_dictionary(models);
|
|
|
|
self.update(callback);
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.start = (callback = null) => {
|
|
|
|
/** @type {continue_callback} */
|
|
const end = ok => Common.execute(callback, ok);
|
|
|
|
if(started){
|
|
end(false);
|
|
return false;
|
|
};
|
|
started = true;
|
|
|
|
self.update(end);
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.close = (callback = null) => {
|
|
|
|
/** @type {continue_callback} */
|
|
const end = ok => Common.execute(callback, ok);
|
|
|
|
if(!started){
|
|
end(false);
|
|
return false;
|
|
};
|
|
started = false;
|
|
|
|
end(true);
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* @param {?any} inputs
|
|
* @param {!boolean} [overwrite = false]
|
|
* @param {?default_callback} [callback = null]
|
|
* @return {void}
|
|
* @access public
|
|
*/
|
|
this.add = (inputs, overwrite = false, callback = null) => {
|
|
anp.files.load_json(inputs, data => {
|
|
Object.entries(data).forEach(([key, Model]) => {
|
|
Model &&
|
|
(overwrite || !models[key]) &&
|
|
(Check.is_function(Model) || Check.is_object(Model)) &&
|
|
(models[key] = Model);
|
|
});
|
|
Common.execute(callback);
|
|
}, true);
|
|
};
|
|
|
|
/**
|
|
* @param {!string} name
|
|
* @returns {Object.<string, any|null>|Function|null}
|
|
*/
|
|
this.get = name => models[name] || null;
|
|
|
|
constructor();
|
|
|
|
};
|
|
|
|
return ModelsManager;
|
|
|
|
})(); |