1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const promise_1 = require("./promise");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function execPools(pools, threadNum, ...args) {
|
21 | let running_thread = [];
|
22 | for (let i = 0; i < threadNum; i++) {
|
23 | running_thread.push([]);
|
24 | }
|
25 |
|
26 | for (let i = 0; i < pools.length; i++) {
|
27 | running_thread[i % threadNum].push(pools[i]);
|
28 | }
|
29 | return new Promise(function (resolve) {
|
30 | let count = 0;
|
31 | let succPool = [];
|
32 | let failPool = [];
|
33 | for (let i = 0; i < threadNum; i++) {
|
34 | promise_1.createPromise(exec(i, threadNum, running_thread[i], ...args))
|
35 | .then(function (result) {
|
36 | failPool.push(...result.fail);
|
37 | succPool.push(...result.succ);
|
38 | })
|
39 | .finally(function () {
|
40 | count++;
|
41 | if (count >= threadNum) {
|
42 | resolve({ succ: succPool, fail: failPool });
|
43 | }
|
44 | });
|
45 | }
|
46 | });
|
47 | }
|
48 | exports.execPools = execPools;
|
49 | function exec(theadIdx, threadNum, pools, ...args) {
|
50 | return __awaiter(this, void 0, void 0, function* () {
|
51 | let succ = [];
|
52 | let fail = [];
|
53 | for (let i = 0; i < pools.length; i++) {
|
54 | let idx = theadIdx + i * threadNum;
|
55 | try {
|
56 | yield pools[i](...args);
|
57 | succ.push(idx);
|
58 | }
|
59 | catch (e) {
|
60 | fail.push(idx);
|
61 | }
|
62 | }
|
63 | return { succ: succ, fail: fail };
|
64 | });
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function execPools2(exec, threadNum, ...args) {
|
74 | let running_thread = [];
|
75 | for (let i = 0; i < threadNum; i++) {
|
76 | running_thread.push([]);
|
77 | }
|
78 |
|
79 | for (let i = 0; i < args.length; i++) {
|
80 | running_thread[i % threadNum].push(args[i]);
|
81 | }
|
82 | return new Promise(function (resolve) {
|
83 | let count = 0;
|
84 | let succPool = [];
|
85 | let failPool = [];
|
86 | for (let i = 0; i < threadNum; i++) {
|
87 | promise_1.createPromise(exec2(i, threadNum, exec, ...running_thread[i]))
|
88 | .then(function (result) {
|
89 | failPool.push(...result.fail);
|
90 | succPool.push(...result.succ);
|
91 | })
|
92 | .finally(function () {
|
93 | count++;
|
94 | if (count >= threadNum) {
|
95 | resolve({ succ: succPool, fail: failPool });
|
96 | }
|
97 | });
|
98 | }
|
99 | });
|
100 | }
|
101 | exports.execPools2 = execPools2;
|
102 | function exec2(theadIdx, threadNum, run_exec, ...args) {
|
103 | return __awaiter(this, void 0, void 0, function* () {
|
104 | let succ = [];
|
105 | let fail = [];
|
106 | for (let i = 0; i < args.length; i++) {
|
107 | let idx = theadIdx + i * threadNum;
|
108 | try {
|
109 | yield run_exec(args[i]);
|
110 | succ.push(idx);
|
111 | }
|
112 | catch (e) {
|
113 | fail.push(idx);
|
114 | }
|
115 | }
|
116 | return { succ: succ, fail: fail };
|
117 | });
|
118 | }
|