"use strict"; /** * @typedef {import("../Application/AnP.ecma.js").AnP} AnP */ /** * @class FilesDriver * @constructor * @param {!AnP} anp * @returns {void} * @access public * @static */ export const FilesDriver = (function(){ /** * @callback files_driver_start_callback * @param {!boolean} ok * @returns {boolean} */ /** * @constructs FilesDriver * @param {!AnP} anp * @returns {void} * @access private * @static */ const FilesDriver = function(anp){ /** @type {FilesDriver} */ const self = this; /** @type {boolean} */ let started = false, /** @type {boolean} */ closed = false, /** @type {string} */ default_http_method = "GET", /** @type {boolean} */ default_http_asynchronous = true, /** @type {number} */ default_timeout = 2000; /** * @returns {void} * @access private */ const constructor = () => { self.update(); }; /** * @param {?files_driver_start_callback} [callback = null] * @returns {boolean} * @access public */ this.update = (callback = null) => { default_http_method = anp.settings.get([ "http_method", "default_http_method" ], null, default_http_method); default_http_asynchronous = anp.settings.get([ "http_asynchronous", "default_http_asynchronous" ], null, default_http_asynchronous); default_timeout = anp.settings.get([ "timeout", "default_timeout" ], null, default_timeout); Utils.execute(callback); return true; }; /** * @param {?files_driver_start_callback} [callback = null] * @returns {boolean} * @access public */ this.start = (callback = null) => { if(started){ return false; }; started = true; self.update(callback); return true; }; this.load = (path, callback = null, inputs = null) => { /** @type {boolean} */ let ended = false, /** @type {number} */ error = 0; /** @type {XMLHttpRequest} */ const ajax = new XMLHttpRequest(), /** @type {number} */ timeout = Utils.get_value([ "timeout", "ajax_timeout", "default_timeout" ], inputs, default_timeout), /** @type {number} */ date = Date.now(), end = code => { code && (error |= 1 << code); !ended && (ended = true) && Utils.execute( callback, ajax.responseText, ajax.status, ajax.readyState, error, error === 0 ); }; ajax.open( Utils.get_value([ "method", "http_method", "default_http_method" ], inputs, default_http_method), path, Utils.get_value([ "asynchronous", "http_asynchronous", "default_http_asynchronous" ], inputs, default_http_asynchronous) ); ajax.timeout = timeout; ajax.onreadystatechange = function(){ if(ended) return; if(ajax.readyState === 4) end(( ajax.status >= 200 && ajax.status < 300 ) || [301, 302, 304].includes(ajax.status) ? 0 : 1); else if(Date.now() - date >= timeout) end(2); }; ajax.send(null); ajax.onerror = () => {end(3);} ajax.onabort = () => {end(4);} ajax.ontimeout = () => {end(5);} return ajax; }; constructor(); }; return FilesDriver; })();