144 lines
3.6 KiB
TypeScript
144 lines
3.6 KiB
TypeScript
|
/// <reference lib="es6" />
|
|||
|
/**
|
|||
|
* 微信小程序操作队列封装管理
|
|||
|
* @example var rq = new WxQueue(wx.requst);
|
|||
|
* @template TParam 微信操作参数类型
|
|||
|
* @template TTask 微信操返回task类型
|
|||
|
*/
|
|||
|
export declare class WxQueue<TParam extends BaseOption, TTask extends BaseTask> {
|
|||
|
/**
|
|||
|
* 队列最大长度
|
|||
|
*/
|
|||
|
readonly MAX: number;
|
|||
|
/**
|
|||
|
* 任务ID计数器
|
|||
|
*/
|
|||
|
private taskid;
|
|||
|
/**
|
|||
|
* 待完成队列
|
|||
|
*/
|
|||
|
private readonly todo;
|
|||
|
/**
|
|||
|
* 保持正在运行的任务
|
|||
|
*/
|
|||
|
private readonly taskMap;
|
|||
|
/**
|
|||
|
* 小程序的原始操作API
|
|||
|
*/
|
|||
|
private readonly operator;
|
|||
|
/**
|
|||
|
* 创建Wx操作队列
|
|||
|
* @param wxFunc Wx操作函数
|
|||
|
* @param maxLength 最大队列长度,默认10
|
|||
|
*/
|
|||
|
constructor(wxFunc: (params: TParam) => TTask, maxLength?: number);
|
|||
|
/**
|
|||
|
* 向队列中添加操作
|
|||
|
* @param param 微信操作
|
|||
|
*/
|
|||
|
push(param: QueueOption<TParam>): TTask;
|
|||
|
/**
|
|||
|
* check and do next task
|
|||
|
*/
|
|||
|
private _next;
|
|||
|
/**
|
|||
|
* process a task
|
|||
|
* @param id task ID
|
|||
|
* @param options task param
|
|||
|
*/
|
|||
|
private _process;
|
|||
|
/**
|
|||
|
* stop and remove a task
|
|||
|
* @param taskid - the id of task to abort
|
|||
|
*/
|
|||
|
private _abort;
|
|||
|
/**
|
|||
|
* progress update callback
|
|||
|
* https://developers.weixin.qq.com/miniprogram/dev/api/network/download/DownloadTask.onProgressUpdate.html
|
|||
|
* @param taskid - task id
|
|||
|
* @param callback 回调操作
|
|||
|
*/
|
|||
|
private _onProgress;
|
|||
|
private _onHeaders;
|
|||
|
}
|
|||
|
export declare type QueueOption<T> = T & ExtraOptions;
|
|||
|
/**
|
|||
|
* 微信操作参数声明
|
|||
|
*/
|
|||
|
interface ExtraOptions {
|
|||
|
/**
|
|||
|
* progress 回调
|
|||
|
*/
|
|||
|
onProgressUpdate?: wx.UploadTaskOnProgressUpdateCallback | wx.DownloadTaskOnProgressUpdateCallback;
|
|||
|
/**
|
|||
|
* 是否插队
|
|||
|
*/
|
|||
|
jump?: boolean;
|
|||
|
/**
|
|||
|
* 自定义超时时间
|
|||
|
*/
|
|||
|
timeout?: number;
|
|||
|
/**
|
|||
|
* 记录时间戳
|
|||
|
*/
|
|||
|
timestamp?: boolean | object;
|
|||
|
/**
|
|||
|
* 开发者服务器返回的 HTTP Response Header 回调
|
|||
|
*/
|
|||
|
onHeadersReceived?(result: {
|
|||
|
header: object;
|
|||
|
}): void;
|
|||
|
}
|
|||
|
interface BaseOption {
|
|||
|
/** 开发者服务器接口地址 */
|
|||
|
url: string;
|
|||
|
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
|
|||
|
complete?: Function;
|
|||
|
/** 接口调用失败的回调函数 */
|
|||
|
fail?: Function;
|
|||
|
/** 接口调用成功的回调函数 */
|
|||
|
success?: Function;
|
|||
|
}
|
|||
|
interface BaseTask {
|
|||
|
abort(): void;
|
|||
|
/** HTTP Response Header 事件的回调函数 */
|
|||
|
onHeadersReceived(callback: ExtraOptions['onHeadersReceived']): void;
|
|||
|
/** 下载进度变化事件的回调函数 */
|
|||
|
onProgressUpdate?(callback: ExtraOptions['onProgressUpdate']): void;
|
|||
|
}
|
|||
|
declare namespace wx {
|
|||
|
type UploadTaskOnProgressUpdateCallback = (res: {
|
|||
|
/**
|
|||
|
* 上传进度百分比
|
|||
|
*/
|
|||
|
progress: number;
|
|||
|
/**
|
|||
|
* 已经上传的数据长度,单位 Bytes
|
|||
|
*/
|
|||
|
totalBytesSent: number;
|
|||
|
/**
|
|||
|
* 预期需要上传的数据总长度,单位 Bytes
|
|||
|
*/
|
|||
|
totalBytesExpectedToSend: number;
|
|||
|
}) => void;
|
|||
|
type DownloadTaskOnProgressUpdateCallback = (res: {
|
|||
|
/**
|
|||
|
* 下载进度百分比
|
|||
|
*/
|
|||
|
progress: number;
|
|||
|
/**
|
|||
|
* 已经下载的数据长度,单位 Bytes
|
|||
|
*/
|
|||
|
totalBytesWritten: number;
|
|||
|
/**
|
|||
|
* 预期需要下载的数据总长度,单位 Bytes
|
|||
|
*/
|
|||
|
totalBytesExpectedToWrite: number;
|
|||
|
}) => void;
|
|||
|
}
|
|||
|
export interface TimeRecorder {
|
|||
|
send?: number;
|
|||
|
response?: number;
|
|||
|
}
|
|||
|
export {};
|
|||
|
//# sourceMappingURL=index.d.ts.map
|