Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"manager.min.js","sources":["../../../src/local/process_monitor/manager.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\n/**\n * The course file uploader.\n *\n * This module is used to upload files directly into the course.\n *\n * @module     core/local/process_monitor/manager\n * @copyright  2022 Ferran Recio <ferran@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport {Reactive} from 'core/reactive';\nimport {eventTypes, dispatchStateChangedEvent} from 'core/local/process_monitor/events';\n\nconst initialState = {\n    display: {\n        show: false,\n    },\n    queue: [],\n};\n\n/**\n * The reactive file uploader class.\n *\n * As all the upload queues are reactive, any plugin can implement its own upload monitor.\n *\n * @module     core/local/process_monitor/manager\n * @class      ProcessMonitorManager\n * @copyright  2021 Ferran Recio <ferran@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nclass ProcessMonitorManager extends Reactive {\n    /**\n     * The next process id to use.\n     *\n     * @attribute nextId\n     * @type number\n     * @default 1\n     * @package\n     */\n    nextId = 1;\n\n    /**\n     * Generate a unique process id.\n     * @return {number} a generated process Id\n     */\n    generateProcessId() {\n        return this.nextId++;\n    }\n}\n\n/**\n * @var {Object} mutations the monitor mutations.\n */\nconst mutations = {\n    /**\n     * Add a new process to the queue.\n     *\n     * @param {StateManager} stateManager the current state manager\n     * @param {Object} processData the upload id to finish\n     */\n    addProcess: function(stateManager, processData) {\n        const state = stateManager.state;\n        stateManager.setReadOnly(false);\n        state.queue.add({...processData});\n        state.display.show = true;\n        stateManager.setReadOnly(true);\n    },\n\n    /**\n     * Remove a process from the queue.\n     *\n     * @param {StateManager} stateManager the current state manager\n     * @param {Number} processId the process id\n     */\n    removeProcess: function(stateManager, processId) {\n        const state = stateManager.state;\n        stateManager.setReadOnly(false);\n        state.queue.delete(processId);\n        if (state.queue.size === 0) {\n            state.display.show = false;\n        }\n        stateManager.setReadOnly(true);\n    },\n\n    /**\n     * Update a process process to the queue.\n     *\n     * @param {StateManager} stateManager the current state manager\n     * @param {Object} processData the upload id to finish\n     * @param {Number} processData.id the process id\n     */\n    updateProcess: function(stateManager, processData) {\n        if (processData.id === undefined) {\n            throw Error(`Missing process ID in process data`);\n        }\n        const state = stateManager.state;\n        stateManager.setReadOnly(false);\n        const queueItem = state.queue.get(processData.id);\n        if (!queueItem) {\n            throw Error(`Unkown process with id ${processData.id}`);\n        }\n        for (const [prop, propValue] of Object.entries(processData)) {\n            queueItem[prop] = propValue;\n        }\n        stateManager.setReadOnly(true);\n    },\n\n    /**\n     * Set the monitor show attribute.\n     *\n     * @param {StateManager} stateManager the current state manager\n     * @param {Boolean} show the show value\n     */\n    setShow: function(stateManager, show) {\n        const state = stateManager.state;\n        stateManager.setReadOnly(false);\n        state.display.show = show;\n        if (!show) {\n            this.cleanFinishedProcesses(stateManager);\n        }\n        stateManager.setReadOnly(true);\n    },\n\n    /**\n     * Remove a processes from the queue.\n     *\n     * @param {StateManager} stateManager the current state manager\n     */\n    removeAllProcesses: function(stateManager) {\n        const state = stateManager.state;\n        stateManager.setReadOnly(false);\n        state.queue.forEach((element) => {\n            state.queue.delete(element.id);\n        });\n        state.display.show = false;\n        stateManager.setReadOnly(true);\n    },\n\n    /**\n     * Clean all finished processes.\n     *\n     * @param {StateManager} stateManager the current state manager\n     */\n    cleanFinishedProcesses: function(stateManager) {\n        const state = stateManager.state;\n        stateManager.setReadOnly(false);\n        state.queue.forEach((element) => {\n            if (element.finished && !element.error) {\n                state.queue.delete(element.id);\n            }\n        });\n        if (state.queue.size === 0) {\n            state.display.show = false;\n        }\n        stateManager.setReadOnly(true);\n    },\n};\n\nconst manager = new ProcessMonitorManager({\n    name: `ProcessMonitor`,\n    eventName: eventTypes.processMonitorStateChange,\n    eventDispatch: dispatchStateChangedEvent,\n    mutations: mutations,\n    state: initialState,\n});\n\nexport {manager};\n"],"names":["ProcessMonitorManager","Reactive","generateProcessId","this","nextId","mutations","addProcess","stateManager","processData","state","setReadOnly","queue","add","display","show","removeProcess","processId","delete","size","updateProcess","undefined","id","Error","queueItem","get","prop","propValue","Object","entries","setShow","cleanFinishedProcesses","removeAllProcesses","forEach","element","finished","error","manager","name","eventName","eventTypes","processMonitorStateChange","eventDispatch","dispatchStateChangedEvent"],"mappings":";;;;;;;;;;;MA6CMA,8BAA8BC,6EASvB,mIAMTC,2BACWC,KAAKC,gBAOdC,UAAY,CAOdC,WAAY,SAASC,aAAcC,mBACzBC,MAAQF,aAAaE,MAC3BF,aAAaG,aAAY,GACzBD,MAAME,MAAMC,IAAI,IAAIJ,cACpBC,MAAMI,QAAQC,MAAO,EACrBP,aAAaG,aAAY,IAS7BK,cAAe,SAASR,aAAcS,iBAC5BP,MAAQF,aAAaE,MAC3BF,aAAaG,aAAY,GACzBD,MAAME,MAAMM,OAAOD,WACM,IAArBP,MAAME,MAAMO,OACZT,MAAMI,QAAQC,MAAO,GAEzBP,aAAaG,aAAY,IAU7BS,cAAe,SAASZ,aAAcC,qBACXY,IAAnBZ,YAAYa,SACNC,kDAEJb,MAAQF,aAAaE,MAC3BF,aAAaG,aAAY,SACnBa,UAAYd,MAAME,MAAMa,IAAIhB,YAAYa,QACzCE,gBACKD,uCAAgCd,YAAYa,SAEjD,MAAOI,KAAMC,aAAcC,OAAOC,QAAQpB,aAC3Ce,UAAUE,MAAQC,UAEtBnB,aAAaG,aAAY,IAS7BmB,QAAS,SAAStB,aAAcO,YACtBL,MAAQF,aAAaE,MAC3BF,aAAa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