Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"processqueue.min.js","sources":["../../../src/local/process_monitor/processqueue.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\nimport {debounce} from 'core/utils';\nimport {LoadingProcess} from 'core/local/process_monitor/loadingprocess';\nimport log from 'core/log';\n\nconst TOASTSTIMER = 3000;\n\n/**\n * A process queue manager.\n *\n * Adding process to the queue will guarante process are executed in sequence.\n *\n * @module     core/local/process_monitor/processqueue\n * @class      ProcessQueue\n * @copyright  2022 Ferran Recio <ferran@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nexport class ProcessQueue {\n    /** @var {Array} pending the pending queue. */\n    pending = [];\n\n    /** @var {LoadingProcess} current the current uploading process. */\n    currentProcess = null;\n\n    /**\n     * Class constructor.\n     * @param {ProcessMonitorManager} manager the monitor manager\n     */\n    constructor(manager) {\n        this.manager = manager;\n        this.cleanFinishedProcesses = debounce(\n            () => manager.dispatch('cleanFinishedProcesses'),\n            TOASTSTIMER\n        );\n    }\n\n    /**\n     * Adds a new pending upload to the queue.\n     * @param {String} processName the process name\n     * @param {Function} processor the execution function\n     */\n    addPending(processName, processor) {\n        const process = new LoadingProcess(this.manager, {name: processName});\n        process.setExtraData({\n            processor,\n        });\n        process.onFinish((uploadedFile) => {\n            if (this.currentProcess?.id !== uploadedFile.id) {\n                return;\n            }\n            this._discardCurrent();\n        });\n        this.pending.push(process);\n        this._continueProcessing();\n    }\n\n    /**\n     * Adds a new pending upload to the queue.\n     * @param {String} processName the file info\n     * @param {String} errorMessage the file processor\n     */\n    addError(processName, errorMessage) {\n        const process = new LoadingProcess(this.manager, {name: processName});\n        process.setError(errorMessage);\n    }\n\n    /**\n     * Discard the current process and execute the next one if any.\n     */\n    _discardCurrent() {\n        if (this.currentProcess) {\n            this.currentProcess = null;\n        }\n        this.cleanFinishedProcesses();\n        this._continueProcessing();\n    }\n\n    /**\n     * Return the current file uploader.\n     * @return {FileUploader}\n     */\n    _currentProcessor() {\n        return this.currentProcess.data.processor;\n    }\n\n    /**\n     * Continue the queue processing if no current process is defined.\n     */\n    async _continueProcessing() {\n        if (this.currentProcess !== null || this.pending.length === 0) {\n            return;\n        }\n        this.currentProcess = this.pending.shift();\n        try {\n            const processor = this._currentProcessor();\n            await processor(this.currentProcess);\n        } catch (error) {\n            this.currentProcess.setError(error.message);\n            log.error(error);\n        }\n    }\n}\n"],"names":["constructor","manager","cleanFinishedProcesses","dispatch","addPending","processName","processor","process","LoadingProcess","this","name","setExtraData","onFinish","uploadedFile","currentProcess","id","_discardCurrent","pending","push","_continueProcessing","addError","errorMessage","setError","_currentProcessor","data","length","shift","error","message"],"mappings":";;;;;;;;;;;MA0CIA,YAAYC,wCATF,0CAGO,WAORA,QAAUA,aACVC,wBAAyB,oBAC1B,IAAMD,QAAQE,SAAS,2BA1Bf,KAoChBC,WAAWC,YAAaC,iBACdC,QAAU,IAAIC,+BAAeC,KAAKR,QAAS,CAACS,KAAML,cACxDE,QAAQI,aAAa,CACjBL,UAAAA,YAEJC,QAAQK,UAAUC,2EACLC,2EAAgBC,MAAOF,aAAaE,SAGxCC,0BAEJC,QAAQC,KAAKX,cACbY,sBAQTC,SAASf,YAAagB,cACF,IAAIb,+BAAeC,KAAKR,QAAS,CAACS,KAAML,cAChDiB,SAASD,cAMrBL,kBACQP,KAAKK,sBACAA,eAAiB,WAErBZ,8BACAiB,sBAOTI,2BACWd,KAAKK,eAAeU,KAAKlB,yCAOJ,OAAxBG,KAAKK,gBAAmD,IAAxBL,KAAKQ,QAAQQ,aAG5CX,eAAiBL,KAAKQ,QAAQS,kBAEzBpB,UAAYG,KAAKc,0BACjBjB,UAAUG,KAAKK,gBACvB,MAAOa,YACAb,eAAeQ,SAASK,MAAMC,sBAC/BD,MAAMA"}