Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/Pipe.ts')
-rw-r--r--src/util/Pipe.ts26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/util/Pipe.ts b/src/util/Pipe.ts
index 359bba6a..95dad395 100644
--- a/src/util/Pipe.ts
+++ b/src/util/Pipe.ts
@@ -1,14 +1,14 @@
+import Log from './Log';
+
const MAX_PRIORITY = 100;
const MIN_PRIORITY = 0;
-type Params = any[];
-
-export default class Pipe<params extends Params = any[]> {
- private pipe = [];
+export default class Pipe<Params extends any[] = any[]> {
+ private pipe: ((...args: Params) => Promise<Params> | Params)[][] = [];
constructor() {}
- public addProcessor(processor: (...args: params) => Promise<params> | params, priority: number = 50) {
+ public addProcessor(processor: (...args: Params) => Promise<Params> | Params, priority: number = 50) {
if (isNaN(priority) || priority < MIN_PRIORITY || priority > MAX_PRIORITY) {
throw new Error('Priority has to be between 0 and 100');
}
@@ -20,7 +20,7 @@ export default class Pipe<params extends Params = any[]> {
this.pipe[priority].push(processor);
}
- public run(...args: params): Promise<params> {
+ public run(...args: Params): Promise<Params> {
let chain = Promise.resolve(args);
this.pipe.forEach(processors => {
@@ -34,9 +34,17 @@ export default class Pipe<params extends Params = any[]> {
}
processors.forEach(processor => {
- chain = chain.then((args2: any[]) => {
- return processor.apply(this, args2);
- });
+ chain = chain
+ .then((args2: Params) => {
+ return processor.apply(this, args2);
+ })
+ .then(args3 => {
+ if (args.length !== args3.length) {
+ Log.warn('Bad processor detected', processor);
+ }
+
+ return args3;
+ });
});
});