Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ - (instancetype)initWithFrame:(CGRect)frame
_imageView.layer.minificationFilter = kCAFilterTrilinear;
_imageView.layer.magnificationFilter = kCAFilterTrilinear;

_imageResponseObserverProxy = std::make_shared<RCTImageResponseObserverProxy>(self);

self.contentView = _imageView;
}

Expand Down Expand Up @@ -124,6 +122,11 @@ - (void)_setStateAndResubscribeImageResponseObserver:(const ImageShadowNode::Con
_state = state;

if (_state) {
// A new observer per subscription: callbacks of a previous request can still be queued on the
// main queue (e.g. after this view was recycled and reused), and must not be applied here.
// The callbacks are matched by the proxy's address. The new proxy is allocated before the
// previous one is released, so two consecutive subscriptions never share an address.
_imageResponseObserverProxy = std::make_shared<RCTImageResponseObserverProxy>(self);
auto &observerCoordinator = _state->getData().getImageRequest().getObserverCoordinator();
observerCoordinator.addObserver(_imageResponseObserverProxy);
}
Expand All @@ -140,8 +143,9 @@ - (void)prepareForRecycle

- (void)didReceiveImage:(UIImage *)image metadata:(id)metadata fromObserver:(const void *)observer
{
if (!_eventEmitter || !_state) {
// Notifications are delivered asynchronously and might arrive after the view is already recycled.
if (!_eventEmitter || !_state || observer != _imageResponseObserverProxy.get()) {
// Notifications are delivered asynchronously and might arrive after the view is already recycled,
// or after it has been reused for another image.
// In the future, we should incorporate an `EventEmitter` into a separate object owned by `ImageRequest` or `State`.
// See for more info: T46311063.
return;
Expand Down Expand Up @@ -187,7 +191,7 @@ - (void)didReceiveProgress:(float)progress
total:(int64_t)total
fromObserver:(const void *)observer
{
if (!_eventEmitter) {
if (!_eventEmitter || observer != _imageResponseObserverProxy.get()) {
return;
}

Expand All @@ -196,6 +200,10 @@ - (void)didReceiveProgress:(float)progress

- (void)didReceiveFailure:(NSError *)error fromObserver:(const void *)observer
{
if (observer != _imageResponseObserverProxy.get()) {
return;
}

_imageView.image = nil;

if (!_eventEmitter) {
Expand Down
Loading