Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
iOS: Ensure onLoad and onLoadEnd get called
Browse files Browse the repository at this point in the history
Setting properties from JS to native components does not guarantee any
order (as far as I know). Because of this, `setSource` can get called
and complete before `setOnFastImageLoad` and `setOnFastImageLoadEnd`,
which results in the callbacks never firing.

This change implements a simple check for these callback properties
getting set after the image has loaded, and fires them at that time.
  • Loading branch information
danhodos committed Oct 19, 2017
1 parent f2d9671 commit ddebbc1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ios/FastImage/FFFastImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

@implementation FFFastImageView {
BOOL hasSentOnLoadStart;
BOOL isComplete;
BOOL hasError;
}

- (void)setResizeMode:(RCTResizeMode)resizeMode
Expand All @@ -12,6 +14,20 @@ - (void)setResizeMode:(RCTResizeMode)resizeMode
}
}

- (void)setOnFastImageLoadEnd:(RCTBubblingEventBlock)onFastImageLoadEnd {
_onFastImageLoadEnd = onFastImageLoadEnd;
if (isComplete) {
_onFastImageLoadEnd(@{});
}
}

- (void)setOnFastImageLoad:(RCTBubblingEventBlock)onFastImageLoad {
_onFastImageLoad = onFastImageLoad;
if (isComplete && hasError == NO) {
_onFastImageLoad(@{});
}
}

- (void)setOnFastImageLoadStart:(RCTBubblingEventBlock)onFastImageLoadStart {
if (_source && !hasSentOnLoadStart) {
_onFastImageLoadStart = onFastImageLoadStart;
Expand All @@ -25,6 +41,9 @@ - (void)setOnFastImageLoadStart:(RCTBubblingEventBlock)onFastImageLoadStart {

- (void)setSource:(FFFastImageSource *)source {
if (_source != source) {
isComplete = NO;
hasError = NO;

_source = source;

// Set headers.
Expand Down Expand Up @@ -70,7 +89,9 @@ - (void)setSource:(FFFastImageSource *)source {
NSError * _Nullable error,
SDImageCacheType cacheType,
NSURL * _Nullable imageURL) {
isComplete = YES;
if (error) {
hasError = YES;
if (_onFastImageError) {
_onFastImageError(@{});
if (_onFastImageLoadEnd) {
Expand Down

0 comments on commit ddebbc1

Please sign in to comment.