Skip to content
Open
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 number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ void ResizeObserverManager::connect(
}

notifyResizeObserversFunction_ = std::move(notifyResizeObserversFunction);
runtimeScheduler_ = &runtimeScheduler;
pendingWake_ = std::make_shared<std::atomic<bool>>(false);
shadowTreeRegistry_ = &uiManager.getShadowTreeRegistry();

runtimeScheduler.setResizeObserverDelegate(this);
uiManager.registerCommitHook(*this);
shadowTreeRegistry_ = &uiManager.getShadowTreeRegistry();
commitHookRegistered_ = true;
}

Expand All @@ -145,7 +147,11 @@ void ResizeObserverManager::disconnect(
}

runtimeScheduler.setResizeObserverDelegate(nullptr);
// UIManager holds its hook mutex during dispatch; unregister takes it
// exclusively and waits for in-flight calls before these members are reset.
uiManager.unregisterCommitHook(*this);
runtimeScheduler_ = nullptr;
pendingWake_.reset();
shadowTreeRegistry_ = nullptr;

// May run from inside `broadcastActiveResizeObservations` (a callback can
Expand Down Expand Up @@ -195,9 +201,8 @@ void ResizeObserverManager::shadowTreeDidCommit(
affectedLayoutableNodes) noexcept {
TraceSection s{"ResizeObserverManager::shadowTreeDidCommit"};

// Runs on the commit hook (any thread), so it only collects which observed
// targets went dirty; it must not compute observations or notify JS.
// `runResizeObservations` (JS thread) does that against the latest tree.
// The commit hook only collects dirty families. Sizes are computed by the
// resize-observation rendering step on the JS thread.
auto surfaceId = shadowTree.getSurfaceId();

std::unordered_set<const ShadowNodeFamily*> observedFamilies;
Expand All @@ -223,14 +228,24 @@ void ResizeObserverManager::shadowTreeDidCommit(
}
}

std::unique_lock lock(dirtyFamiliesMutex_);
// Record the commit even with no dirty family, so `runResizeObservations`
// re-checks for removed targets (they aren't in `affectedLayoutableNodes`).
committedSurfaceIds_.insert(surfaceId);
if (!newlyDirtyFamilies.empty()) {
{
std::unique_lock lock(dirtyFamiliesMutex_);
// Record the commit even with no dirty family, so `runResizeObservations`
// re-checks for removed targets (they aren't in `affectedLayoutableNodes`).
committedSurfaceIds_.insert(surfaceId);
if (newlyDirtyFamilies.empty()) {
return;
}

auto& dirtyFamilies = dirtyFamiliesBySurfaceId_[surfaceId];
dirtyFamilies.insert(newlyDirtyFamilies.begin(), newlyDirtyFamilies.end());
}

auto pendingWake = pendingWake_;
if (!pendingWake->exchange(true)) {
runtimeScheduler_->scheduleWork(
[pendingWake](jsi::Runtime&) { pendingWake->store(false); });
}
}

#pragma mark - RuntimeSchedulerResizeObserverDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <react/renderer/runtimescheduler/RuntimeSchedulerResizeObserverDelegate.h>
#include <react/renderer/uimanager/UIManager.h>
#include <react/renderer/uimanager/UIManagerCommitHook.h>
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -112,6 +113,8 @@ class ResizeObserverManager final : public UIManagerCommitHook, public RuntimeSc
std::mutex dirtyFamiliesMutex_;

std::function<void(jsi::Runtime &, bool)> notifyResizeObserversFunction_;
RuntimeScheduler *runtimeScheduler_{nullptr};
std::shared_ptr<std::atomic<bool>> pendingWake_;
bool commitHookRegistered_{};

// Set for the duration of `runResizeObservations`. The broadcast runs JS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ interface Spec extends TurboModule {
viewportOffsetY?: number,
) => RootTag;
stopSurface: (surfaceId: RootTag) => void;
updateSurfaceConstraints: (
surfaceId: RootTag,
viewportWidth: number,
viewportHeight: number,
devicePixelRatio: number,
) => void;
enqueueNativeEvent: (
shadowNode: unknown /* ShadowNode */,
type: string,
Expand Down
Loading
Loading