From 765e542c8d25401e1b8818ff3caec2796efb51b1 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Thu, 22 Feb 2024 00:58:29 -0800 Subject: [PATCH] react-native | Allow invoking a sync callback under AsyncCallback via 'unsafeCallSync'. (#43143) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43143 AsyncCallback allows storing SyncCallback and invoking it from any thread. However, there are cases where if you have a mix of sync and async callbacks - you might want to invoke them together in one go, instead of spreading them out across thread invocations. For those cases - allow invoking any AsyncCallback as a sync one, prefixing it with "unsafe", because it's inherently not a safe operation to perform. Changelog: [General][Changed] - Allow invoking the AsyncCallback synchronously to allow for tight performance optimization. Reviewed By: s-rws Differential Revision: D54028850 fbshipit-source-id: f6729819f791f1d58d2ca655d4082547f18bdd2d --- .../react-native/ReactCommon/react/bridging/Function.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/react-native/ReactCommon/react/bridging/Function.h b/packages/react-native/ReactCommon/react/bridging/Function.h index 58cb238ac08..7fae7fc8575 100644 --- a/packages/react-native/ReactCommon/react/bridging/Function.h +++ b/packages/react-native/ReactCommon/react/bridging/Function.h @@ -54,6 +54,15 @@ class AsyncCallback { callWithFunction(priority, std::move(callImpl)); } + /// Invoke the function write-away as if it was a synchronous function + /// without any synchronization or delegating to JS context. + /// @note Caller is responsible for calling this from within JS context. + void unsafeCallSync(Args... args) const noexcept { + if (callback_) { + (*callback_)(std::forward(args)...); + } + } + private: friend Bridging;