From e80a9f828db1472506d4dce330515a391cbe57fc Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Wed, 31 May 2023 14:20:08 +0200 Subject: [PATCH 1/3] Inject `_WORKLET` to RN runtime --- android/src/main/cpp/NativeProxy.cpp | 2 ++ ios/REAModule.mm | 2 ++ ios/native/REAInitializer.mm | 2 ++ 3 files changed, 6 insertions(+) diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index 96c80221880a..1386d81187e2 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -474,6 +474,8 @@ void NativeProxy::setGlobalProperties( jsRuntime.global().setProperty( jsRuntime, "_WORKLET_RUNTIME", workletRuntimeValue); + jsRuntime.global().setProperty(jsRuntime, "_WORKLET", false); + #ifdef RCT_NEW_ARCH_ENABLED jsRuntime.global().setProperty(jsRuntime, "_IS_FABRIC", true); #else diff --git a/ios/REAModule.mm b/ios/REAModule.mm index 3cb75a0b8cf0..43fd35603ac6 100644 --- a/ios/REAModule.mm +++ b/ios/REAModule.mm @@ -215,6 +215,8 @@ - (void)setBridge:(RCTBridge *)bridge runtime.global().setProperty(runtime, "_WORKLET_RUNTIME", workletRuntimeValue); + runtime.global().setProperty(runtime, "_WORKLET", false); + runtime.global().setProperty(runtime, "_IS_FABRIC", true); auto version = getReanimatedVersionString(runtime); diff --git a/ios/native/REAInitializer.mm b/ios/native/REAInitializer.mm index 60ee16df3da1..a0ea056dd0f9 100644 --- a/ios/native/REAInitializer.mm +++ b/ios/native/REAInitializer.mm @@ -57,6 +57,8 @@ - (void)setBridge:(RCTBridge *)bridge; runtime.global().setProperty(runtime, "_WORKLET_RUNTIME", workletRuntimeValue); + runtime.global().setProperty(runtime, "_WORKLET", false); + runtime.global().setProperty(runtime, "_IS_FABRIC", false); auto version = getReanimatedVersionString(runtime); From cc8531c58ae90bb7ab99622bfe449767b58309f6 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Mon, 5 Jun 2023 15:49:52 +0200 Subject: [PATCH 2/3] Improve Babel plugin check Co-authored-by: Tymoteusz Boba --- src/reanimated2/threads.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/reanimated2/threads.ts b/src/reanimated2/threads.ts index bf4e88b39231..ee3428009808 100644 --- a/src/reanimated2/threads.ts +++ b/src/reanimated2/threads.ts @@ -141,11 +141,12 @@ export function runOnUIImmediately( }; } -if (__DEV__) { - try { - // eslint-disable-next-line @typescript-eslint/no-empty-function - runOnUI(() => {}); - } catch (e) { +if (__DEV__ && !IS_WEB) { + const f = () => { + 'worklet'; + }; + // @ts-ignore plugin + if (f.__workletHash === undefined) { throw new Error( 'Failed to create a worklet. Did you forget to add Reanimated Babel plugin in babel.config.js? See installation docs at https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation#babel-plugin.' ); From 7947dda83ba2081dd46387f71a6718d7bf00d25f Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Mon, 5 Jun 2023 15:51:58 +0200 Subject: [PATCH 3/3] Unify logic --- src/reanimated2/threads.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reanimated2/threads.ts b/src/reanimated2/threads.ts index ee3428009808..b81d4a0583b6 100644 --- a/src/reanimated2/threads.ts +++ b/src/reanimated2/threads.ts @@ -123,7 +123,7 @@ export function runOnUIImmediately( worklet: ComplexWorkletFunction ): (...args: A) => void { 'worklet'; - if (__DEV__ && !IS_JEST && _WORKLET) { + if (__DEV__ && !IS_WEB && _WORKLET) { throw new Error( 'runOnUIImmediately() cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.' );