interceptorManager.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var debug = require('debug')('crowi:InterceptorManager')
  2. /**
  3. * the manager class of Interceptor
  4. */
  5. class InterceptorManager {
  6. constructor(crowi) {
  7. this.interceptors = [];
  8. }
  9. addInterceptor(interceptor) {
  10. this.addInterceptors([interceptor]);
  11. }
  12. addInterceptors(interceptors) {
  13. this.interceptors = this.interceptors.concat(interceptors);
  14. }
  15. process(contextName, ...args) {
  16. // filter only context matches
  17. const matchInterceptors = this.interceptors.filter((i) => i.isInterceptWhen(contextName));
  18. const parallels = matchInterceptors.filter((i) => i.isProcessableParallel());
  19. const sequentials = matchInterceptors.filter((i) => !i.isProcessableParallel());
  20. debug(`processing parallels(${parallels.length})`);
  21. debug(`processing sequentials(${sequentials.length})`);
  22. return Promise.all(
  23. parallels.map((interceptor) => {
  24. return interceptor.process(contextName, args);
  25. })
  26. // .concat[
  27. // sequentials.map((interceptor) => (results) => {
  28. // interceptor.process(contextName, results)
  29. // }).reduce((promise, func) => {
  30. // return promise.then((results) => func(results));
  31. // }, Promise.resolve(args)).then(() => { return Promise.resolve() })
  32. // ]
  33. ).then(() => {
  34. console.log("Promise.all().then()");
  35. return;
  36. });
  37. }
  38. }
  39. module.exports = (crowi) => {
  40. return new InterceptorManager(crowi);
  41. }