@mtgoo/ctool
Advanced tools
| export declare class TaskPool { | ||
| private doingCount; | ||
| maxConcurrency: number; | ||
| errBreak: boolean; | ||
| constructor(options?: { | ||
| maxConcurrency: number; | ||
| errBreak: boolean; | ||
| }); | ||
| private tasks; | ||
| push<T = any>(task: () => Promise<T>): Promise<T>; | ||
| pushArray<T = any>(taskArr: (() => Promise<T>)[]): Promise<T[]>; | ||
| private execute; | ||
| } |
+122
-104
@@ -318,76 +318,107 @@ /** | ||
| /*! ***************************************************************************** | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
| this file except in compliance with the License. You may obtain a copy of the | ||
| License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
| WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
| MERCHANTABLITY OR NON-INFRINGEMENT. | ||
| See the Apache Version 2.0 License for specific language governing permissions | ||
| and limitations under the License. | ||
| ***************************************************************************** */ | ||
| function __awaiter(thisArg, _arguments, P, generator) { | ||
| return new (P || (P = Promise))(function (resolve, reject) { | ||
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
| function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
| }); | ||
| } | ||
| /** | ||
| * 将多条任务分割到多个小组中执行。 | ||
| * @param tasks 任务 | ||
| * @param options.batchCount 分组的数量, 默认: 10 | ||
| * @param options.waitTime 组任务执行间隔, 默认: 0 | ||
| * @param options.onprogress 每个任务完成后回调,返回总任务进度。() | ||
| * | ||
| * 将promise 任务的完成和失败控制句柄暴露在外部 | ||
| * @example | ||
| * ``` | ||
| * let task_a = ExposedPromise.create(); | ||
| * let task_b = ExposedPromise.create(); | ||
| * | ||
| * @description <br/> | ||
| * <br/> | ||
| * 可用于:耗时函数拆分、削峰等 | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const tasks = [...Array(100)].map(item => { | ||
| * return () => new Promise((resolve, reject) => { | ||
| * setTimeout(() => resolve(1), 1000); | ||
| * }); | ||
| * let taskGroup = Promise.all([task_a.ins, task_b.ins]); | ||
| * taskGroup.then(() => { | ||
| * console.log("all finish!"); | ||
| * }); | ||
| * executePromisesByBatch(tasks, { | ||
| * onprogress: (progress, result) => { | ||
| * console.log("progress", progress, result); | ||
| * } | ||
| * }); | ||
| * | ||
| * task_a.ins.then(() => { | ||
| * console.log("task a finish"); | ||
| * }) | ||
| * | ||
| * task_b.ins.then(() => { | ||
| * console.log("task b finish"); | ||
| * }) | ||
| * | ||
| * setTimeout(() => task_a.resolve(), 1000); | ||
| * setTimeout(() => task_b.resolve(), 3000); | ||
| * ``` | ||
| */ | ||
| function executePromisesByBatch(tasks, options) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const { batchCount = 10, waitTime = 0, onprogress } = options || {}; | ||
| const count = Math.ceil(tasks.length / batchCount); | ||
| let results = []; | ||
| for (let i = 0; i < count; i++) { | ||
| const currentTasks = tasks.slice(i * batchCount, Math.min((i + 1) * batchCount, tasks.length)); | ||
| yield new Promise((resolve, reject) => { | ||
| Promise.all(currentTasks.map(item => { | ||
| return item() | ||
| .then((res) => { | ||
| results.push(res); | ||
| onprogress === null || onprogress === void 0 ? void 0 : onprogress(results.length / tasks.length, res); | ||
| }); | ||
| })) | ||
| .then(() => { | ||
| setTimeout(() => resolve(), waitTime); | ||
| class ExposedPromise { | ||
| constructor() { } | ||
| static create() { | ||
| let newTask = new ExposedPromise(); | ||
| newTask.ins = new Promise((resolve, reject) => { | ||
| newTask.resolve = resolve; | ||
| newTask.reject = reject; | ||
| }); | ||
| return newTask; | ||
| } | ||
| } | ||
| class OncePromise { | ||
| constructor() { | ||
| this.beExecuted = false; | ||
| } | ||
| static create() { | ||
| let newTask = new OncePromise(); | ||
| newTask.ins = new Promise((resolve, reject) => { | ||
| newTask.resolve = (result) => { | ||
| if (!newTask.beExecuted) { | ||
| newTask.beExecuted = true; | ||
| resolve(result); | ||
| } | ||
| }; | ||
| newTask.reject = (err) => { | ||
| if (!newTask.beExecuted) { | ||
| newTask.beExecuted = true; | ||
| reject(err); | ||
| } | ||
| }; | ||
| }); | ||
| return newTask; | ||
| } | ||
| } | ||
| class TaskPool { | ||
| constructor(options) { | ||
| var _a, _b; | ||
| //执行中数量 | ||
| this.doingCount = 0; | ||
| this.tasks = []; | ||
| this.maxConcurrency = (_a = options.maxConcurrency) !== null && _a !== void 0 ? _a : 5; | ||
| this.errBreak = (_b = options.errBreak) !== null && _b !== void 0 ? _b : false; | ||
| } | ||
| push(task) { | ||
| let onEnd = OncePromise.create(); | ||
| this.tasks.push({ task, onEnd }); | ||
| this.execute(); | ||
| return onEnd.ins; | ||
| } | ||
| pushArray(taskArr) { | ||
| let arr = taskArr.map(el => { | ||
| let onEnd = OncePromise.create(); | ||
| this.tasks.push({ task: el, onEnd }); | ||
| return onEnd.ins; | ||
| }); | ||
| this.execute(); | ||
| return Promise.all(arr); | ||
| } | ||
| execute() { | ||
| if (this.doingCount < this.maxConcurrency) { | ||
| let task = this.tasks.shift(); | ||
| if (task != null) { | ||
| this.doingCount++; | ||
| task.task() | ||
| .then((result) => task.onEnd.resolve(result)) | ||
| .catch(err => { | ||
| task.onEnd.reject(err); | ||
| if (this.errBreak) { | ||
| this.tasks.forEach(el => el.onEnd.reject(new Error("other task in pool failed"))); | ||
| this.tasks = []; | ||
| } | ||
| }) | ||
| .catch((err) => reject(err)); | ||
| }); | ||
| .finally(() => { | ||
| this.doingCount--; | ||
| this.execute(); | ||
| }); | ||
| } | ||
| } | ||
| return results; | ||
| }); | ||
| } | ||
| } | ||
@@ -546,2 +577,26 @@ | ||
| /*! ***************************************************************************** | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
| this file except in compliance with the License. You may obtain a copy of the | ||
| License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
| WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
| MERCHANTABLITY OR NON-INFRINGEMENT. | ||
| See the Apache Version 2.0 License for specific language governing permissions | ||
| and limitations under the License. | ||
| ***************************************************************************** */ | ||
| function __awaiter(thisArg, _arguments, P, generator) { | ||
| return new (P || (P = Promise))(function (resolve, reject) { | ||
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
| function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
| }); | ||
| } | ||
| /** | ||
@@ -837,39 +892,2 @@ * websocket封装 | ||
| /** | ||
| * | ||
| * 将promise 任务的完成和失败控制句柄暴露在外部 | ||
| * @example | ||
| * ``` | ||
| * let task_a = ExposedPromise.create(); | ||
| * let task_b = ExposedPromise.create(); | ||
| * | ||
| * let taskGroup = Promise.all([task_a.ins, task_b.ins]); | ||
| * taskGroup.then(() => { | ||
| * console.log("all finish!"); | ||
| * }); | ||
| * | ||
| * task_a.ins.then(() => { | ||
| * console.log("task a finish"); | ||
| * }) | ||
| * | ||
| * task_b.ins.then(() => { | ||
| * console.log("task b finish"); | ||
| * }) | ||
| * | ||
| * setTimeout(() => task_a.resolve(), 1000); | ||
| * setTimeout(() => task_b.resolve(), 3000); | ||
| * ``` | ||
| */ | ||
| class ExposedPromise { | ||
| constructor() { } | ||
| static create() { | ||
| let newTask = new ExposedPromise(); | ||
| newTask.ins = new Promise((resolve, reject) => { | ||
| newTask.resolve = resolve; | ||
| newTask.reject = reject; | ||
| }); | ||
| return newTask; | ||
| } | ||
| } | ||
| /** | ||
| * 基于JSON-RPC2.0实现的客户端 | ||
@@ -1001,3 +1019,3 @@ * | ||
| return Promise.reject({ "message": "websocket还未连接." }); | ||
| let task = ExposedPromise.create(); | ||
| let task = OncePromise.create(); | ||
| let count = 0; | ||
@@ -1588,3 +1606,3 @@ let content = requests.map((item, index) => { | ||
| export { DebuffAction, EventEmitter, EventTarget, ExposedPromise, Interceptor, ObjectPool, Observable, ObservableData, PersistPromise, RecurveObservable, ExposedPromise as TaskPromise, Timer, tinyHFSM as TinyHFSM, tinyRpc as TinyRpc, TinyWsClient, UUID, WSEventEnum, Watch, executePromisesByBatch, retryPromise, wrapPromise }; | ||
| export { DebuffAction, EventEmitter, EventTarget, ExposedPromise, Interceptor, ObjectPool, Observable, ObservableData, OncePromise, PersistPromise, RecurveObservable, TaskPool, ExposedPromise as TaskPromise, Timer, tinyHFSM as TinyHFSM, tinyRpc as TinyRpc, TinyWsClient, UUID, WSEventEnum, Watch, retryPromise, wrapPromise }; | ||
| //# sourceMappingURL=ctool.es.js.map |
@@ -33,2 +33,10 @@ /** | ||
| } | ||
| export declare class OncePromise<T = void> { | ||
| resolve: (value: T) => void; | ||
| reject: (error: any) => void; | ||
| ins: Promise<T>; | ||
| private beExecuted; | ||
| private constructor(); | ||
| static create<T = void>(): OncePromise<T>; | ||
| } | ||
| export { ExposedPromise as TaskPromise }; |
@@ -5,3 +5,3 @@ export * from "./debuffAction"; | ||
| export * from "./retryPromise"; | ||
| export * from "./executePromisesByBatch"; | ||
| export * from "./taskPool"; | ||
| export * from "./wrapPromise"; | ||
@@ -8,0 +8,0 @@ export * from "./uuid"; |
+1
-1
| { | ||
| "name": "@mtgoo/ctool", | ||
| "version": "0.1.4", | ||
| "version": "0.1.5", | ||
| "main": "./dist/ctool.js", | ||
@@ -5,0 +5,0 @@ "module": "./dist/ctool.es.js", |
| /** | ||
| * 将多条任务分割到多个小组中执行。 | ||
| * @param tasks 任务 | ||
| * @param options.batchCount 分组的数量, 默认: 10 | ||
| * @param options.waitTime 组任务执行间隔, 默认: 0 | ||
| * @param options.onprogress 每个任务完成后回调,返回总任务进度。() | ||
| * | ||
| * | ||
| * @description <br/> | ||
| * <br/> | ||
| * 可用于:耗时函数拆分、削峰等 | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const tasks = [...Array(100)].map(item => { | ||
| * return () => new Promise((resolve, reject) => { | ||
| * setTimeout(() => resolve(1), 1000); | ||
| * }); | ||
| * }); | ||
| * executePromisesByBatch(tasks, { | ||
| * onprogress: (progress, result) => { | ||
| * console.log("progress", progress, result); | ||
| * } | ||
| * }); | ||
| * | ||
| * ``` | ||
| */ | ||
| export declare function executePromisesByBatch<T>(tasks: (() => Promise<T>)[], options?: { | ||
| batchCount?: number; | ||
| waitTime?: number; | ||
| onprogress?: (progress: number, result?: T) => void; | ||
| }): Promise<T[]>; |
Sorry, the diff of this file is too big to display
166573
1.1%2708
0.26%