|
@@ -9,39 +9,69 @@ class InterceptorManager {
|
|
|
this.interceptors = [];
|
|
this.interceptors = [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * add an Interceptor
|
|
|
|
|
+ * @param {BasicInterceptor} interceptor
|
|
|
|
|
+ */
|
|
|
addInterceptor(interceptor) {
|
|
addInterceptor(interceptor) {
|
|
|
this.addInterceptors([interceptor]);
|
|
this.addInterceptors([interceptor]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * add Interceptors
|
|
|
|
|
+ * @param {BasicInterceptor[]} interceptors
|
|
|
|
|
+ */
|
|
|
addInterceptors(interceptors) {
|
|
addInterceptors(interceptors) {
|
|
|
this.interceptors = this.interceptors.concat(interceptors);
|
|
this.interceptors = this.interceptors.concat(interceptors);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * process Interceptors
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {string} contextName
|
|
|
|
|
+ * @param {any} args
|
|
|
|
|
+ */
|
|
|
process(contextName, ...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 matchInterceptors = this.interceptors.filter((i) => i.isInterceptWhen(contextName));
|
|
|
|
|
|
|
|
const parallels = matchInterceptors.filter((i) => i.isProcessableParallel());
|
|
const parallels = matchInterceptors.filter((i) => i.isProcessableParallel());
|
|
|
const sequentials = 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(
|
|
return Promise.all(
|
|
|
|
|
+ // parallel
|
|
|
parallels.map((interceptor) => {
|
|
parallels.map((interceptor) => {
|
|
|
- return interceptor.process(contextName, ...args);
|
|
|
|
|
|
|
+ return this.doProcess(interceptor, contextName, ...args);
|
|
|
})
|
|
})
|
|
|
|
|
+ // sequential
|
|
|
.concat([
|
|
.concat([
|
|
|
sequentials.reduce((prevPromise, nextInterceptor) => {
|
|
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(() => {
|
|
).then(() => {
|
|
|
- console.log("Promise.all().then()");
|
|
|
|
|
|
|
+ debug(`end processing context '${contextName}'`);
|
|
|
return;
|
|
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) => {
|
|
module.exports = (crowi) => {
|