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

Improvement: Detach code blocks correctly

* ensure to be able to add interceptor with order
Yuki Takei 7 лет назад
Родитель
Сommit
ac2e174d16
1 измененных файлов с 31 добавлено и 5 удалено
  1. 31 5
      lib/util/interceptor-manager.js

+ 31 - 5
lib/util/interceptor-manager.js

@@ -6,25 +6,51 @@ const logger = require('@alias/logger')('growi:InterceptorManager');
 class InterceptorManager {
 
   constructor() {
+    this.interceptorAndOrders = []; /* [
+                                          {interceptor: instanceA, order: 200 },
+                                          {interceptor: instanceB, order: 100 },
+                                          ...
+                                       ] */
     this.interceptors = [];
   }
 
   /**
    * add an Interceptor
    * @param {BasicInterceptor} interceptor
+   * @param {number} order
    */
-  addInterceptor(interceptor) {
-    this.addInterceptors([interceptor]);
+  addInterceptor(interceptor, order) {
+    this.addInterceptors([interceptor], order);
   }
 
   /**
    * add Interceptors
    * @param {BasicInterceptor[]} interceptors
+   * @param {number} order
    */
-  addInterceptors(interceptors) {
+  addInterceptors(interceptors, order) {
+    let isDefaultOrder = false;
+    if (order == null) {
+      order = 100;
+      isDefaultOrder = true;
+    }
+
     const interceptorIds = interceptors.map((i) => i.getId());
-    logger.debug(`adding interceptors '${interceptorIds}'`);
-    this.interceptors = this.interceptors.concat(interceptors);
+    logger.info(`'addInterceptors' invoked. adding interceptors '${interceptorIds}' at order=${order}${isDefaultOrder ? '(default)' : ''}`);
+
+    this.interceptorAndOrders = this.interceptorAndOrders.concat(
+      interceptors.map(interceptor => {
+        return { interceptor, order };
+      })
+    );
+
+    // sort asc
+    this.interceptorAndOrders.sort((a, b) => a.order - b.order);
+    // store sorted list
+    this.interceptors = this.interceptorAndOrders.map(obj => obj.interceptor);
+
+    const thisInterceptorIds = this.interceptors.map((i) => i.getId());
+    logger.info(`interceptors list has initialized: ${thisInterceptorIds}`);
   }
 
   /**