diff --git a/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp b/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp index 4c808002b7d..2e1367be594 100644 --- a/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp +++ b/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp @@ -453,17 +453,17 @@ jsi::Value UIManagerBinding::get( auto shadowNodeList = shadowNodeListFromValue(runtime, arguments[1]); static std::atomic_uint_fast8_t completeRootEventCounter{0}; + static std::atomic_uint_fast32_t mostRecentSurfaceId{0}; completeRootEventCounter += 1; + mostRecentSurfaceId = surfaceId; sharedUIManager->backgroundExecutor_( - [sharedUIManager, - surfaceId, - shadowNodeList, - eventCount = completeRootEventCounter.load()] { - auto shouldCancel = [eventCount]() -> bool { + [=, eventCount = completeRootEventCounter.load()] { + auto shouldCancel = [=]() -> bool { // If `completeRootEventCounter` was incremented, another // `completeSurface` call has been scheduled and current // `completeSurface` should be cancelled. - return completeRootEventCounter > eventCount; + return completeRootEventCounter > eventCount && + mostRecentSurfaceId == surfaceId; }; sharedUIManager->completeSurface( surfaceId, shadowNodeList, {true, shouldCancel});