Skip to content

Commit de7d073

Browse files
author
farfromrefug
committed
chore: refactor src handling
1 parent 5d0341c commit de7d073

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/image/index-common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ export const needRequestImage = function (target: any, propertyKey: string | Sym
230230
};
231231
};
232232

233+
export type BasicSrcType = string | ImageSource | ImageAsset;
234+
export type SrcType = BasicSrcType | (() => BasicSrcType | PromiseLike<BasicSrcType>) | PromiseLike<BasicSrcType>;
233235
export abstract class ImageBase extends View {
234236
public static finalImageSetEvent: string = 'finalImageSet';
235237
public static failureEvent: string = 'failure';
@@ -238,7 +240,7 @@ export abstract class ImageBase extends View {
238240
public static releaseEvent: string = 'release';
239241
public static submitEvent: string = 'submit';
240242

241-
public src: string | ImageSource | ImageAsset;
243+
public src: SrcType;
242244
public lowerResSrc: string;
243245
public placeholderImageUri: string;
244246
public failureImageUri: string;

src/image/index.android.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ export class Img extends ImageBase {
444444
}
445445
get cacheKey() {
446446
const src = this.src;
447-
if (src && !(src instanceof ImageSource)) {
448-
return getUri(src);
447+
const srcType = typeof src;
448+
if (src && (srcType === 'string' || src instanceof ImageAsset)) {
449+
return getUri(src as string | ImageAsset);
449450
}
450451
return undefined;
451452
}
@@ -459,8 +460,8 @@ export class Img extends ImageBase {
459460
await imagePipeLine.evictFromCache(cacheKey);
460461
// }
461462
}
462-
this.src = null;
463-
this.src = src;
463+
this.handleImageSrc(null);
464+
this.initImage();
464465
}
465466

466467
@needUpdateHierarchy
@@ -581,13 +582,18 @@ export class Img extends ImageBase {
581582

582583
controllerListener: com.facebook.drawee.controller.ControllerListener<com.facebook.imagepipeline.image.ImageInfo>;
583584

584-
protected async initImage() {
585+
protected async handleImageSrc(src: SrcType) {
585586
const view = this.nativeViewProtected;
586587
if (view) {
587-
// this.nativeImageViewProtected.setImageURI(null);
588-
const src = this.src;
589588
if (src instanceof Promise) {
590-
this.src = await src;
589+
this.handleImageSrc(await src);
590+
return;
591+
} else if (typeof src === 'function') {
592+
const newSrc = src();
593+
if (newSrc instanceof Promise) {
594+
await newSrc;
595+
}
596+
this.handleImageSrc(newSrc);
591597
return;
592598
}
593599
if (src) {
@@ -788,6 +794,11 @@ export class Img extends ImageBase {
788794
}
789795
}
790796

797+
protected async initImage() {
798+
// this.nativeImageViewProtected.setImageURI(null);
799+
this.handleImageSrc(this.src);
800+
}
801+
791802
private updateHierarchy() {
792803
if (!this.mCanUpdateHierarchy) {
793804
this.mNeedUpdateHierarchy = true;

src/image/index.ios.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ImageInfo as ImageInfoBase,
1111
ImagePipelineConfigSetting,
1212
ScaleType,
13+
SrcType,
1314
Stretch,
1415
failureImageUriProperty,
1516
imageRotationProperty,
@@ -295,17 +296,17 @@ export class Img extends ImageBase {
295296
public async updateImageUri() {
296297
const imagePipeLine = getImagePipeline();
297298
const src = this.src;
298-
if (!(src instanceof ImageSource)) {
299-
const cachekKey = this.mCacheKey || getUri(src).absoluteString;
299+
const srcType = typeof src;
300+
if (src && (srcType === 'string' || src instanceof ImageAsset)) {
301+
const cachekKey = this.mCacheKey || getUri(src as string | ImageAsset).absoluteString;
300302
// const isInCache = imagePipeLine.isInBitmapMemoryCache(cachekKey);
301303
// if (isInCache) {
302304
await imagePipeLine.evictFromCache(cachekKey);
303305
// }
304306
}
305-
this.src = null;
306307
// ensure we clear the image as
307308
this._setNativeImage(null, false);
308-
this.src = src;
309+
this.initImage();
309310
}
310311

311312
public _setNativeImage(nativeImage: UIImage, animated = true) {
@@ -402,10 +403,21 @@ export class Img extends ImageBase {
402403
}
403404

404405
protected async initImage() {
406+
// this.nativeImageViewProtected.setImageURI(null);
407+
this.handleImageSrc(this.src);
408+
}
409+
410+
protected async handleImageSrc(src: SrcType) {
405411
if (this.nativeViewProtected) {
406-
const src = this.src;
407412
if (src instanceof Promise) {
408-
this.src = await src;
413+
this.handleImageSrc(await src);
414+
return;
415+
} else if (typeof src === 'function') {
416+
const newSrc = src();
417+
if (newSrc instanceof Promise) {
418+
await newSrc;
419+
}
420+
this.handleImageSrc(newSrc);
409421
return;
410422
}
411423
if (src) {
@@ -426,7 +438,7 @@ export class Img extends ImageBase {
426438
}
427439
}
428440

429-
const uri = getUri(src);
441+
const uri = getUri(src as string | ImageAsset);
430442
this.isLoading = true;
431443
let options = SDWebImageOptions.ScaleDownLargeImages | SDWebImageOptions.AvoidAutoSetImage;
432444

0 commit comments

Comments
 (0)