Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: enforce ReanimatedModule load before first isFabric call #6836

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ class ReanimatedModuleProxy : public ReanimatedModuleProxySpec {
return workletsModuleProxy_;
}

[[nodiscard]] inline jsi::Value isFabric() override {
#ifdef RCT_NEW_ARCH_ENABLED
return jsi::Value(true);
#else
return jsi::Value(false);
#endif // RCT_NEW_ARCH_ENABLED
}

private:
void commonInit(const PlatformDepMethodsHolder &platformDepMethodsHolder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ static jsi::Value REANIMATED_SPEC_PREFIX(setShouldAnimateExiting)(
return jsi::Value::undefined();
}

static jsi::Value REANIMATED_SPEC_PREFIX(isFabric)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t) {
return static_cast<ReanimatedModuleProxySpec *>(&turboModule)->isFabric();
}

ReanimatedModuleProxySpec::ReanimatedModuleProxySpec(
const std::shared_ptr<CallInvoker> &jsInvoker)
: TurboModule("NativeReanimated", jsInvoker) {
Expand Down Expand Up @@ -148,5 +156,7 @@ ReanimatedModuleProxySpec::ReanimatedModuleProxySpec(
MethodMetadata{1, REANIMATED_SPEC_PREFIX(configureLayoutAnimationBatch)};
methodMap_["setShouldAnimateExitingForTag"] =
MethodMetadata{2, REANIMATED_SPEC_PREFIX(setShouldAnimateExiting)};

methodMap_["isFabric"] = MethodMetadata{0, REANIMATED_SPEC_PREFIX(isFabric)};
}
} // namespace reanimated
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class JSI_EXPORT ReanimatedModuleProxySpec : public TurboModule {
jsi::Runtime &rt,
const jsi::Value &viewTag,
const jsi::Value &shouldAnimate) = 0;

virtual jsi::Value isFabric() = 0;
};

} // namespace reanimated
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ void RNRuntimeDecorator::decorate(
rnRuntime.global().setProperty(
rnRuntime, "_WORKLET_RUNTIME", workletRuntimeValue);

#ifdef RCT_NEW_ARCH_ENABLED
constexpr auto isFabric = true;
#else
constexpr auto isFabric = false;
#endif // RCT_NEW_ARCH_ENABLED
rnRuntime.global().setProperty(rnRuntime, "_IS_FABRIC", isFabric);

rnRuntime.global().setProperty(
rnRuntime, "_IS_BRIDGELESS", reanimatedModuleProxy->isBridgeless());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ void WorkletRuntimeDecorator::decorate(

rt.global().setProperty(rt, "_LABEL", jsi::String::createFromAscii(rt, name));

#ifdef RCT_NEW_ARCH_ENABLED
constexpr auto isFabric = true;
#else
constexpr auto isFabric = false;
#endif // RCT_NEW_ARCH_ENABLED
rt.global().setProperty(rt, "_IS_FABRIC", isFabric);

#ifndef NDEBUG
auto evalWithSourceUrl = [](jsi::Runtime &rt,
const jsi::Value &thisValue,
Expand Down
15 changes: 12 additions & 3 deletions packages/react-native-reanimated/src/PlatformChecker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
import { Platform } from 'react-native';
import { ReanimatedModule } from './ReanimatedModule';

// This type is necessary since some libraries tend to do a lib check
// and this file causes type errors on `global` access.
Expand Down Expand Up @@ -34,9 +35,17 @@ export function shouldBeUseWeb() {
return isJest() || isChromeDebugger() || isWeb() || isWindows();
}

export function isFabric() {
return !!(global as localGlobal)._IS_FABRIC;
}
export let isFabric = () => {
if (ReanimatedModule.isFabric()) {
isFabric = () => true;
global._IS_FABRIC = true;
return true;
} else {
isFabric = () => false;
global._IS_FABRIC = false;
return false;
}
};

export function isWindowAvailable() {
// the window object is unavailable when building the server portion of a site that uses SSG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,8 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
unsubscribeFromKeyboardEvents(listenerId: number) {
this.#reanimatedModuleProxy.unsubscribeFromKeyboardEvents(listenerId);
}

isFabric() {
return this.#reanimatedModuleProxy.isFabric();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ class JSReanimated implements IReanimatedModule {
'configureProps is not available in JSReanimated.'
);
}

isFabric() {
return false;
}
}

// Lack of this export breaks TypeScript generation since
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ export interface ReanimatedModuleProxy {
): void;

setShouldAnimateExitingForTag(viewTag: number, shouldAnimate: boolean): void;

isFabric(): boolean;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
import { NativeEventEmitter, Platform } from 'react-native';
import type { NativeModule } from 'react-native';
import { shouldBeUseWeb } from '../PlatformChecker';
import { isFabric, shouldBeUseWeb } from '../PlatformChecker';
import type { StyleProps } from '../commonTypes';
import { runOnJS, runOnUIImmediately } from '../threads';
import type {
Expand Down Expand Up @@ -146,7 +146,7 @@ type JSPropsUpdaterOptions =
let JSPropsUpdater: JSPropsUpdaterOptions;
if (SHOULD_BE_USE_WEB) {
JSPropsUpdater = JSPropsUpdaterWeb;
} else if (global._IS_FABRIC) {
} else if (isFabric()) {
JSPropsUpdater = JSPropsUpdaterFabric;
} else {
JSPropsUpdater = JSPropsUpdaterPaper;
Expand Down
Loading