修复自定义下载失败无失败回调以及新增全局自定义下载接口

Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
zgf
2024-07-26 15:15:47 +08:00
parent 19e1ba9528
commit 44b3ca48ef
9 changed files with 120 additions and 5 deletions

View File

@@ -38,7 +38,7 @@ export class ImageKnife {
private _isRequestInSubThread: boolean = true;
//定义全局网络请求header map
headerMap: Map<string, Object> = new Map<string, Object>();
customGetImage: ((context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>) | undefined = undefined
public static getInstance(): ImageKnife {
if (!ImageKnife.instance) {
ImageKnife.instance = new ImageKnife();
@@ -380,4 +380,14 @@ export class ImageKnife {
getEngineKeyImpl(): IEngineKey {
return this.dispatcher.getEngineKeyImpl();
}
/**
* 全局设置自定义下载
* @param customGetImage 自定义请求函数
*/
setCustomGetImage(customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>) {
this.customGetImage = customGetImage
}
getCustomGetImage(): undefined | ((context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>){
return this.customGetImage
}
}

View File

@@ -179,7 +179,9 @@ export class ImageKnifeDispatcher {
isAnimator:isAnimator
}
if(request.customGetImage == undefined) {
request.customGetImage = ImageKnife.getInstance().getCustomGetImage()
}
if (ImageKnife.getInstance().isRequestInSubThread){
// 启动线程下载和解码主图
LogUtil.log("ImageKnife_DataTime_getAndShowImage_Task.start:" + currentRequest.imageKnifeOption.loadSrc)
@@ -399,6 +401,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
if (resBuf === undefined) {
LogUtil.log("customGetImage customGetImage");
resBuf = await request.customGetImage(request.context, request.src)
loadError = resBuf == undefined ? "customGetImage loadFile" : loadError
// 保存文件缓存
if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) {
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug不能直接获取resBuf.byteLength