|
@@ -48,32 +48,36 @@ class DelayManager {
|
|
names: readonly string[];
|
|
names: readonly string[];
|
|
groupName: string;
|
|
groupName: string;
|
|
skipNum: number;
|
|
skipNum: number;
|
|
- maxTimeout: number;
|
|
|
|
},
|
|
},
|
|
callback: Function
|
|
callback: Function
|
|
) {
|
|
) {
|
|
- let names = [...options.names];
|
|
|
|
- const { groupName, skipNum, maxTimeout } = options;
|
|
|
|
-
|
|
|
|
- while (names.length) {
|
|
|
|
- const list = names.slice(0, skipNum);
|
|
|
|
- names = names.slice(skipNum);
|
|
|
|
-
|
|
|
|
- let called = false;
|
|
|
|
- setTimeout(() => {
|
|
|
|
- if (!called) {
|
|
|
|
- called = true;
|
|
|
|
- callback();
|
|
|
|
- }
|
|
|
|
- }, maxTimeout);
|
|
|
|
-
|
|
|
|
- await Promise.all(list.map((n) => this.checkDelay(n, groupName)));
|
|
|
|
-
|
|
|
|
- if (!called) {
|
|
|
|
- called = true;
|
|
|
|
- callback();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ const { groupName, skipNum } = options;
|
|
|
|
+
|
|
|
|
+ const names = [...options.names];
|
|
|
|
+ const total = names.length;
|
|
|
|
+
|
|
|
|
+ let count = 0;
|
|
|
|
+ let current = 0;
|
|
|
|
+
|
|
|
|
+ return new Promise((resolve) => {
|
|
|
|
+ const help = async (): Promise<void> => {
|
|
|
|
+ if (current >= skipNum) return;
|
|
|
|
+
|
|
|
|
+ const task = names.shift();
|
|
|
|
+ if (!task) return;
|
|
|
|
+
|
|
|
|
+ current += 1;
|
|
|
|
+ await this.checkDelay(task, groupName);
|
|
|
|
+ current -= 1;
|
|
|
|
+
|
|
|
|
+ if (count++ % skipNum === 0 || count === total) callback();
|
|
|
|
+ if (count === total) resolve(null);
|
|
|
|
+
|
|
|
|
+ return help();
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ for (let i = 0; i < skipNum; ++i) help();
|
|
|
|
+ });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|