summaryrefslogtreecommitdiffstats
path: root/install/ui/src/freeipa/_base/Phase_controller.js
blob: 9147f201a811c3057d0402f5ea9c5bc5bea2c02e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*  Authors:
 *    Petr Vobornik <pvoborni@redhat.com>
 *
 * Copyright (C) 2012 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

define([
    'dojo/_base/lang',
    'dojo/_base/array',
    'dojo/_base/declare',
    'dojo/Deferred',
    'dojo/promise/all',
    'dojo/topic',
    '../ordered-map'
], function(lang, array, declare, Deferred, all, topic, ordered_map) {


    /**
     * Phase
     *
     * This class does not exist, it's only for documentation purposes.
     *
     * @class _base.Phase_controller.phase
     * @abstract
     */
    /**
     * Name
     * @property {string} name
     */
    /**
     * Tasks
     * @property {Array.<_base.Phase_controller.task>} tasks
     */

    /**
     * Phase task
     *
     * This class does not exist, it's only for documentation purposes.
     *
     * @class _base.Phase_controller.task
     * @abstract
     */
    /**
     * Name
     * @property {number} priority
     */
    /**
     * Tasks
     * @property {Function} handler
     */

    /**
     * Phase Controller
     *
     * Creates synchronization points - phases - in application life cycle.
     *
     * Phases:
     *
     * - are ordered
     * - consist of task
     * - phase finishes when all task finishes
     * - new phases can be added at runtime
     *
     * @class _base.Phase_controller
     */
    var Phase_controller = declare(null, {

        /**
         * Phases
         * @property {ordered_map.<string, _base.Phase_controller.phase>}
         */
        phases: null,

        /**
         * Current phase name
         * @property {string}
         */
        current: null,

        /**
         * Run all phases in a row starting from current, or the first when
         * current is not set.
         */
        run: function() {
            if (this.current !== null) {
                var phase = this.phases.get(this.current);
                this._run_phase(phase);
            } else {
                this.next_phase(true);
            }
        },

        /**
         * Runs phase
         *
         * 1. Sorts tasks of the phase based on their priority.
         * 2. Runs all task sequentially.
         * 3. Waits for all tasks to complete (in case of asynchronous ones)
         * 4. Optionally runs next phase
         *
         * @param {_base.Phase_controller.phase} phase Phase to run
         * @param {boolean} next_phase Whether to run next phase when current finishes
         */
        _run_phase: function(phase, next_phase) {

            if (!phase) return;
            this.current = phase.name;
            topic.publish('phase-start', { phase: phase.name });
            var promises = [];

            var tasks = phase.tasks.sort(function(a,b) {
                return a.priority-b.priority;
            });

            array.forEach(tasks, function(task) {
                var promise;
                try {
                    promise = task.handler();
                } catch (e) {
                    var fail = new Deferred();
                    fail.reject(e, true);
                    promise = fail.promise;
                }
                promises.push(promise);
            });

            all(promises).then(lang.hitch(this, function(results) {
                topic.publish('phase-finished',
                              { phase: phase.name, results: results });
                if (next_phase) {
                    this.next_phase(next_phase);
                }
            }), function(results) {
                topic.publish('phase-error',
                              { phase: phase.name, results: results });
                // don't go for next phase on error, let app decide what to do
            });
        },

        /**
         * Selects next phase and then runs it.
         *
         * @param {boolean} continuous Whether to run phases continuously
         */
        next_phase: function(continuous) {
            var phase;

            if (this.current === null) {
                phase = this.phases.get_value_by_index(0);
            } else {
                var index = this.phases.get_key_index(this.current);
                phase = this.phases.get_value_by_index(index + 1);
            }

            this._run_phase(phase, continuous);
        },

        /**
         * Adds task for a phase.
         *
         * At phase execution, tasks are sorted by priority and executed in
         * that order.
         *
         * @param {string} phase_name Name of associated phase
         * @param {Function} handler Task handler. Should return promise if async.
         * @param {number} [priority=10] Priority of task.
         */
        add_task: function(phase_name, handler, priority) {

            var phase = this.phases.get(phase_name);

            if (!phase) {
                window.console.warn('no such phase: ' + phase_name);
                return;
            }

            if (typeof priority !== 'number') priority = 10;

            phase.tasks.push({
                priority: priority,
                handler: handler
            });
        },

        /**
         * Adds a phase
         *
         * Possible options:
         *
         * - before: 'name-of-phase'
         * - after: 'name-of-phase'
         * - position: 'position for new phase'
         *
         * @param {string} phase name
         * @param {Array.<_base.Phase_controller.task>} tasks
         * @param {Object} options
         */
        add_phase: function(name, tasks, options) {

            var phase = {
                name: name,
                tasks: tasks || []
            };

            var position;
            if (options) {
                if (options.before) {
                    position = this.phases.get_key_index(options.before);
                } else if (options.after) {
                    position = this.phases.get_key_index(options.after);
                    if (position === -1) position = this.phases.length;
                    else position++;
                } else if (options.position) {
                    position = options.position;
                }
            }

            this.phases.put(name, phase, position);
        },

        /**
         * Checks if phases with given name exists
         *
         * @param {string} name
         * @return {boolean}
         */
        exists: function(name) {
            return !!this.phases.get(name);
        },

        constructor: function(spec) {
            spec = spec || {};

            this.phases = ordered_map();

            var phases = spec.phases || [];
            array.forEach(phases, lang.hitch(this, function(phase) {
                this.add_phase(phase);
            }));
        }
    });

    return Phase_controller;

});