Kaynağa Gözat

impl InterceptorManager

clean code
Yuki Takei 9 yıl önce
ebeveyn
işleme
8ef8205eff

+ 1 - 51
lib/crowi/index.js

@@ -280,56 +280,6 @@ Crowi.prototype.setupInterceptorManager = function() {
   var self = this;
   return new Promise(function(resolve, reject) {
     self.interceptorManager = require('../util/interceptorManager')(self);
-
-    // intercepter test 1
-    self.interceptorManager.addInterceptor({
-      id: 'test 1',
-      isInterceptWhen: (contextName) => {
-        // implement this
-        return true;
-      },
-      isProcessableParallel: () => {
-        // implement this
-        return false;
-      },
-      process: (contextName, ...args) => {
-        args[0].page.revision.body += 'hogehoge';
-        return Promise.resolve(...args);
-      }
-    });
-    // intercepter test 2
-    self.interceptorManager.addInterceptor({
-      id: 'test 2',
-      isInterceptWhen: (contextName) => {
-        // implement this
-        return true;
-      },
-      isProcessableParallel: () => {
-        // implement this
-        return false;
-      },
-      process: (contextName, ...args) => {
-        args[0].page.revision.body += 'fugafuga';
-        return Promise.resolve(...args);
-      }
-    });
-    // intercepter test 3
-    self.interceptorManager.addInterceptor({
-      id: 'test 2',
-      isInterceptWhen: (contextName) => {
-        // implement this
-        return true;
-      },
-      isProcessableParallel: () => {
-        // implement this
-        return true;
-      },
-      process: (contextName, ...args) => {
-        args[0].page.creator.name += '2(renamed)';
-        return Promise.resolve(...args);
-      }
-    });
-
     resolve();
   });
 };
@@ -421,7 +371,7 @@ Crowi.prototype.buildServer = function() {
 /**
  * require API for plugins
  *
- * @param {string} modulePath
+ * @param {string} modulePath relative path from /lib/crowi/index.js
  * @return {module}
  *
  * @memberof Crowi

+ 0 - 47
lib/util/basicInterceptor.js

@@ -1,47 +0,0 @@
-/**
- * Basic Interceptor class
- */
-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);
-  }
-
-}
-
-module.exports = BasicInterceptor;

+ 6 - 4
lib/util/interceptorManager.js

@@ -22,6 +22,8 @@ class InterceptorManager {
    * @param {BasicInterceptor[]} interceptors
    */
   addInterceptors(interceptors) {
+    const interceptorIds = interceptors.map((i) => i.getId());
+    debug(`adding interceptors '${interceptorIds}'`);
     this.interceptors = this.interceptors.concat(interceptors);
   }
 
@@ -32,7 +34,7 @@ class InterceptorManager {
    * @param {any} args
    */
   process(contextName, ...args) {
-    debug(`processing context '${contextName}'`);
+    debug(`processing the context '${contextName}'`);
 
     // filter only contexts matches to specified 'contextName'
     const matchInterceptors = this.interceptors.filter((i) => i.isInterceptWhen(contextName));
@@ -55,7 +57,7 @@ class InterceptorManager {
         }, Promise.resolve(...args)/* initial Promise */)
       ])
     ).then(() => {
-      debug(`end processing context '${contextName}'`);
+      debug(`end processing the context '${contextName}'`);
       return;
     });
   }
@@ -63,11 +65,11 @@ class InterceptorManager {
   doProcess(interceptor, contextName, ...args) {
     return interceptor.process(contextName, ...args)
       .then((...results) => {
-        debug(`processing '${interceptor.id}' in context '${contextName}'`);
+        debug(`processing '${interceptor.getId()}' in the context '${contextName}'`);
         return Promise.resolve(...results);
       })
       .catch((reason) => {
-        debug(`failed when processing '${interceptor.id}' in context '${contextName}'`);
+        debug(`failed when processing '${interceptor.id}' in the context '${contextName}'`);
         debug(reason);
         return Promise.resolve(...args);
       });