Blame view

node_modules/loopback-phase/lib/phase-list.js 7.09 KB
f7563de62   Palak Handa   first commit
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  // Copyright IBM Corp. 2014,2016. All Rights Reserved.
  // Node module: loopback-phase
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT
  
  var g = require('strong-globalize')();
  
  var Phase = require('./phase');
  var zipMerge = require('./merge-name-lists');
  var async = require('async');
  
  module.exports = PhaseList;
  
  /**
   * An ordered list of phases.
   *
   * ```js
   * var PhaseList = require('loopback-phase').PhaseList;
   * var phases = new PhaseList();
   * phases.add('my-phase');
   * ```
   *
   * @class PhaseList
   */
  
  function PhaseList() {
    this._phases = [];
    this._phaseMap = {};
  }
  
  /**
   * Get the first `Phase` in the list.
   *
   * @returns {Phase} The first phase.
   */
  
  PhaseList.prototype.first = function() {
    return this._phases[0];
  };
  
  /**
   * Get the last `Phase` in the list.
   *
   * @returns {Phase} The last phase.
   */
  
  PhaseList.prototype.last = function() {
    return this._phases[this._phases.length - 1];
  };
  
  /**
   * Add one or more phases to the list.
   *
   * @param {Phase|String|String[]} phase The phase (or phases) to be added.
   * @returns {Phase|Phase[]} The added phase or phases.
   */
  
  PhaseList.prototype.add = function(phase) {
    var phaseList = this;
    var phaseArray = Array.isArray(phase) ? phase : null;
  
    if(phaseArray) {
      return phaseArray.map(phaseList.add.bind(phaseList));
    }
  
    phase = this._resolveNameAndAddToMap(phase);
    this._phases.push(phase);
  
    return phase;
  };
  
  PhaseList.prototype._resolveNameAndAddToMap = function(phaseOrName) {
    var phase = phaseOrName;
  
    if(typeof phase === 'string') {
      phase = new Phase(phase);
    }
  
    if (phase.id in this._phaseMap) {
      throw new Error(g.f('Phase "%s" already exists.', phase.id));
    }
  
  
    if(!phase.__isPhase__) {
      throw new Error(g.f('Cannot add a non phase object to a {{PhaseList}}'));
    }
  
    this._phaseMap[phase.id] = phase;
    return phase;
  };
  
  /**
   * Add a new phase at the specified index.
   * @param {Number} index The zero-based index.
   * @param {String|String[]} phase The name of the phase to add.
   * @returns {Phase} The added phase.
   */
  
  PhaseList.prototype.addAt = function(index, phase) {
    phase = this._resolveNameAndAddToMap(phase);
    this._phases.splice(index, 0, phase);
    return phase;
  };
  
  /**
   * Add a new phase as the next one after the given phase.
   * @param {String} after The referential phase.
   * @param {String|String[]} phase The name of the phase to add.
   * @returns {Phase} The added phase.
   */
  
  PhaseList.prototype.addAfter = function(after, phase) {
    var ix = this.getPhaseNames().indexOf(after);
    if (ix === -1) {
      throw new Error(g.f('Unknown phase: %s', after));
    }
    return this.addAt(ix+1, phase);
  };
  
  /**
   * Add a new phase as the previous one before the given phase.
   * @param {String} before The referential phase.
   * @param {String|String[]} phase The name of the phase to add.
   * @returns {Phase} The added phase.
   */
  
  PhaseList.prototype.addBefore = function(before, phase) {
    var ix = this.getPhaseNames().indexOf(before);
    if (ix === -1) {
      throw new Error(g.f('Unknown phase: %s', before));
    }
    return this.addAt(ix, phase);
  };
  
  /**
   * Remove a `Phase` from the list.
   *
   * @param {Phase|String} phase The phase to be removed.
   * @returns {Phase} The removed phase.
   */
  
  PhaseList.prototype.remove = function(phase) {
    var phases = this._phases;
    var phaseMap = this._phaseMap;
    var phaseId;
  
    if(!phase) return null;
  
    if(typeof phase === 'object') {
      phaseId = phase.id;
    } else {
      phaseId = phase;
      phase = phaseMap[phaseId];
    }
  
    if(!phase || !phase.__isPhase__) return null;
  
    phases.splice(phases.indexOf(phase), 1);
    delete this._phaseMap[phaseId];
  
    return phase;
  };
  
  /**
   * Merge the provided list of names with the existing phases
   * in such way that the order of phases is preserved.
   *
   * **Example**
   *
   * ```js
   * // Initial list of phases
   * phaseList.add(['initial', 'session', 'auth', 'routes', 'files', 'final']);
   *
   * // zip-merge more phases
   * phaseList.zipMerge([
   *   'initial', 'postinit', 'preauth', 'auth',
   *   'routes', 'subapps', 'final', 'last'
   * ]);
   *
   * // print the result
   * console.log('Result:', phaseList.getPhaseNames());
   * // Result: [
   * //   'initial', 'postinit', 'preauth', 'session', 'auth',
   * //   'routes', 'subapps', 'files', 'final', 'last'
   * // ]
   * ```
   *
   * @param {String[]} names List of phase names to zip-merge
   */
  PhaseList.prototype.zipMerge = function(names) {
    if (!names.length) return;
  
    var mergedNames = zipMerge(this.getPhaseNames(), names);
    this._phases = mergedNames.map(function(name) {
      var existing = this.find(name);
      return existing ?
        existing :
        this._resolveNameAndAddToMap(name);
    }, this);
  };
  
  /**
   * Find a `Phase` from the list.
   *
   * @param {String} id The phase identifier
   * @returns {Phase} The `Phase` with the given `id`.
   */
  
  PhaseList.prototype.find = function(id) {
    return this._phaseMap[id] || null;
  };
  
  /**
   * Find or add a `Phase` from/into the list.
   *
   * @param {String} id The phase identifier
   * @returns {Phase} The `Phase` with the given `id`.
   */
  
  PhaseList.prototype.findOrAdd = function(id) {
    var phase = this.find(id);
    if(phase) return phase;
    return this.add(id);
  };
  
  /**
   * Get the list of phases as an array of `Phase` objects.
   *
   * @returns {Phase[]} An array of phases.
   */
  
  PhaseList.prototype.toArray = function() {
    return this._phases.slice(0);
  };
  
  /**
   * Launch the phases contained in the list. If there are no phases
   * in the list `process.nextTick` is called with the provided callback.
   *
   * @param {Object} [context] The context of each `Phase` handler.
   * @callback {Function} cb
   * @param {Error} err Any error that occured during a phase contained
   * in the list.
   */
  
  PhaseList.prototype.run = function(ctx, cb) {
    var phases = this._phases;
  
    if(typeof ctx === 'function') {
      cb = ctx;
      ctx = undefined;
    }
  
    if(phases.length) {
      async.eachSeries(phases, function(phase, next) {
        phase.run(ctx, next);
      }, cb);
    } else {
      process.nextTick(cb);
    }
  };
  
  /**
   * Get an array of phase identifiers.
   * @returns {String[]} phaseNames
   */
  
  PhaseList.prototype.getPhaseNames = function() {
    return this._phases.map(function(phase) {
      return phase.id;
    });
  };
  
  /**
   * Register a phase handler for the given phase (and sub-phase).
   *
   * **Example**
   *
   * ```js
   * // register via phase.use()
   * phaseList.registerHandler('routes', function(ctx, next) { next(); });
   * // register via phase.before()
   * phaseList.registerHandler('auth:before', function(ctx, next) { next(); });
   * // register via phase.after()
   * phaseList.registerHandler('auth:after', function(ctx, next) { next(); });
   * ```
   *
   * @param {String} phaseName Name of an existing phase, optionally with
   *   ":before" or ":after" suffix.
   * @param {Function(Object, Function)} handler The handler function to register
   *   with the given phase.
   */
  PhaseList.prototype.registerHandler = function(phaseName, handler) {
    var subphase = 'use';
    var m = phaseName.match(/^(.+):(before|after)$/);
    if (m) {
      phaseName = m[1];
      subphase = m[2];
    }
    var phase = this.find(phaseName);
    if (!phase) throw new Error(g.f('Unknown phase %s', phaseName));
    phase[subphase](handler);
  };