Skip to content

Commit

Permalink
feat: enhance some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed Apr 23, 2021
1 parent bb4dd62 commit 62a7d0e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 31 deletions.
29 changes: 27 additions & 2 deletions src/promise/async-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export interface AsyncPoolOpts {
maxConcurrency: number;
}

export interface AsyncPoolExecutorOpts {
/**
* 下一次优先执行
* @default false
*/
isPriority?: boolean;
}

/**
* 异步池,放入需要执行的异步任务,总并行执行任务不超过设置的最大并发数量
* @param options
Expand All @@ -28,17 +36,34 @@ function asyncPool(options: AsyncPoolOpts) {
}
}

function executor<T>(fn: PromiseFN<T>) {
/**
* 添加一个任务,返回当前任务的执行结果
* @param fn
* @param opts
*/
function executor<T>(fn: PromiseFN<T>, opts?: AsyncPoolExecutorOpts) {
return new Promise<T>((resolve, reject) => {
queue.push(() => fn().then(resolve, reject));
const _fn = () => fn().then(resolve, reject);
if (opts?.isPriority) {
queue.unshift(_fn);
} else {
queue.push(_fn);
}
next();
});
}

/**
* 清理掉当前所有任务
*/
function clear() {
queue.length = 0;
}

/**
* 当前任务长度
* @return queue len
*/
function length() {
return queue.length;
}
Expand Down
4 changes: 4 additions & 0 deletions src/promise/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export interface PromiseFN<T = any> {
(): Promise<T>;
}

export interface AnyPromiseFN<A = any, T = any> {
(arg?: A): Promise<T>;
}

export interface BaseRetryOpts {
/**
* 最大等待次数
Expand Down
55 changes: 37 additions & 18 deletions src/promise/promisify.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
interface LikeWXApi {
success?(res?: any): void;
fail?(res?: any): void;
}

export default function promisify<T>(
f: (cb: (err: any, res: T) => void) => void,
thisContext?: any,
): () => Promise<T>;
export default function promisify<A, T>(
f: (arg: A, cb: (err: any, res: T) => void) => void,
thisContext?: any,
): (arg: A) => Promise<T>;
export default function promisify<A, A2, T>(
f: (arg: A, arg2: A2, cb: (err: any, res: T) => void) => void,
thisContext?: any,
): (arg: A, arg2: A2) => Promise<T>;
export default function promisify<A, A2, A3, T>(
f: (arg: A, arg2: A2, arg3: A3, cb: (err: any, res: T) => void) => void,
thisContext?: any,
): (arg: A, arg2: A2, arg3: A3) => Promise<T>;
export default function promisify<A, A2, A3, A4, T>(
f: (arg: A, arg2: A2, arg3: A3, arg4: A4, cb: (err: any, res: T) => void) => void,
thisContext?: any,
): (arg: A, arg2: A2, arg3: A3, arg4: A4) => Promise<T>;
export default function promisify<A, A2, A3, A4, A5, T>(
f: (arg: A, arg2: A2, arg3: A3, arg4: A4, arg5: A5, cb: (err: any, res: T) => void) => void,
thisContext?: any,
): (arg: A, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise<T>;
/**
* promise化 success fail 参数的api
* @param func
* promisify fun
* @param f
* @param thisContext
* eg:
*/
function promisify<Options extends LikeWXApi, PromisifyResult>(func: (_: Options) => void) {
return (opt?: Options) =>
new Promise<PromisifyResult>((resolve, reject) =>
func({
...opt,
success: resolve,
fail: reject,
} as Options),
);
export default function promisify(f: any, thisContext?: any) {
return function () {
// eslint-disable-next-line prefer-rest-params
const args = Array.prototype.slice.call(arguments);
return new Promise((resolve, reject) => {
args.push((err: any, result: any) => (err ? reject(err) : resolve(result)));
f.apply(thisContext, args);
});
};
}

export default promisify;
22 changes: 11 additions & 11 deletions src/promise/sequence-chain.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { PromiseFN } from './interfaces';
import { AnyPromiseFN } from './interfaces';

/**
* 链式处理,向下传递结果
* eg:拦截器
* @param handlers
*/
function sequenceChain(handlers: PromiseFN[]) {
const chain = handlers.slice();
let promise = Promise.resolve();
while (chain.length) {
const fulfilled = chain.shift();
const rejected = chain.shift();
promise = promise.then(fulfilled, rejected);
}
return promise;
function sequenceChain(handlers: Array<[AnyPromiseFN, AnyPromiseFN]>) {
return <T>(arg?: any): Promise<T> => {
const chain = handlers.slice();
let promise = Promise.resolve(arg!);
while (chain.length) {
const [fulfilled, rejected] = chain.shift()!;
promise = promise.then(fulfilled, rejected);
}
return promise;
};
}

export default sequenceChain;

1 comment on commit 62a7d0e

@vercel
Copy link

@vercel vercel bot commented on 62a7d0e Apr 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

The most recent charge for your active payment method has failed. Please update it here: https://vercel.com/teams/planjs/settings/billing.

Please sign in to comment.