From 3ff66087e833d551d47197714ca41ada4eafb113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Sat, 5 Oct 2024 15:35:28 +0200 Subject: [PATCH] feat: Move shareable creation to worklets --- .../NativeModules/NativeReanimatedModule.cpp | 9 --------- .../NativeModules/NativeReanimatedModule.h | 6 ------ .../NativeModules/NativeReanimatedModuleSpec.cpp | 15 --------------- .../NativeModules/NativeReanimatedModuleSpec.h | 7 ------- .../NativeModules/NativeWorkletsModule.cpp | 11 +++++++++++ .../NativeModules/NativeWorkletsModule.h | 6 ++++++ .../NativeModules/NativeWorkletsModuleSpec.cpp | 16 +++++++++++++++- .../NativeModules/NativeWorkletsModuleSpec.h | 7 +++++++ .../src/ReanimatedModule/NativeReanimated.ts | 13 +------------ .../js-reanimated/JSReanimated.ts | 6 ------ .../ReanimatedModule/reanimatedModuleProxy.ts | 5 ----- .../react-native-reanimated/src/commonTypes.ts | 14 +++++++------- .../react-native-reanimated/src/shareables.ts | 6 +++--- .../src/worklets/WorkletsModule/JSWorklets.ts | 11 +++++++++-- .../worklets/WorkletsModule/NativeWorklets.ts | 12 ++++++++++++ .../WorkletsModule/workletsModuleProxy.ts | 10 +++++++++- 16 files changed, 80 insertions(+), 74 deletions(-) diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp index 42892ccfb25..835f92d3718 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp @@ -254,15 +254,6 @@ jsi::Value NativeReanimatedModule::scheduleOnRuntime( return jsi::Value::undefined(); } -jsi::Value NativeReanimatedModule::makeShareableClone( - jsi::Runtime &rt, - const jsi::Value &value, - const jsi::Value &shouldRetainRemote, - const jsi::Value &nativeStateSource) { - return reanimated::makeShareableClone( - rt, value, shouldRetainRemote, nativeStateSource); -} - jsi::Value NativeReanimatedModule::registerEventHandler( jsi::Runtime &rt, const jsi::Value &worklet, diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h index 12ad4293ea2..bb65b875662 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h @@ -46,12 +46,6 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec { ~NativeReanimatedModule(); - jsi::Value makeShareableClone( - jsi::Runtime &rt, - const jsi::Value &value, - const jsi::Value &shouldRetainRemote, - const jsi::Value &nativeStateSource) override; - void scheduleOnUI(jsi::Runtime &rt, const jsi::Value &worklet) override; jsi::Value executeOnUIRuntimeSync(jsi::Runtime &rt, const jsi::Value &worklet) override; diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.cpp index 1a48dc46ad0..7bf3a0e0494 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.cpp @@ -6,18 +6,6 @@ namespace reanimated { -// SharedValue - -static jsi::Value SPEC_PREFIX(makeShareableClone)( - jsi::Runtime &rt, - TurboModule &turboModule, - const jsi::Value *args, - size_t) { - return static_cast(&turboModule) - ->makeShareableClone( - rt, std::move(args[0]), std::move(args[1]), std::move(args[2])); -} - // scheduler static jsi::Value SPEC_PREFIX(scheduleOnUI)( @@ -174,9 +162,6 @@ static jsi::Value SPEC_PREFIX(setShouldAnimateExiting)( NativeReanimatedModuleSpec::NativeReanimatedModuleSpec( const std::shared_ptr &jsInvoker) : TurboModule("NativeReanimated", jsInvoker) { - methodMap_["makeShareableClone"] = - MethodMetadata{2, SPEC_PREFIX(makeShareableClone)}; - methodMap_["scheduleOnUI"] = MethodMetadata{1, SPEC_PREFIX(scheduleOnUI)}; methodMap_["executeOnUIRuntimeSync"] = MethodMetadata{1, SPEC_PREFIX(executeOnUIRuntimeSync)}; diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.h b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.h index 870f06de1dd..04245e407a3 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/NativeReanimatedModuleSpec.h @@ -15,13 +15,6 @@ class JSI_EXPORT NativeReanimatedModuleSpec : public TurboModule { const std::shared_ptr &jsInvoker); public: - // SharedValue - virtual jsi::Value makeShareableClone( - jsi::Runtime &rt, - const jsi::Value &value, - const jsi::Value &shouldRetainRemote, - const jsi::Value &nativeStateSource) = 0; - // Scheduling virtual void scheduleOnUI(jsi::Runtime &rt, const jsi::Value &worklet) = 0; virtual jsi::Value executeOnUIRuntimeSync( diff --git a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.cpp b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.cpp index 1a776bf8812..67d66bbf09d 100644 --- a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.cpp +++ b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.cpp @@ -6,6 +6,7 @@ #endif // RCT_NEW_ARCH_ENABLED #include +#include #ifdef __ANDROID__ #include @@ -22,4 +23,14 @@ NativeWorkletsModule::NativeWorkletsModule(const std::string &valueUnpackerCode) valueUnpackerCode_(valueUnpackerCode) {} NativeWorkletsModule::~NativeWorkletsModule() {} + +jsi::Value NativeWorkletsModule::makeShareableClone( + jsi::Runtime &rt, + const jsi::Value &value, + const jsi::Value &shouldRetainRemote, + const jsi::Value &nativeStateSource) { + return worklets::makeShareableClone( + rt, value, shouldRetainRemote, nativeStateSource); +} + } // namespace worklets diff --git a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.h b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.h index 15e8d7a271a..26787d13f06 100644 --- a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.h +++ b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModule.h @@ -12,6 +12,12 @@ class NativeWorkletsModule : public NativeWorkletsModuleSpec { ~NativeWorkletsModule(); + jsi::Value makeShareableClone( + jsi::Runtime &rt, + const jsi::Value &value, + const jsi::Value &shouldRetainRemote, + const jsi::Value &nativeStateSource) override; + [[nodiscard]] inline std::string getValueUnpackerCode() const { return valueUnpackerCode_; } diff --git a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.cpp b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.cpp index 4c305ecf8eb..a08111e1f1e 100644 --- a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.cpp +++ b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.cpp @@ -4,7 +4,21 @@ namespace worklets { +static jsi::Value SPEC_PREFIX(makeShareableClone)( + jsi::Runtime &rt, + TurboModule &turboModule, + const jsi::Value *args, + size_t) { + return static_cast(&turboModule) + ->makeShareableClone( + rt, std::move(args[0]), std::move(args[1]), std::move(args[2])); +} + NativeWorkletsModuleSpec::NativeWorkletsModuleSpec( const std::shared_ptr jsInvoker) - : TurboModule("NativeWorklets", jsInvoker) {} + : TurboModule("NativeWorklets", jsInvoker) { + methodMap_["makeShareableClone"] = + MethodMetadata{2, SPEC_PREFIX(makeShareableClone)}; +} + } // namespace worklets diff --git a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.h b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.h index 5b813ceaf0e..1c1d622e17c 100644 --- a/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.h +++ b/packages/react-native-reanimated/Common/cpp/worklets/NativeModules/NativeWorkletsModuleSpec.h @@ -13,6 +13,13 @@ class JSI_EXPORT NativeWorkletsModuleSpec : public TurboModule { protected: explicit NativeWorkletsModuleSpec( const std::shared_ptr jsInvoker); + + public: + virtual jsi::Value makeShareableClone( + jsi::Runtime &rt, + const jsi::Value &value, + const jsi::Value &shouldRetainRemote, + const jsi::Value &nativeStateSource) = 0; }; } // namespace worklets diff --git a/packages/react-native-reanimated/src/ReanimatedModule/NativeReanimated.ts b/packages/react-native-reanimated/src/ReanimatedModule/NativeReanimated.ts index 51e084c9d79..4f948c96f4d 100644 --- a/packages/react-native-reanimated/src/ReanimatedModule/NativeReanimated.ts +++ b/packages/react-native-reanimated/src/ReanimatedModule/NativeReanimated.ts @@ -45,6 +45,7 @@ class NativeReanimatedModule implements IReanimatedModule { constructor() { this.#workletsModule = WorkletsModule; // These checks have to split since version checking depend on the execution order + this.#workletsModule = WorkletsModule; if (__DEV__) { assertSingleReanimatedInstance(); } @@ -64,18 +65,6 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti this.#reanimatedModuleProxy = global.__reanimatedModuleProxy; } - makeShareableClone( - value: T, - shouldPersistRemote: boolean, - nativeStateSource?: object - ) { - return this.#reanimatedModuleProxy.makeShareableClone( - value, - shouldPersistRemote, - nativeStateSource - ); - } - scheduleOnUI(shareable: ShareableRef) { return this.#reanimatedModuleProxy.scheduleOnUI(shareable); } diff --git a/packages/react-native-reanimated/src/ReanimatedModule/js-reanimated/JSReanimated.ts b/packages/react-native-reanimated/src/ReanimatedModule/js-reanimated/JSReanimated.ts index 05c04c97e74..f54963b6171 100644 --- a/packages/react-native-reanimated/src/ReanimatedModule/js-reanimated/JSReanimated.ts +++ b/packages/react-native-reanimated/src/ReanimatedModule/js-reanimated/JSReanimated.ts @@ -42,12 +42,6 @@ class JSReanimated implements IReanimatedModule { sensors = new Map(); platform?: Platform = undefined; - makeShareableClone(): ShareableRef { - throw new ReanimatedError( - 'makeShareableClone should never be called in JSReanimated.' - ); - } - scheduleOnUI(worklet: ShareableRef) { // @ts-ignore web implementation has still not been updated after the rewrite, this will be addressed once the web implementation updates are ready requestAnimationFrameImpl(worklet); diff --git a/packages/react-native-reanimated/src/ReanimatedModule/reanimatedModuleProxy.ts b/packages/react-native-reanimated/src/ReanimatedModule/reanimatedModuleProxy.ts index 85bb9548777..a790564dba1 100644 --- a/packages/react-native-reanimated/src/ReanimatedModule/reanimatedModuleProxy.ts +++ b/packages/react-native-reanimated/src/ReanimatedModule/reanimatedModuleProxy.ts @@ -11,11 +11,6 @@ import type { WorkletRuntime } from '../runtimes'; /** Type of `__reanimatedModuleProxy` injected with JSI. */ export interface ReanimatedModuleProxy { - makeShareableClone( - value: T, - shouldPersistRemote: boolean, - nativeStateSource?: object - ): ShareableRef; scheduleOnUI(shareable: ShareableRef): void; executeOnUIRuntimeSync(shareable: ShareableRef): R; createWorkletRuntime( diff --git a/packages/react-native-reanimated/src/commonTypes.ts b/packages/react-native-reanimated/src/commonTypes.ts index 1439a9d9b70..1473252a056 100644 --- a/packages/react-native-reanimated/src/commonTypes.ts +++ b/packages/react-native-reanimated/src/commonTypes.ts @@ -7,7 +7,13 @@ import type { } from 'react-native'; import type { WorkletRuntime } from './runtimes'; -export interface IWorkletsModule {} +export interface IWorkletsModule { + makeShareableClone( + value: TValue, + shouldPersistRemote: boolean, + nativeStateSource?: object + ): ShareableRef; +} export interface IReanimatedModule { registerSensor( @@ -49,12 +55,6 @@ export interface IReanimatedModule { unsubscribeFromKeyboardEvents(listenerId: number): void; - makeShareableClone( - value: TValue, - shouldPersistRemote: boolean, - nativeStateSource?: object - ): ShareableRef; - scheduleOnUI(shareable: ShareableRef): void; executeOnUIRuntimeSync( diff --git a/packages/react-native-reanimated/src/shareables.ts b/packages/react-native-reanimated/src/shareables.ts index 0565272e30c..23070fe7de9 100644 --- a/packages/react-native-reanimated/src/shareables.ts +++ b/packages/react-native-reanimated/src/shareables.ts @@ -13,7 +13,7 @@ import { shareableMappingFlag, } from './shareableMappingCache'; import { logger } from './logger'; -import { ReanimatedModule } from './ReanimatedModule'; +import { WorkletsModule } from './worklets'; // for web/chrome debugger/jest environments this file provides a stub implementation // where no shareable references are used. Instead, the objects themselves are used @@ -273,7 +273,7 @@ Offending code was: \`${getWorkletCode(value)}\``); shareableMappingCache.set(value, inaccessibleObject); return inaccessibleObject; } - const adapted = ReanimatedModule.makeShareableClone( + const adapted = WorkletsModule.makeShareableClone( toAdapt, shouldPersistRemote, value @@ -283,7 +283,7 @@ Offending code was: \`${getWorkletCode(value)}\``); return adapted; } } - return ReanimatedModule.makeShareableClone( + return WorkletsModule.makeShareableClone( value, shouldPersistRemote, undefined diff --git a/packages/react-native-reanimated/src/worklets/WorkletsModule/JSWorklets.ts b/packages/react-native-reanimated/src/worklets/WorkletsModule/JSWorklets.ts index 95b0978d214..c280e8abfa0 100644 --- a/packages/react-native-reanimated/src/worklets/WorkletsModule/JSWorklets.ts +++ b/packages/react-native-reanimated/src/worklets/WorkletsModule/JSWorklets.ts @@ -1,9 +1,16 @@ 'use strict'; -import type { IWorkletsModule } from '../../commonTypes'; +import type { IWorkletsModule, ShareableRef } from '../../commonTypes'; +import { ReanimatedError } from '../../errors'; export function createJSWorkletsModule(): IWorkletsModule { return new JSWorklets(); } -class JSWorklets implements IWorkletsModule {} +class JSWorklets implements IWorkletsModule { + makeShareableClone(): ShareableRef { + throw new ReanimatedError( + 'makeShareableClone should never be called in JSReanimated.' + ); + } +} diff --git a/packages/react-native-reanimated/src/worklets/WorkletsModule/NativeWorklets.ts b/packages/react-native-reanimated/src/worklets/WorkletsModule/NativeWorklets.ts index 2dbd69df4ab..e305d6f164d 100644 --- a/packages/react-native-reanimated/src/worklets/WorkletsModule/NativeWorklets.ts +++ b/packages/react-native-reanimated/src/worklets/WorkletsModule/NativeWorklets.ts @@ -25,4 +25,16 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti } this.#workletsModuleProxy = global.__workletsModuleProxy; } + + makeShareableClone( + value: T, + shouldPersistRemote: boolean, + nativeStateSource?: object + ) { + return this.#workletsModuleProxy.makeShareableClone( + value, + shouldPersistRemote, + nativeStateSource + ); + } } diff --git a/packages/react-native-reanimated/src/worklets/WorkletsModule/workletsModuleProxy.ts b/packages/react-native-reanimated/src/worklets/WorkletsModule/workletsModuleProxy.ts index 00ae1391e31..444c5ed7df3 100644 --- a/packages/react-native-reanimated/src/worklets/WorkletsModule/workletsModuleProxy.ts +++ b/packages/react-native-reanimated/src/worklets/WorkletsModule/workletsModuleProxy.ts @@ -1,4 +1,12 @@ 'use strict'; +import type { ShareableRef } from '../../commonTypes'; + /** Type of `__workletsModuleProxy` injected with JSI. */ -export interface WorkletsModuleProxy {} +export interface WorkletsModuleProxy { + makeShareableClone( + value: T, + shouldPersistRemote: boolean, + nativeStateSource?: object + ): ShareableRef; +}