VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/www/html/libs/absol-full/dist/js/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/html/libs/absol-full/dist/js/mdls__absol__src__AppPattern__TaskManager.js
/*** module: node_modules/absol/src/AppPattern/TaskManager.js ***/
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _stringGenerate = require("../String/stringGenerate");

var _EventEmitter = _interopRequireDefault(require("../HTML5/EventEmitter"));

var _OOP = _interopRequireDefault(require("../HTML5/OOP"));


/***
 *
 * @param {TaskManager} manager
 * @param {function(finishTask: function, bundle: any): (void|Promise)=} func
 * @param {any=} bundle
 * @constructor
 */
function Task(manager, func, bundle) {
  this.manager = manager;
  this.id = (0, _stringGenerate.randomIdent)(12);
  this.func = func;
  this.bundle = bundle;
  this.state = 0;
}

Task.prototype.begin = function () {
  if (this.state !== 0) return;
  this.state = 1;
  var sync;

  if (this.func) {
    try {
      sync = this.func(this.end.bind(this), this.bundle);
    } catch (error) {
      console.error(error);
    }
  }

  if (sync && sync.then) sync.catch(error => console.error(error)).then(this.end.bind(this));
};

Task.prototype.end = function () {
  if (this.state !== 1) return;
  this.state = 2;
  this.manager.onFinishTask(this);
};

function TaskManager(opt) {
  _EventEmitter.default.call(this);

  opt = opt || {};
  this.limit = typeof opt.limit === "number" && opt.limit >= 1 ? opt.limit : Infinity;
  this.pendingTasks = [];
  this.runningTasks = [];
}

_OOP.default.mixClass(TaskManager, _EventEmitter.default);
/***
 *
 * @param {function(finishTask: function, bundle: any): (void|Promise)} func
 * @param {any} bundle
 */


TaskManager.prototype.requestTask = function (func, bundle) {
  var task = new Task(this, func, bundle);

  if (this.runningTasks.length < this.limit) {
    this.runningTasks.push(task);
    this.emit('task_begin', {
      type: 'task_begin',
      task
    }, this);
    task.begin();
  } else {
    this.pendingTasks.push(task);
  }
};

TaskManager.prototype.onFinishTask = function (task) {
  var idx = this.runningTasks.indexOf(task);
  if (idx < 0) return;
  this.runningTasks.splice(idx, 1);
  this.emit('task_end', {
    type: 'task_end',
    task
  }, this);

  while (this.pendingTasks.length > 0 && this.runningTasks.length < this.limit) {
    task = this.pendingTasks.shift();
    this.runningTasks.push(task);
    this.emit('task_begin', {
      type: 'task_begin',
      task
    }, this);
    task.begin();
  }
};

TaskManager.prototype.newTask = function () {
  var task = new Task(this, null, null);
  this.runningTasks.push(task);
  this.emit('task_begin', {
    type: 'task_begin',
    task
  }, this);
  task.begin();
  return task;
};

var _default = TaskManager;
exports.default = _default;

VaKeR 2022