1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* The course file uploader.
|
|
|
18 |
*
|
|
|
19 |
* This module is used to upload files directly into the course.
|
|
|
20 |
*
|
|
|
21 |
* @module core/local/process_monitor/manager
|
|
|
22 |
* @copyright 2022 Ferran Recio <ferran@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
import {Reactive} from 'core/reactive';
|
|
|
27 |
import {eventTypes, dispatchStateChangedEvent} from 'core/local/process_monitor/events';
|
|
|
28 |
|
|
|
29 |
const initialState = {
|
|
|
30 |
display: {
|
|
|
31 |
show: false,
|
|
|
32 |
},
|
|
|
33 |
queue: [],
|
|
|
34 |
};
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* The reactive file uploader class.
|
|
|
38 |
*
|
|
|
39 |
* As all the upload queues are reactive, any plugin can implement its own upload monitor.
|
|
|
40 |
*
|
|
|
41 |
* @module core/local/process_monitor/manager
|
|
|
42 |
* @class ProcessMonitorManager
|
|
|
43 |
* @copyright 2021 Ferran Recio <ferran@moodle.com>
|
|
|
44 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
45 |
*/
|
|
|
46 |
class ProcessMonitorManager extends Reactive {
|
|
|
47 |
/**
|
|
|
48 |
* The next process id to use.
|
|
|
49 |
*
|
|
|
50 |
* @attribute nextId
|
|
|
51 |
* @type number
|
|
|
52 |
* @default 1
|
|
|
53 |
* @package
|
|
|
54 |
*/
|
|
|
55 |
nextId = 1;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Generate a unique process id.
|
|
|
59 |
* @return {number} a generated process Id
|
|
|
60 |
*/
|
|
|
61 |
generateProcessId() {
|
|
|
62 |
return this.nextId++;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* @var {Object} mutations the monitor mutations.
|
|
|
68 |
*/
|
|
|
69 |
const mutations = {
|
|
|
70 |
/**
|
|
|
71 |
* Add a new process to the queue.
|
|
|
72 |
*
|
|
|
73 |
* @param {StateManager} stateManager the current state manager
|
|
|
74 |
* @param {Object} processData the upload id to finish
|
|
|
75 |
*/
|
|
|
76 |
addProcess: function(stateManager, processData) {
|
|
|
77 |
const state = stateManager.state;
|
|
|
78 |
stateManager.setReadOnly(false);
|
|
|
79 |
state.queue.add({...processData});
|
|
|
80 |
state.display.show = true;
|
|
|
81 |
stateManager.setReadOnly(true);
|
|
|
82 |
},
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Remove a process from the queue.
|
|
|
86 |
*
|
|
|
87 |
* @param {StateManager} stateManager the current state manager
|
|
|
88 |
* @param {Number} processId the process id
|
|
|
89 |
*/
|
|
|
90 |
removeProcess: function(stateManager, processId) {
|
|
|
91 |
const state = stateManager.state;
|
|
|
92 |
stateManager.setReadOnly(false);
|
|
|
93 |
state.queue.delete(processId);
|
|
|
94 |
if (state.queue.size === 0) {
|
|
|
95 |
state.display.show = false;
|
|
|
96 |
}
|
|
|
97 |
stateManager.setReadOnly(true);
|
|
|
98 |
},
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Update a process process to the queue.
|
|
|
102 |
*
|
|
|
103 |
* @param {StateManager} stateManager the current state manager
|
|
|
104 |
* @param {Object} processData the upload id to finish
|
|
|
105 |
* @param {Number} processData.id the process id
|
|
|
106 |
*/
|
|
|
107 |
updateProcess: function(stateManager, processData) {
|
|
|
108 |
if (processData.id === undefined) {
|
|
|
109 |
throw Error(`Missing process ID in process data`);
|
|
|
110 |
}
|
|
|
111 |
const state = stateManager.state;
|
|
|
112 |
stateManager.setReadOnly(false);
|
|
|
113 |
const queueItem = state.queue.get(processData.id);
|
|
|
114 |
if (!queueItem) {
|
|
|
115 |
throw Error(`Unkown process with id ${processData.id}`);
|
|
|
116 |
}
|
|
|
117 |
for (const [prop, propValue] of Object.entries(processData)) {
|
|
|
118 |
queueItem[prop] = propValue;
|
|
|
119 |
}
|
|
|
120 |
stateManager.setReadOnly(true);
|
|
|
121 |
},
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Set the monitor show attribute.
|
|
|
125 |
*
|
|
|
126 |
* @param {StateManager} stateManager the current state manager
|
|
|
127 |
* @param {Boolean} show the show value
|
|
|
128 |
*/
|
|
|
129 |
setShow: function(stateManager, show) {
|
|
|
130 |
const state = stateManager.state;
|
|
|
131 |
stateManager.setReadOnly(false);
|
|
|
132 |
state.display.show = show;
|
|
|
133 |
if (!show) {
|
|
|
134 |
this.cleanFinishedProcesses(stateManager);
|
|
|
135 |
}
|
|
|
136 |
stateManager.setReadOnly(true);
|
|
|
137 |
},
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Remove a processes from the queue.
|
|
|
141 |
*
|
|
|
142 |
* @param {StateManager} stateManager the current state manager
|
|
|
143 |
*/
|
|
|
144 |
removeAllProcesses: function(stateManager) {
|
|
|
145 |
const state = stateManager.state;
|
|
|
146 |
stateManager.setReadOnly(false);
|
|
|
147 |
state.queue.forEach((element) => {
|
|
|
148 |
state.queue.delete(element.id);
|
|
|
149 |
});
|
|
|
150 |
state.display.show = false;
|
|
|
151 |
stateManager.setReadOnly(true);
|
|
|
152 |
},
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Clean all finished processes.
|
|
|
156 |
*
|
|
|
157 |
* @param {StateManager} stateManager the current state manager
|
|
|
158 |
*/
|
|
|
159 |
cleanFinishedProcesses: function(stateManager) {
|
|
|
160 |
const state = stateManager.state;
|
|
|
161 |
stateManager.setReadOnly(false);
|
|
|
162 |
state.queue.forEach((element) => {
|
|
|
163 |
if (element.finished && !element.error) {
|
|
|
164 |
state.queue.delete(element.id);
|
|
|
165 |
}
|
|
|
166 |
});
|
|
|
167 |
if (state.queue.size === 0) {
|
|
|
168 |
state.display.show = false;
|
|
|
169 |
}
|
|
|
170 |
stateManager.setReadOnly(true);
|
|
|
171 |
},
|
|
|
172 |
};
|
|
|
173 |
|
|
|
174 |
const manager = new ProcessMonitorManager({
|
|
|
175 |
name: `ProcessMonitor`,
|
|
|
176 |
eventName: eventTypes.processMonitorStateChange,
|
|
|
177 |
eventDispatch: dispatchStateChangedEvent,
|
|
|
178 |
mutations: mutations,
|
|
|
179 |
state: initialState,
|
|
|
180 |
});
|
|
|
181 |
|
|
|
182 |
export {manager};
|