@gedit/utils
Advanced tools
Comparing version 0.1.31 to 0.1.32
@@ -1,2 +0,2 @@ | ||
export declare function iterToArray(iter: IterableIterator<any>): any[]; | ||
export declare function iterToArray<T = any>(iter: IterableIterator<T>): T[]; | ||
export declare function arrayToSet(arr: any[]): Set<any>; | ||
@@ -3,0 +3,0 @@ export declare function arrayUnion(arr: any[]): any[]; |
@@ -13,5 +13,5 @@ export declare function deepClone<T>(obj: T): T; | ||
export declare const omit: (obj: any, fields: string[], dest?: any) => any; | ||
export declare const reduce: <T = any>(obj: any, fn: (res: any, value: T, key: string) => any, res?: any) => T; | ||
export declare const mapValues: <T = any>(obj: any, fn: (value: T, key: string) => any) => T; | ||
export declare const mapKeys: <T = any>(obj: any, fn: (value: T, key: string) => any) => T; | ||
export declare const reduce: <V = any, r = any>(obj: any, fn: (res: any, value: V, key: string) => any, res?: any) => any; | ||
export declare const mapValues: <V = any>(obj: any, fn: (value: V, key: string) => any) => any; | ||
export declare const mapKeys: <V = any>(obj: any, fn: (value: V, key: string) => any) => any; | ||
/** | ||
@@ -18,0 +18,0 @@ * @param target |
@@ -32,3 +32,23 @@ /******************************************************************************** | ||
export declare function timeout(ms: number, token?: CancellationToken): Promise<void>; | ||
export declare function retry<T>(task: () => Promise<T>, delay: number, retries: number): Promise<T>; | ||
export declare function retry<T>(task: () => Promise<T>, delay: number, retries: number, shouldRetry?: (res: T) => boolean): Promise<T>; | ||
interface PromiseTask<T> { | ||
(): Promise<T>; | ||
} | ||
export interface PromisePoolOpts { | ||
intervalCount?: number; | ||
intervalTime?: number; | ||
retries?: number; | ||
retryDelay?: number; | ||
} | ||
export declare class PromisePool { | ||
protected opts: Required<PromisePoolOpts>; | ||
constructor(opts?: PromisePoolOpts); | ||
protected tryToExec<T>(task: PromiseTask<T>, checkIfRetry?: (res: T) => boolean): Promise<T>; | ||
/** | ||
* @param tasks 执行任务 | ||
* @param checkIfRetry 判断结果是否需要重试 | ||
*/ | ||
run<T>(tasks: PromiseTask<T>[], checkIfRetry?: (res: T) => boolean): Promise<T[]>; | ||
} | ||
export {}; | ||
//# sourceMappingURL=promise-util.d.ts.map |
@@ -17,2 +17,13 @@ "use strict"; | ||
********************************************************************************/ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -55,3 +66,3 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.retry = exports.timeout = exports.Deferred = exports.PromiseDeferred = void 0; | ||
exports.PromisePool = exports.retry = exports.timeout = exports.Deferred = exports.PromiseDeferred = void 0; | ||
var cancellation_1 = require("./cancellation"); | ||
@@ -89,5 +100,5 @@ /** | ||
exports.timeout = timeout; | ||
function retry(task, delay, retries) { | ||
function retry(task, delay, retries, shouldRetry) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var lastError, i, error_1; | ||
var lastError, result, i, error_1; | ||
return __generator(this, function (_a) { | ||
@@ -99,19 +110,30 @@ switch (_a.label) { | ||
case 1: | ||
if (!(i < retries)) return [3 /*break*/, 7]; | ||
if (!(i < retries)) return [3 /*break*/, 9]; | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 4, , 6]); | ||
_a.trys.push([2, 6, , 8]); | ||
return [4 /*yield*/, task()]; | ||
case 3: return [2 /*return*/, _a.sent()]; | ||
case 3: | ||
result = _a.sent(); | ||
if (!(shouldRetry && shouldRetry(result))) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, timeout(delay)]; | ||
case 4: | ||
_a.sent(); | ||
return [3 /*break*/, 8]; | ||
case 5: return [2 /*return*/, result]; | ||
case 6: | ||
error_1 = _a.sent(); | ||
lastError = error_1; | ||
return [4 /*yield*/, timeout(delay)]; | ||
case 5: | ||
case 7: | ||
_a.sent(); | ||
return [3 /*break*/, 6]; | ||
case 6: | ||
return [3 /*break*/, 8]; | ||
case 8: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 7: throw lastError; | ||
case 9: | ||
if (lastError) { | ||
throw lastError; | ||
} | ||
return [2 /*return*/, result]; | ||
} | ||
@@ -122,2 +144,59 @@ }); | ||
exports.retry = retry; | ||
var PromisePoolOptsDefault = { | ||
intervalCount: 10, | ||
intervalTime: 0, | ||
retries: 0, | ||
retryDelay: 10, | ||
}; | ||
var PromisePool = /** @class */ (function () { | ||
function PromisePool(opts) { | ||
if (opts === void 0) { opts = PromisePoolOptsDefault; } | ||
this.opts = __assign(__assign({}, PromisePoolOptsDefault), opts); | ||
} | ||
PromisePool.prototype.tryToExec = function (task, checkIfRetry) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
if (this.opts.retries === 0) | ||
return [2 /*return*/, task()]; | ||
return [2 /*return*/, retry(task, this.opts.retryDelay, this.opts.retries, checkIfRetry)]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* @param tasks 执行任务 | ||
* @param checkIfRetry 判断结果是否需要重试 | ||
*/ | ||
PromisePool.prototype.run = function (tasks, checkIfRetry) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var curTasks, promises, result, nextTasks, _a, _b; | ||
var _this = this; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (tasks.length === 0) | ||
return [2 /*return*/, []]; | ||
curTasks = tasks.slice(0, this.opts.intervalCount); | ||
promises = curTasks.map(function (task) { return _this.tryToExec(task, checkIfRetry); }); | ||
return [4 /*yield*/, Promise.all(promises)]; | ||
case 1: | ||
result = _c.sent(); | ||
nextTasks = tasks.slice(this.opts.intervalCount); | ||
if (nextTasks.length === 0) | ||
return [2 /*return*/, result]; | ||
if (!(this.opts.intervalTime !== 0)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, timeout(this.opts.intervalTime)]; | ||
case 2: | ||
_c.sent(); | ||
_c.label = 3; | ||
case 3: | ||
_b = (_a = result).concat; | ||
return [4 /*yield*/, this.run(nextTasks, checkIfRetry)]; | ||
case 4: return [2 /*return*/, _b.apply(_a, [_c.sent()])]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return PromisePool; | ||
}()); | ||
exports.PromisePool = PromisePool; | ||
//# sourceMappingURL=promise-util.js.map |
{ | ||
"name": "@gedit/utils", | ||
"version": "0.1.31", | ||
"version": "0.1.32", | ||
"license": "MIT", | ||
@@ -30,3 +30,3 @@ "main": "lib/common/index", | ||
}, | ||
"gitHead": "12d3eec48b48c60a7f56c6d4dabe5e94e087b7c9" | ||
"gitHead": "57110a48281a357dec142b9f4440efb7324100e5" | ||
} |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export function iterToArray(iter: IterableIterator<any>): any[] { | ||
export function iterToArray<T = any>(iter: IterableIterator<T>): T[] { | ||
const result = []; | ||
@@ -4,0 +4,0 @@ for (const v of iter) { |
@@ -86,7 +86,7 @@ /******************************************************************************** | ||
export const reduce = <T = any>(obj: any, fn: (res: any, value: T, key: string) => any, res: any = {}): T => keys(obj).reduce((r, k) => fn(r, obj[k], k), res); | ||
export const reduce = <V = any, r = any>(obj: any, fn: (res: any, value: V, key: string) => any, res: any = {}) => keys(obj).reduce((r, k) => fn(r, obj[k], k), res) as any; | ||
export const mapValues = <T = any>(obj: any, fn: (value: T, key: string) => any) => reduce<T>(obj, (res, value, key) => Object.assign(res, { [key]: fn(value, key) })); | ||
export const mapValues = <V = any>(obj: any, fn: (value: V, key: string) => any) => reduce<V>(obj, (res, value, key) => Object.assign(res, { [key]: fn(value, key) })); | ||
export const mapKeys = <T = any>(obj: any, fn: (value: T, key: string) => any) => reduce<T>(obj, (res, value, key) => Object.assign(res, { [fn(value, key)]: value })); | ||
export const mapKeys = <V = any>(obj: any, fn: (value: V, key: string) => any) => reduce<V>(obj, (res, value, key) => Object.assign(res, { [fn(value, key)]: value })); | ||
@@ -93,0 +93,0 @@ /** |
@@ -48,8 +48,14 @@ /******************************************************************************** | ||
export async function retry<T>(task: () => Promise<T>, delay: number, retries: number): Promise<T> { | ||
export async function retry<T>(task: () => Promise<T>, delay: number, retries: number, shouldRetry?: (res: T) => boolean): Promise<T> { | ||
let lastError: Error | undefined; | ||
let result: T; | ||
for (let i = 0; i < retries; i++) { | ||
try { | ||
return await task(); | ||
result = await task(); | ||
if (shouldRetry && shouldRetry(result)) { | ||
await timeout(delay); | ||
continue; | ||
} | ||
return result; | ||
} catch (error) { | ||
@@ -62,4 +68,50 @@ lastError = error; | ||
throw lastError; | ||
if (lastError) { | ||
throw lastError; | ||
} | ||
return result!; | ||
} | ||
interface PromiseTask<T> { | ||
(): Promise<T> | ||
} | ||
export interface PromisePoolOpts { | ||
intervalCount?: number, // 每批数目 | ||
intervalTime?: number, // 执行一批后的间隔时间, 默认没有间隔 | ||
retries?: number, // 如果某个执行失败, 尝试的次数,默认不尝试 | ||
retryDelay?: number | ||
} | ||
const PromisePoolOptsDefault: Required<PromisePoolOpts> = { | ||
intervalCount: 10, // 每批数目 | ||
intervalTime: 0, | ||
retries: 0, | ||
retryDelay: 10, | ||
}; | ||
export class PromisePool { | ||
protected opts: Required<PromisePoolOpts>; | ||
constructor(opts: PromisePoolOpts = PromisePoolOptsDefault) { | ||
this.opts = { ...PromisePoolOptsDefault, ...opts }; | ||
} | ||
protected async tryToExec<T>(task: PromiseTask<T>, checkIfRetry?: (res: T) => boolean): Promise<T> { | ||
if (this.opts.retries === 0) return task(); | ||
return retry<T>(task, this.opts.retryDelay, this.opts.retries, checkIfRetry); | ||
} | ||
/** | ||
* @param tasks 执行任务 | ||
* @param checkIfRetry 判断结果是否需要重试 | ||
*/ | ||
async run<T>(tasks: PromiseTask<T>[], checkIfRetry?: (res: T) => boolean): Promise<T[]> { | ||
if (tasks.length === 0) return []; | ||
const curTasks = tasks.slice(0, this.opts.intervalCount); | ||
const promises = curTasks.map(task => this.tryToExec<T>(task, checkIfRetry)); | ||
const result = await Promise.all(promises); | ||
const nextTasks = tasks.slice(this.opts.intervalCount); | ||
if (nextTasks.length === 0) return result; | ||
if (this.opts.intervalTime !== 0) await timeout(this.opts.intervalTime); | ||
return result.concat(await this.run(nextTasks, checkIfRetry)); | ||
} | ||
} |
@@ -63,3 +63,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
if (decoration.properties) { | ||
return mapValues<any>(decoration.properties, (v, k) => { | ||
return mapValues(decoration.properties, (v, k) => { | ||
const childKey = prefixKey + k; | ||
@@ -70,3 +70,3 @@ if (mixinDefaults && mixinDefaults[childKey] !== undefined) { | ||
return createDefault(v, mixinDefaults, childKey); | ||
}); | ||
}) as T; | ||
} | ||
@@ -73,0 +73,0 @@ return typeof decoration.default === 'function' ? decoration.default() : decoration.default; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 2 instances in 1 package
541620
202
10256