Skip to content

Commit

Permalink
Inject _WORKLET to RN runtime (#4511)
Browse files Browse the repository at this point in the history
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

Fixes #4506 and fixes #4507.

## Test plan

<!-- Provide a minimal but complete code snippet that can be used to
test out this change along with instructions how to run it and a
description of the expected behavior. -->

---------

Co-authored-by: Tymoteusz Boba <[email protected]>
  • Loading branch information
tomekzaw and tboba committed Jun 6, 2023
1 parent 7397188 commit a27e45f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions android/src/main/cpp/NativeProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ios/REAModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions ios/native/REAInitializer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions src/reanimated2/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function runOnUIImmediately<A extends any[], R>(
worklet: ComplexWorkletFunction<A, R>
): (...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.'
);
Expand All @@ -141,11 +141,12 @@ export function runOnUIImmediately<A extends any[], R>(
};
}

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.'
);
Expand Down

0 comments on commit a27e45f

Please sign in to comment.