From cf8aa1d961cf8e9fd1a49d5259badbd796b2edec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Thu, 16 Jan 2025 19:05:00 +0100 Subject: [PATCH] fix: setting up this referencing callbacks --- .../FabricExample.xcodeproj/project.pbxproj | 12 ++++++--- apps/fabric-example/ios/Podfile.lock | 2 +- apps/macos-example/macos/Podfile.lock | 2 +- apps/paper-example/ios/Podfile.lock | 2 +- .../project.pbxproj | 12 ++++++--- apps/tvos-example/ios/Podfile.lock | 2 +- .../NativeModules/ReanimatedModuleProxy.cpp | 25 ++++++++++--------- .../NativeModules/ReanimatedModuleProxy.h | 9 ++++--- .../cpp/reanimated/android/NativeProxy.cpp | 1 + .../reanimated/apple/native/NativeProxy.mm | 1 + 10 files changed, 43 insertions(+), 25 deletions(-) diff --git a/apps/fabric-example/ios/FabricExample.xcodeproj/project.pbxproj b/apps/fabric-example/ios/FabricExample.xcodeproj/project.pbxproj index 92b63505069..f4f88fbb0d0 100644 --- a/apps/fabric-example/ios/FabricExample.xcodeproj/project.pbxproj +++ b/apps/fabric-example/ios/FabricExample.xcodeproj/project.pbxproj @@ -321,7 +321,7 @@ }; 1232BDDFC9DE5BCE2776187D /* [CP-User] Generate metadata for clangd */ = { isa = PBXShellScriptBuildPhase; - alwaysOutOfDate = 1; + alwaysOutOfDate = true; buildActionMask = 2147483647; files = ( ); @@ -573,7 +573,10 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = "$(inherited) "; + OTHER_LDFLAGS = ( + "$(inherited)", + " ", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../../../node_modules/react-native"; SDKROOT = iphoneos; SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG"; @@ -646,7 +649,10 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = "$(inherited) "; + OTHER_LDFLAGS = ( + "$(inherited)", + " ", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../../../node_modules/react-native"; SDKROOT = iphoneos; USE_HERMES = true; diff --git a/apps/fabric-example/ios/Podfile.lock b/apps/fabric-example/ios/Podfile.lock index 4c09f0df1bd..05164163610 100644 --- a/apps/fabric-example/ios/Podfile.lock +++ b/apps/fabric-example/ios/Podfile.lock @@ -2229,7 +2229,7 @@ SPEC CHECKSUMS: RNScreens: d0854539b51a53e38b61bcc9fb402439a9c73b26 RNSVG: 7e38044415125a1d108294377de261d2fe2c54c9 SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748 - Yoga: dd05f79b1943c96893294ee5040576aa6e8a8d82 + Yoga: 0c8754b0ea9edb13b6ce6b60f0f69eb5f164f16a PODFILE CHECKSUM: 4a9e0af2552a3fcd2303b56ad75e373f8bae65b9 diff --git a/apps/macos-example/macos/Podfile.lock b/apps/macos-example/macos/Podfile.lock index 8544ada63b3..6a84a8a4fad 100644 --- a/apps/macos-example/macos/Podfile.lock +++ b/apps/macos-example/macos/Podfile.lock @@ -1868,7 +1868,7 @@ SPEC CHECKSUMS: RNReanimated: ed490424d3b8b9f2acd104577c73b374fc79310b RNSVG: 669ed128ab9005090c612a0d627dbecb6ab5c76f SocketRocket: 9ee265c4b5ae2382d18e4ee1d2dd2d7af0ff1ab5 - Yoga: 209f62622a01344dbb9fa8d348610eaeb7df2cca + Yoga: 446e6f351a519539ff00a1159fe41e589aab1b94 PODFILE CHECKSUM: 8d50cc2acc9f6a6b1a12bd9106b86385ad72266f diff --git a/apps/paper-example/ios/Podfile.lock b/apps/paper-example/ios/Podfile.lock index 0ac3060060d..46c9898f204 100644 --- a/apps/paper-example/ios/Podfile.lock +++ b/apps/paper-example/ios/Podfile.lock @@ -2060,7 +2060,7 @@ SPEC CHECKSUMS: RNScreens: 81237378a6d7921fb2dbb6a6c6c45c3d8cc696bb RNSVG: 515a902fc18a375907eb4c3abec0b803fbfa37ef SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748 - Yoga: dd05f79b1943c96893294ee5040576aa6e8a8d82 + Yoga: 0c8754b0ea9edb13b6ce6b60f0f69eb5f164f16a PODFILE CHECKSUM: f6c84e0ec8eddea6d3ee15329987727bd1e6ff08 diff --git a/apps/paper-example/ios/ReanimatedExample.xcodeproj/project.pbxproj b/apps/paper-example/ios/ReanimatedExample.xcodeproj/project.pbxproj index b1d0c2b3b80..63b53647023 100644 --- a/apps/paper-example/ios/ReanimatedExample.xcodeproj/project.pbxproj +++ b/apps/paper-example/ios/ReanimatedExample.xcodeproj/project.pbxproj @@ -365,7 +365,7 @@ }; EC2A315BF2310B4CCE81E020 /* [CP-User] Generate metadata for clangd */ = { isa = PBXShellScriptBuildPhase; - alwaysOutOfDate = 1; + alwaysOutOfDate = true; buildActionMask = 2147483647; files = ( ); @@ -579,7 +579,10 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = "$(inherited) "; + OTHER_LDFLAGS = ( + "$(inherited)", + " ", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../../../node_modules/react-native"; SDKROOT = iphoneos; SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG"; @@ -652,7 +655,10 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = "$(inherited) "; + OTHER_LDFLAGS = ( + "$(inherited)", + " ", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../../../node_modules/react-native"; SDKROOT = iphoneos; USE_HERMES = true; diff --git a/apps/tvos-example/ios/Podfile.lock b/apps/tvos-example/ios/Podfile.lock index 04272ce1515..f6536db5edd 100644 --- a/apps/tvos-example/ios/Podfile.lock +++ b/apps/tvos-example/ios/Podfile.lock @@ -1888,7 +1888,7 @@ SPEC CHECKSUMS: ReactCommon: b927fd46115bd2acb146e24cf1a08f22abda8b3f RNReanimated: 3a5e1e235c940894097b0734aad9ebce45431ddd SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748 - Yoga: 2b0e5affb9ab46e4ebad33530df829c153c323d8 + Yoga: 651e5fd560c7e408ab9d9ca44b8de1b622d7f0cc PODFILE CHECKSUM: 79e1477a8eb76b717bdd7c1610f7f8e6772536a9 diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp index 6bb3fffeaf0..9adac17d3f8 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp @@ -53,16 +53,6 @@ ReanimatedModuleProxy::ReanimatedModuleProxy( valueUnpackerCode_(workletsModuleProxy->getValueUnpackerCode()), eventHandlerRegistry_(std::make_unique()), requestRender_(platformDepMethodsHolder.requestRender), - onRenderCallback_([weakReanimatedModuleProxy = - weak_from_this()](const double timestampMs) { - auto reanimatedModuleProxy = weakReanimatedModuleProxy.lock(); - if (!reanimatedModuleProxy) { - return; - } - - reanimatedModuleProxy->renderRequested_ = false; - reanimatedModuleProxy->onRender(timestampMs); - }), animatedSensorModule_(platformDepMethodsHolder), jsLogger_( std::make_shared(workletsModuleProxy->getJSScheduler())), @@ -82,11 +72,22 @@ ReanimatedModuleProxy::ReanimatedModuleProxy( platformDepMethodsHolder.subscribeForKeyboardEvents), unsubscribeFromKeyboardEventsFunction_( platformDepMethodsHolder.unsubscribeFromKeyboardEvents) { - commonInit(platformDepMethodsHolder); } -void ReanimatedModuleProxy::commonInit( +void ReanimatedModuleProxy::init( const PlatformDepMethodsHolder &platformDepMethodsHolder) { + auto onRenderCallback = [weakReanimatedModuleProxy = + weak_from_this()](const double timestampMs) { + auto reanimatedModuleProxy = weakReanimatedModuleProxy.lock(); + if (!reanimatedModuleProxy) { + return; + } + + reanimatedModuleProxy->renderRequested_ = false; + reanimatedModuleProxy->onRender(timestampMs); + }; + onRenderCallback_ = std::move(onRenderCallback); + auto requestAnimationFrame = [weakReanimatedModuleProxy = weak_from_this()]( jsi::Runtime &rt, const jsi::Value &callback) { diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h index 0fac7967828..75ebf6be681 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h @@ -42,6 +42,11 @@ class ReanimatedModuleProxy const bool isBridgeless, const bool isReducedMotion); + // We need this init method to initialize callbacks with + // weak_from_this() which is available only after the object + // is fully constructed. + void init(const PlatformDepMethodsHolder &platformDepMethodsHolder); + ~ReanimatedModuleProxy(); jsi::Value registerEventHandler( @@ -164,8 +169,6 @@ class ReanimatedModuleProxy } private: - void commonInit(const PlatformDepMethodsHolder &platformDepMethodsHolder); - void requestAnimationFrame(jsi::Runtime &rt, const jsi::Value &callback); #ifdef RCT_NEW_ARCH_ENABLED @@ -184,7 +187,7 @@ class ReanimatedModuleProxy const RequestRenderFunction requestRender_; std::vector> frameCallbacks_; volatile bool renderRequested_{false}; - const std::function onRenderCallback_; + std::function onRenderCallback_; AnimatedSensorModule animatedSensorModule_; const std::shared_ptr jsLogger_; std::shared_ptr layoutAnimationsManager_; diff --git a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp b/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp index 7424d7b655b..76a85dfb495 100644 --- a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp +++ b/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp @@ -50,6 +50,7 @@ NativeProxy::NativeProxy( isBridgeless, getIsReducedMotion())), layoutAnimations_(std::move(layoutAnimations)) { + reanimatedModuleProxy_->init(getPlatformDependentMethods()); #ifdef RCT_NEW_ARCH_ENABLED commonInit(fabricUIManager); #endif // RCT_NEW_ARCH_ENABLED diff --git a/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm b/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm index 1766a892b8d..b1bd603ad6b 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm +++ b/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm @@ -72,6 +72,7 @@ static inline bool getIsReducedMotion() auto reanimatedModuleProxy = std::make_shared( workletsModuleProxy, rnRuntime, jsInvoker, platformDepMethodsHolder, isBridgeless, getIsReducedMotion()); + reanimatedModuleProxy->init(platformDepMethodsHolder); commonInit(reaModule, workletsModuleProxy->getUIWorkletRuntime()->getJSIRuntime(), reanimatedModuleProxy); // Layout Animation callbacks setup