mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
3bc09892c0
Summary: `addObserver` and `removeObserver` now accepts const references instead of pointers which indicates the intent (non-nullability and non-owning) clearly. The delegate methods are also marked as `const` to designate the possible concurrent execution (`const` means "thread-safe" here). All changes are pure syntactical, nothing really changes (besides the fact overall code quality and redability). Reviewed By: JoshuaGross Differential Revision: D17535395 fbshipit-source-id: b0c6c872d44fee22e38fd067ccd3320e7231c94a
90 lines
2.3 KiB
C++
90 lines
2.3 KiB
C++
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "ImageResponseObserverCoordinator.h"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
void ImageResponseObserverCoordinator::addObserver(
|
|
ImageResponseObserver const &observer) const {
|
|
mutex_.lock();
|
|
switch (status_) {
|
|
case ImageResponse::Status::Loading: {
|
|
observers_.push_back(&observer);
|
|
mutex_.unlock();
|
|
break;
|
|
}
|
|
case ImageResponse::Status::Completed: {
|
|
auto imageData = imageData_;
|
|
mutex_.unlock();
|
|
observer.didReceiveImage(ImageResponse{imageData});
|
|
break;
|
|
}
|
|
case ImageResponse::Status::Failed: {
|
|
mutex_.unlock();
|
|
observer.didReceiveFailure();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ImageResponseObserverCoordinator::removeObserver(
|
|
ImageResponseObserver const &observer) const {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
// We remove only one element to maintain a balance between add/remove calls.
|
|
auto position = std::find(observers_.begin(), observers_.end(), &observer);
|
|
if (position != observers_.end()) {
|
|
observers_.erase(position, observers_.end());
|
|
}
|
|
}
|
|
|
|
void ImageResponseObserverCoordinator::nativeImageResponseProgress(
|
|
float progress) const {
|
|
mutex_.lock();
|
|
auto observers = observers_;
|
|
assert(status_ == ImageResponse::Status::Loading);
|
|
mutex_.unlock();
|
|
|
|
for (auto observer : observers) {
|
|
observer->didReceiveProgress(progress);
|
|
}
|
|
}
|
|
|
|
void ImageResponseObserverCoordinator::nativeImageResponseComplete(
|
|
ImageResponse const &imageResponse) const {
|
|
mutex_.lock();
|
|
imageData_ = imageResponse.getImage();
|
|
assert(status_ == ImageResponse::Status::Loading);
|
|
status_ = ImageResponse::Status::Completed;
|
|
auto observers = observers_;
|
|
mutex_.unlock();
|
|
|
|
for (auto observer : observers_) {
|
|
observer->didReceiveImage(imageResponse);
|
|
}
|
|
}
|
|
|
|
void ImageResponseObserverCoordinator::nativeImageResponseFailed() const {
|
|
mutex_.lock();
|
|
assert(status_ == ImageResponse::Status::Loading);
|
|
status_ = ImageResponse::Status::Failed;
|
|
auto observers = observers_;
|
|
mutex_.unlock();
|
|
|
|
for (auto observer : observers) {
|
|
observer->didReceiveFailure();
|
|
}
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|