Просмотр исходного кода

impl InterceptorManager

* add comments
* add debug line
Yuki Takei 9 лет назад
Родитель
Сommit
230a1dab38
3 измененных файлов с 64 добавлено и 7 удалено
  1. 3 0
      lib/crowi/index.js
  2. 24 0
      lib/util/basicInterceptor.js
  3. 37 7
      lib/util/interceptorManager.js

+ 3 - 0
lib/crowi/index.js

@@ -283,6 +283,7 @@ Crowi.prototype.setupInterceptorManager = function() {
 
     // intercepter test 1
     self.interceptorManager.addInterceptor({
+      id: 'test 1',
       isInterceptWhen: (contextName) => {
         // implement this
         return true;
@@ -298,6 +299,7 @@ Crowi.prototype.setupInterceptorManager = function() {
     });
     // intercepter test 2
     self.interceptorManager.addInterceptor({
+      id: 'test 2',
       isInterceptWhen: (contextName) => {
         // implement this
         return true;
@@ -313,6 +315,7 @@ Crowi.prototype.setupInterceptorManager = function() {
     });
     // intercepter test 3
     self.interceptorManager.addInterceptor({
+      id: 'test 2',
       isInterceptWhen: (contextName) => {
         // implement this
         return true;

+ 24 - 0
lib/util/basicInterceptor.js

@@ -3,16 +3,40 @@
  */
 class BasicInterceptor {
 
+  /**
+   * getter for id
+   */
+  getId() {
+    return this.constructor.name;
+  }
+
+  /**
+   * return whether this interceptor works by specified contextName
+   *
+   * @param {string} contextName
+   * @return {boolean}
+   */
   isInterceptWhen(contextName) {
     // implement this
     return false;
   }
 
+  /**
+   * return whether this interceptor processes in parallel mode or sequencial mode
+   * @return {boolean}
+   */
   isProcessableParallel() {
     // implement this
     return true;
   }
 
+  /**
+   * process method
+   *
+   * @param {string} contextName
+   * @param {any} args
+   * @return {Promise}
+   */
   process(contextName, ...args) {
     // override this
     return Promise.resolve(...args);

+ 37 - 7
lib/util/interceptorManager.js

@@ -9,39 +9,69 @@ class InterceptorManager {
     this.interceptors = [];
   }
 
+  /**
+   * add an Interceptor
+   * @param {BasicInterceptor} interceptor
+   */
   addInterceptor(interceptor) {
     this.addInterceptors([interceptor]);
   }
 
+  /**
+   * add Interceptors
+   * @param {BasicInterceptor[]} interceptors
+   */
   addInterceptors(interceptors) {
     this.interceptors = this.interceptors.concat(interceptors);
   }
 
+  /**
+   * process Interceptors
+   *
+   * @param {string} contextName
+   * @param {any} args
+   */
   process(contextName, ...args) {
-    // filter only context matches
+    debug(`processing context '${contextName}'`);
+
+    // filter only contexts matches to specified 'contextName'
     const matchInterceptors = this.interceptors.filter((i) => i.isInterceptWhen(contextName));
 
     const parallels = matchInterceptors.filter((i) => i.isProcessableParallel());
     const sequentials = matchInterceptors.filter((i) => !i.isProcessableParallel());
 
-    debug(`processing parallels(${parallels.length})`);
-    debug(`processing sequentials(${sequentials.length})`);
+    debug(`${parallels.length} parallel interceptors found.`);
+    debug(`${sequentials.length} sequencial interceptors found.`);
 
     return Promise.all(
+      // parallel
       parallels.map((interceptor) => {
-        return interceptor.process(contextName, ...args);
+        return this.doProcess(interceptor, contextName, ...args);
       })
+      // sequential
       .concat([
         sequentials.reduce((prevPromise, nextInterceptor) => {
-          return prevPromise.then((...results) => nextInterceptor.process(contextName, ...results));
-        }, Promise.resolve(...args))
+          return prevPromise.then((...results) => this.doProcess(nextInterceptor, contextName, ...results));
+        }, Promise.resolve(...args)/* initial Promise */)
       ])
     ).then(() => {
-      console.log("Promise.all().then()");
+      debug(`end processing context '${contextName}'`);
       return;
     });
   }
 
+  doProcess(interceptor, contextName, ...args) {
+    return interceptor.process(contextName, ...args)
+      .then((...results) => {
+        debug(`processing '${interceptor.id}' in context '${contextName}'`);
+        return Promise.resolve(...results);
+      })
+      .catch((reason) => {
+        debug(`failed when processing '${interceptor.id}' in context '${contextName}'`);
+        debug(reason);
+        return Promise.resolve(...args);
+      });
+  }
 }
 
 module.exports = (crowi) => {