|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import { cpus } from 'os'; |
18 | 17 | import { Context, Compression, OrderedCompressionValues, maxSize } from './validation/Condition';
|
19 | 18 | import { readFile } from './helpers/fs';
|
20 | 19 | import { Report } from './log/report';
|
21 | 20 | import { compressor } from './compressor';
|
22 |
| - |
23 |
| -const COMPRESSION_CONCURRENCY = cpus().length; |
| 21 | +import { pool } from '@kristoferbaxter/async'; |
24 | 22 |
|
25 | 23 | export interface CompressionItem {
|
26 | 24 | path: string;
|
@@ -84,31 +82,8 @@ export default async function compress(
|
84 | 82 | return true;
|
85 | 83 | }
|
86 | 84 |
|
87 |
| - const returnable: Array<Promise<boolean>> = []; |
88 |
| - const executing: Array<Promise<boolean>> = []; |
89 | 85 | const reportInstance: Report | null = report ? new report(context) : null;
|
90 |
| - let success = true; |
91 |
| - for (const item of toCompress) { |
92 |
| - const promise: Promise<boolean> = Promise.resolve(item).then((item) => compressor(context, reportInstance, item)); |
93 |
| - returnable.push(promise); |
94 |
| - |
95 |
| - if (COMPRESSION_CONCURRENCY <= toCompress.length) { |
96 |
| - const execute: any = promise.then((successful) => { |
97 |
| - if (!successful) { |
98 |
| - success = successful; |
99 |
| - } |
100 |
| - executing.splice(executing.indexOf(execute), 1); |
101 |
| - }); |
102 |
| - executing.push(execute); |
103 |
| - if (executing.length >= COMPRESSION_CONCURRENCY) { |
104 |
| - await Promise.race(executing); |
105 |
| - } |
106 |
| - } |
107 |
| - } |
108 |
| - if ((await Promise.all(returnable)).includes(false)) { |
109 |
| - success = false; |
110 |
| - } |
111 |
| - |
| 86 | + const successful = await pool(toCompress, (item: CompressionItem) => compressor(context, reportInstance, item)); |
112 | 87 | reportInstance?.end();
|
113 |
| - return success; |
| 88 | + return successful.every((success) => success); |
114 | 89 | }
|
0 commit comments