Skip to content
Closed
Show file tree
Hide file tree
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 numberDiff line numberDiff line change
Expand Up@@ -10,9 +10,39 @@
#include <glog/logging.h>
#include <react/common/mapbuffer/JReadableMapBuffer.h>
#include <react/renderer/imagemanager/conversions.h>
#include <react/renderer/uimanager/UIManagerCommitHook.h>

namespace facebook::react {

class ImageFetcherCommitHook : public UIManagerCommitHook {
public:
explicit ImageFetcherCommitHook(ImageFetcher* fetcher) : fetcher_(fetcher) {}

RootShadowNode::Unshared shadowTreeWillCommit(
const ShadowTree& /*shadowTree*/,
const RootShadowNode::Shared& /*oldRootShadowNode*/,
const RootShadowNode::Unshared& newRootShadowNode,
const ShadowTree::CommitOptions& /*commitOptions*/) noexcept override {
if (fetcher_ != nullptr) {
fetcher_->flushImageRequests();
}
return newRootShadowNode;
}

void commitHookWasRegistered(
const UIManager& /*uiManager*/) noexcept override {}

void commitHookWasUnregistered(
const UIManager& /*uiManager*/) noexcept override {}

void invalidate() {
fetcher_ = nullptr;
}

private:
ImageFetcher* fetcher_;
};

ImageFetcher::ImageFetcher(
std::shared_ptr<const ContextContainer> contextContainer)
: contextContainer_(std::move(contextContainer)) {
Expand All@@ -22,20 +52,24 @@ ImageFetcher::ImageFetcher(
->find<std::shared_ptr<UIManagerCommitHookManager>>(
std::string(UIManagerCommitHookManagerKey));
uiManagerCommitHookManager.has_value()) {
(*uiManagerCommitHookManager)->registerCommitHook(*this);
commitHook_ = std::make_unique<ImageFetcherCommitHook>(this);
(*uiManagerCommitHookManager)->registerCommitHook(*commitHook_);
}
}
}

ImageFetcher::~ImageFetcher() {
if (ReactNativeFeatureFlags::enableImagePrefetchingJNIBatchingAndroid()) {
if (ReactNativeFeatureFlags::enableImagePrefetchingJNIBatchingAndroid() &&
commitHook_ != nullptr) {
commitHook_->invalidate();
if (auto uiManagerCommitHookManager =
contextContainer_
->find<std::shared_ptr<UIManagerCommitHookManager>>(
std::string(UIManagerCommitHookManagerKey));
uiManagerCommitHookManager.has_value()) {
(*uiManagerCommitHookManager)->unregisterCommitHook(*this);
(*uiManagerCommitHookManager)->unregisterCommitHook(*commitHook_);
}
commitHook_ = nullptr;
}
}

Expand All@@ -58,15 +92,6 @@ ImageRequest ImageFetcher::requestImage(
return {imageSource, telemetry};
}

RootShadowNode::Unshared ImageFetcher::shadowTreeWillCommit(
const ShadowTree& /*shadowTree*/,
const RootShadowNode::Shared& /*oldRootShadowNode*/,
const RootShadowNode::Unshared& newRootShadowNode,
const ShadowTree::CommitOptions& /*commitOptions*/) noexcept {
flushImageRequests();
return newRootShadowNode;
}

void ImageFetcher::flushImageRequests() {
if (items_.empty()) {
return;
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,17 +11,19 @@
#include <react/renderer/imagemanager/ImageRequest.h>
#include <react/renderer/imagemanager/ImageRequestParams.h>
#include <react/renderer/mounting/ShadowTree.h>
#include <react/renderer/uimanager/UIManagerCommitHook.h>
#include <react/utils/ContextContainer.h>
#include <memory>
#include <unordered_map>
#include <vector>

namespace facebook::react {

class ImageFetcher : public UIManagerCommitHook {
class ImageFetcherCommitHook;

class ImageFetcher {
public:
ImageFetcher(std::shared_ptr<const ContextContainer> contextContainer);
~ImageFetcher() override;
~ImageFetcher();
ImageFetcher(const ImageFetcher&) = delete;
ImageFetcher& operator=(const ImageFetcher&) = delete;
ImageFetcher(ImageFetcher&&) = delete;
Expand All@@ -33,21 +35,12 @@ class ImageFetcher : public UIManagerCommitHook {
const ImageRequestParams& imageRequestParams,
Tag tag);

void commitHookWasRegistered(const UIManager& uiManager) noexcept override {}

void commitHookWasUnregistered(const UIManager& uiManager) noexcept override {
}

RootShadowNode::Unshared shadowTreeWillCommit(
const ShadowTree& shadowTree,
const RootShadowNode::Shared& oldRootShadowNode,
const RootShadowNode::Unshared& newRootShadowNode,
const ShadowTree::CommitOptions& commitOptions) noexcept override;

private:
friend class ImageFetcherCommitHook;
void flushImageRequests();

std::unordered_map<SurfaceId, std::vector<ImageRequestItem>> items_;
std::shared_ptr<const ContextContainer> contextContainer_;
std::unique_ptr<ImageFetcherCommitHook> commitHook_;
};
} // namespace facebook::react
Loading