Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 process wrapper class.
18
 *
19
 * This module is used to update a process in the process monitor.
20
 *
21
 * @module     core/local/process_monitor/loadingprocess
22
 * @class      LoadingProcess
23
 * @copyright  2022 Ferran Recio <ferran@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
import log from 'core/log';
28
 
29
export class LoadingProcess {
30
 
31
    /** @var {Map} editorUpdates the courses pending to be updated. */
32
    processData = null;
33
 
34
    /** @var {Object} extraData any extra process information to store. */
35
    extraData = null;
36
 
37
    /** @var {ProcessMonitorManager} manager the page monitor. */
38
    manager = null;
39
 
40
    /** @var {Function} finishedCallback the finished callback if any. */
41
    finishedCallback = null;
42
 
43
    /** @var {Function} removedCallback the removed callback if any. */
44
    removedCallback = null;
45
 
46
    /** @var {Function} errorCallback the error callback if any. */
47
    errorCallback = null;
48
 
49
    /**
50
     * Class constructor
51
     * @param {ProcessMonitorManager} manager the monitor manager
52
     * @param {Object} definition the process definition data
53
     */
54
    constructor(manager, definition) {
55
        this.manager = manager;
56
        // Add defaults.
57
        this.processData = {
58
            id: manager.generateProcessId(),
59
            name: '',
60
            percentage: 0,
61
            url: null,
62
            error: null,
63
            finished: false,
64
            ...definition,
65
        };
66
        // Create a new entry.
67
        this._dispatch('addProcess', this.processData);
68
    }
69
 
70
    /**
71
     * Execute a monitor manager mutation when the state is ready.
72
     *
73
     * @private
74
     * @param {String} action the mutation to dispatch
75
     * @param {*} params the mutaiton params
76
     */
77
    _dispatch(action, params) {
78
        this.manager.getInitialStatePromise().then(() => {
79
            this.manager.dispatch(action, params);
80
            return;
81
        }).catch(() => {
82
            log.error(`Cannot update process monitor.`);
83
        });
84
    }
85
 
86
    /**
87
     * Define a finished process callback function.
88
     * @param {Function} callback the callback function
89
     */
90
    onFinish(callback) {
91
        this.finishedCallback = callback;
92
    }
93
 
94
    /**
95
     * Define a removed from monitor process callback function.
96
     * @param {Function} callback the callback function
97
     */
98
    onRemove(callback) {
99
        this.removedCallback = callback;
100
    }
101
 
102
    /**
103
     * Define a error process callback function.
104
     * @param {Function} callback the callback function
105
     */
106
    onError(callback) {
107
        this.errorCallback = callback;
108
    }
109
 
110
    /**
111
     * Set the process percentage.
112
     * @param {Number} percentage
113
     */
114
    setPercentage(percentage) {
115
        this.processData.percentage = percentage;
116
        this._dispatch('updateProcess', this.processData);
117
    }
118
 
119
    /**
120
     * Stores extra information to the process.
121
     *
122
     * This method is used to add information like the course, the user
123
     * or any other needed information.
124
     *
125
     * @param {Object} extraData any extra process information to store
126
     */
127
    setExtraData(extraData) {
128
        this.extraData = extraData;
129
    }
130
 
131
    /**
132
     * Set the process error string.
133
     *
134
     * Note: set the error message will mark the process as finished.
135
     *
136
     * @param {String} error the string message
137
     */
138
    setError(error) {
139
        this.processData.error = error;
140
        if (this.errorCallback !== null) {
141
            this.errorCallback(this);
142
        }
143
        this.processData.finished = true;
144
        if (this.finishedCallback !== null) {
145
            this.finishedCallback(this);
146
        }
147
        this._dispatch('updateProcess', this.processData);
148
    }
149
 
150
    /**
151
     * Rename the process
152
     * @param {String} name the new process name
153
     */
154
    setName(name) {
155
        this.processData.name = name;
156
        this._dispatch('updateProcess', this.processData);
157
    }
158
 
159
    /**
160
     * Mark the process as finished.
161
     */
162
    finish() {
163
        this.processData.finished = true;
164
        if (this.finishedCallback !== null) {
165
            this.finishedCallback(this);
166
        }
167
        this._dispatch('updateProcess', this.processData);
168
    }
169
 
170
    /**
171
     * Remove the process from the monitor.
172
     */
173
    remove() {
174
        if (this.removedCallback !== null) {
175
            this.removedCallback(this);
176
        }
177
        this._dispatch('removeProcess', this.processData.id);
178
    }
179
 
180
    /**
181
     * Returns the current rpocess data.
182
     * @returns {Object} the process data
183
     */
184
    getData() {
185
        return {...this.processData};
186
    }
187
 
188
    /**
189
     * Return the process name
190
     * @return {String}
191
     */
192
    get name() {
193
        return this.processData.name;
194
    }
195
 
196
    /**
197
     * Return the process internal id
198
     * @return {Number}
199
     */
200
    get id() {
201
        return this.processData.id;
202
    }
203
 
204
    /**
205
     * Return the process extra data.
206
     * @return {*} whatever is in extra data
207
     */
208
    get data() {
209
        return this.extraData;
210
    }
211
}