Skip to content

Commit

Permalink
refactor(Worklets): Move shareable creation to Worklets (#6576)
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

Moving `makeShareableClone` function from `NativeReanimatedModule` to
`NativeWorkletsModule`.

## Test plan

CI
  • Loading branch information
tjzel committed Dec 13, 2024
1 parent 3e39168 commit f54fb3c
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@

#include <utility>

#define SPEC_PREFIX(FN_NAME) __hostFunction_NativeReanimatedModuleSpec_##FN_NAME
#define REANIMATED_SPEC_PREFIX(FN_NAME) \
__hostFunction_NativeReanimatedModuleSpec_##FN_NAME

namespace reanimated {

// SharedValue

static jsi::Value SPEC_PREFIX(makeShareableClone)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t) {
return static_cast<NativeReanimatedModuleSpec *>(&turboModule)
->makeShareableClone(
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
}

// scheduler

static jsi::Value SPEC_PREFIX(scheduleOnUI)(
static jsi::Value REANIMATED_SPEC_PREFIX(scheduleOnUI)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -30,7 +19,7 @@ static jsi::Value SPEC_PREFIX(scheduleOnUI)(
return jsi::Value::undefined();
}

static jsi::Value SPEC_PREFIX(executeOnUIRuntimeSync)(
static jsi::Value REANIMATED_SPEC_PREFIX(executeOnUIRuntimeSync)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -39,7 +28,7 @@ static jsi::Value SPEC_PREFIX(executeOnUIRuntimeSync)(
->executeOnUIRuntimeSync(rt, std::move(args[0]));
}

static jsi::Value SPEC_PREFIX(createWorkletRuntime)(
static jsi::Value REANIMATED_SPEC_PREFIX(createWorkletRuntime)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -48,7 +37,7 @@ static jsi::Value SPEC_PREFIX(createWorkletRuntime)(
->createWorkletRuntime(rt, std::move(args[0]), std::move(args[1]));
}

static jsi::Value SPEC_PREFIX(scheduleOnRuntime)(
static jsi::Value REANIMATED_SPEC_PREFIX(scheduleOnRuntime)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -57,7 +46,7 @@ static jsi::Value SPEC_PREFIX(scheduleOnRuntime)(
->scheduleOnRuntime(rt, std::move(args[0]), std::move(args[1]));
}

static jsi::Value SPEC_PREFIX(registerEventHandler)(
static jsi::Value REANIMATED_SPEC_PREFIX(registerEventHandler)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -67,7 +56,7 @@ static jsi::Value SPEC_PREFIX(registerEventHandler)(
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
}

static jsi::Value SPEC_PREFIX(unregisterEventHandler)(
static jsi::Value REANIMATED_SPEC_PREFIX(unregisterEventHandler)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -77,7 +66,7 @@ static jsi::Value SPEC_PREFIX(unregisterEventHandler)(
return jsi::Value::undefined();
}

static jsi::Value SPEC_PREFIX(getViewProp)(
static jsi::Value REANIMATED_SPEC_PREFIX(getViewProp)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -88,7 +77,7 @@ static jsi::Value SPEC_PREFIX(getViewProp)(
return jsi::Value::undefined();
}

static jsi::Value SPEC_PREFIX(enableLayoutAnimations)(
static jsi::Value REANIMATED_SPEC_PREFIX(enableLayoutAnimations)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -98,7 +87,7 @@ static jsi::Value SPEC_PREFIX(enableLayoutAnimations)(
return jsi::Value::undefined();
}

static jsi::Value SPEC_PREFIX(registerSensor)(
static jsi::Value REANIMATED_SPEC_PREFIX(registerSensor)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -112,7 +101,7 @@ static jsi::Value SPEC_PREFIX(registerSensor)(
std::move(args[3]));
}

static jsi::Value SPEC_PREFIX(unregisterSensor)(
static jsi::Value REANIMATED_SPEC_PREFIX(unregisterSensor)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -122,7 +111,7 @@ static jsi::Value SPEC_PREFIX(unregisterSensor)(
return jsi::Value::undefined();
}

static jsi::Value SPEC_PREFIX(configureProps)(
static jsi::Value REANIMATED_SPEC_PREFIX(configureProps)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -132,7 +121,7 @@ static jsi::Value SPEC_PREFIX(configureProps)(
return jsi::Value::undefined();
}

static jsi::Value SPEC_PREFIX(subscribeForKeyboardEvents)(
static jsi::Value REANIMATED_SPEC_PREFIX(subscribeForKeyboardEvents)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -142,7 +131,7 @@ static jsi::Value SPEC_PREFIX(subscribeForKeyboardEvents)(
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
}

static jsi::Value SPEC_PREFIX(unsubscribeFromKeyboardEvents)(
static jsi::Value REANIMATED_SPEC_PREFIX(unsubscribeFromKeyboardEvents)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -152,7 +141,7 @@ static jsi::Value SPEC_PREFIX(unsubscribeFromKeyboardEvents)(
return jsi::Value::undefined();
}

static jsi::Value SPEC_PREFIX(configureLayoutAnimationBatch)(
static jsi::Value REANIMATED_SPEC_PREFIX(configureLayoutAnimationBatch)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -161,7 +150,7 @@ static jsi::Value SPEC_PREFIX(configureLayoutAnimationBatch)(
->configureLayoutAnimationBatch(rt, std::move(args[0]));
}

static jsi::Value SPEC_PREFIX(setShouldAnimateExiting)(
static jsi::Value REANIMATED_SPEC_PREFIX(setShouldAnimateExiting)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
Expand All @@ -174,37 +163,38 @@ static jsi::Value SPEC_PREFIX(setShouldAnimateExiting)(
NativeReanimatedModuleSpec::NativeReanimatedModuleSpec(
const std::shared_ptr<CallInvoker> &jsInvoker)
: TurboModule("NativeReanimated", jsInvoker) {
methodMap_["makeShareableClone"] =
MethodMetadata{2, SPEC_PREFIX(makeShareableClone)};

methodMap_["scheduleOnUI"] = MethodMetadata{1, SPEC_PREFIX(scheduleOnUI)};
methodMap_["scheduleOnUI"] =
MethodMetadata{1, REANIMATED_SPEC_PREFIX(scheduleOnUI)};
methodMap_["executeOnUIRuntimeSync"] =
MethodMetadata{1, SPEC_PREFIX(executeOnUIRuntimeSync)};
MethodMetadata{1, REANIMATED_SPEC_PREFIX(executeOnUIRuntimeSync)};
methodMap_["createWorkletRuntime"] =
MethodMetadata{2, SPEC_PREFIX(createWorkletRuntime)};
MethodMetadata{2, REANIMATED_SPEC_PREFIX(createWorkletRuntime)};
methodMap_["scheduleOnRuntime"] =
MethodMetadata{2, SPEC_PREFIX(scheduleOnRuntime)};
MethodMetadata{2, REANIMATED_SPEC_PREFIX(scheduleOnRuntime)};

methodMap_["registerEventHandler"] =
MethodMetadata{3, SPEC_PREFIX(registerEventHandler)};
MethodMetadata{3, REANIMATED_SPEC_PREFIX(registerEventHandler)};
methodMap_["unregisterEventHandler"] =
MethodMetadata{1, SPEC_PREFIX(unregisterEventHandler)};
MethodMetadata{1, REANIMATED_SPEC_PREFIX(unregisterEventHandler)};

methodMap_["getViewProp"] = MethodMetadata{3, SPEC_PREFIX(getViewProp)};
methodMap_["getViewProp"] =
MethodMetadata{3, REANIMATED_SPEC_PREFIX(getViewProp)};
methodMap_["enableLayoutAnimations"] =
MethodMetadata{2, SPEC_PREFIX(enableLayoutAnimations)};
methodMap_["registerSensor"] = MethodMetadata{4, SPEC_PREFIX(registerSensor)};
MethodMetadata{2, REANIMATED_SPEC_PREFIX(enableLayoutAnimations)};
methodMap_["registerSensor"] =
MethodMetadata{4, REANIMATED_SPEC_PREFIX(registerSensor)};
methodMap_["unregisterSensor"] =
MethodMetadata{1, SPEC_PREFIX(unregisterSensor)};
methodMap_["configureProps"] = MethodMetadata{2, SPEC_PREFIX(configureProps)};
MethodMetadata{1, REANIMATED_SPEC_PREFIX(unregisterSensor)};
methodMap_["configureProps"] =
MethodMetadata{2, REANIMATED_SPEC_PREFIX(configureProps)};
methodMap_["subscribeForKeyboardEvents"] =
MethodMetadata{2, SPEC_PREFIX(subscribeForKeyboardEvents)};
MethodMetadata{2, REANIMATED_SPEC_PREFIX(subscribeForKeyboardEvents)};
methodMap_["unsubscribeFromKeyboardEvents"] =
MethodMetadata{1, SPEC_PREFIX(unsubscribeFromKeyboardEvents)};
MethodMetadata{1, REANIMATED_SPEC_PREFIX(unsubscribeFromKeyboardEvents)};

methodMap_["configureLayoutAnimationBatch"] =
MethodMetadata{1, SPEC_PREFIX(configureLayoutAnimationBatch)};
MethodMetadata{1, REANIMATED_SPEC_PREFIX(configureLayoutAnimationBatch)};
methodMap_["setShouldAnimateExitingForTag"] =
MethodMetadata{2, SPEC_PREFIX(setShouldAnimateExiting)};
MethodMetadata{2, REANIMATED_SPEC_PREFIX(setShouldAnimateExiting)};
}
} // namespace reanimated
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ class JSI_EXPORT NativeReanimatedModuleSpec : public TurboModule {
const std::shared_ptr<CallInvoker> &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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#endif // RCT_NEW_ARCH_ENABLED

#include <worklets/NativeModules/NativeWorkletsModule.h>
#include <worklets/SharedItems/Shareables.h>

#ifdef __ANDROID__
#include <fbjni/fbjni.h>
Expand All @@ -22,4 +23,16 @@ 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) {
// TODO: It might be a good idea to rename one of these methods to avoid
// confusion.
return worklets::makeShareableClone(
rt, value, shouldRetainRemote, nativeStateSource);
}

} // namespace worklets
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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_;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
#include <worklets/NativeModules/NativeWorkletsModuleSpec.h>

#include <utility>

#define WORKLETS_SPEC_PREFIX(FN_NAME) \
__hostFunction_NativeWorkletsModuleSpec_##FN_NAME

namespace worklets {

static jsi::Value WORKLETS_SPEC_PREFIX(makeShareableClone)(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t) {
return static_cast<NativeWorkletsModuleSpec *>(&turboModule)
->makeShareableClone(
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
}

NativeWorkletsModuleSpec::NativeWorkletsModuleSpec(
const std::shared_ptr<CallInvoker> jsInvoker)
: TurboModule("NativeWorklets", jsInvoker) {}
: TurboModule("NativeWorklets", jsInvoker) {
methodMap_["makeShareableClone"] =
MethodMetadata{2, WORKLETS_SPEC_PREFIX(makeShareableClone)};
}

} // namespace worklets
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class JSI_EXPORT NativeWorkletsModuleSpec : public TurboModule {
protected:
explicit NativeWorkletsModuleSpec(
const std::shared_ptr<CallInvoker> jsInvoker);

public:
virtual jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) = 0;
};

} // namespace worklets
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
this.#reanimatedModuleProxy = global.__reanimatedModuleProxy;
}

makeShareableClone<T>(
value: T,
shouldPersistRemote: boolean,
nativeStateSource?: object
) {
return this.#reanimatedModuleProxy.makeShareableClone(
value,
shouldPersistRemote,
nativeStateSource
);
}

scheduleOnUI<T>(shareable: ShareableRef<T>) {
return this.#reanimatedModuleProxy.scheduleOnUI(shareable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ class JSReanimated implements IReanimatedModule {
sensors = new Map<number, WebSensor>();
platform?: Platform = undefined;

makeShareableClone<T>(): ShareableRef<T> {
throw new ReanimatedError(
'makeShareableClone should never be called in JSReanimated.'
);
}

scheduleOnUI<T>(worklet: ShareableRef<T>) {
// @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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import type { WorkletRuntime } from '../runtimes';

/** Type of `__reanimatedModuleProxy` injected with JSI. */
export interface ReanimatedModuleProxy {
makeShareableClone<T>(
value: T,
shouldPersistRemote: boolean,
nativeStateSource?: object
): ShareableRef<T>;

scheduleOnUI<T>(shareable: ShareableRef<T>): void;

executeOnUIRuntimeSync<T, R>(shareable: ShareableRef<T>): R;
Expand Down
Loading

0 comments on commit f54fb3c

Please sign in to comment.