86 lines
2.1 KiB
JavaScript
86 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
import {Utils} from "../Utils/Utils.ecma.js";
|
|
|
|
/**
|
|
* @typedef {import("../Application/AnP.ecma.js").AnP} AnP
|
|
*/
|
|
|
|
/**
|
|
* @callback anp_thread_model_callback
|
|
* @param {ThreadModel} thread
|
|
* @returns {void}
|
|
*/
|
|
|
|
/**
|
|
* @class
|
|
* @constructor
|
|
* @param {!AnP} anp
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @access public
|
|
*/
|
|
export const ThreadModel = (function(){
|
|
|
|
/**
|
|
* @constructs ThreadModel
|
|
* @param {!AnP} anp
|
|
* @param {!anp_thread_model_callback} callback
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} [inputs = null]
|
|
* @access private
|
|
*/
|
|
const ThreadModel = function(anp, callback, inputs = null){
|
|
|
|
/** @type {ThreadModel} */
|
|
const self = this;
|
|
|
|
/** @type {anp_thread_model_callback} */
|
|
this.callback = callback;
|
|
/** @type {number} */
|
|
this.timer = anp.settings.get(["threads_timer", "timer"], inputs, 0);
|
|
/** @type {boolean} */
|
|
this.bucle = anp.settings.get(["threads_bucle", "bucle"], inputs, true);
|
|
/** @type {number} */
|
|
this.last_date = anp.settings.get(["threads_start_now", "start_now"], inputs, true) ? 0 : Date.now();
|
|
/** @type {number} */
|
|
this.i = -1;
|
|
/** @type {boolean} */
|
|
this.working = anp.settings.get(["threads_working", "working"], inputs, true);
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access public
|
|
*/
|
|
this.try_execute = () => {
|
|
if(!self.working || self.i < 0)
|
|
return;
|
|
|
|
/** @type {number} */
|
|
const date = Date.now();
|
|
|
|
if(date - self.last_date > self.timer){
|
|
self.last_date = date;
|
|
Utils.execute(self.callback, self);
|
|
};
|
|
|
|
};
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access public
|
|
*/
|
|
this.play = () => {
|
|
working = true;
|
|
};
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access public
|
|
*/
|
|
this.stop = () => {
|
|
working = false;
|
|
};
|
|
|
|
};
|
|
|
|
return ThreadModel;
|
|
})(); |