UNPKG

4.26 kBJavaScriptView Raw
1"use strict";
2var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12const promise_1 = require("./promise");
13/**
14 * 提供执行不同处理过程的协程
15 * 返回只有 resolve的方式,没有 reject的执行
16 * @param pools 执行程序池子
17 * @param threadNum 启动的执行协程数量
18 * @param args 每个方法执行的时候使用相同的参数组
19 */
20function 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}
48exports.execPools = execPools;
49function 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 * 返回只有 resolve的方式,没有 reject的执行
69 * @param exec 执行方法
70 * @param threadNum 协程数量
71 * @param args 执行的数据池子
72 */
73function 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}
101exports.execPools2 = execPools2;
102function 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}