Skip to content
New issue

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

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

Already on GitHub? # to your account

refactor(Worklets): Move shareable creation to Worklets #6576

Draft
wants to merge 2 commits into
base: @tjzel/worklets/native-worklets-module-stub
Choose a base branch
from
Draft
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 @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NativeReanimatedModuleSpec *>(&turboModule)
->makeShareableClone(
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
}

// scheduler

static jsi::Value SPEC_PREFIX(scheduleOnUI)(
Expand Down Expand Up @@ -174,9 +162,6 @@ 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_["executeOnUIRuntimeSync"] =
MethodMetadata{1, SPEC_PREFIX(executeOnUIRuntimeSync)};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,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,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
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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, 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 @@ -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();
}
Expand All @@ -64,18 +65,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,11 +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;
createWorkletRuntime(
Expand Down
14 changes: 7 additions & 7 deletions packages/react-native-reanimated/src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import type {
} from 'react-native';
import type { WorkletRuntime } from './runtimes';

export interface IWorkletsModule {}
export interface IWorkletsModule {
makeShareableClone<TValue>(
value: TValue,
shouldPersistRemote: boolean,
nativeStateSource?: object
): ShareableRef<TValue>;
}

export interface IReanimatedModule {
registerSensor(
Expand Down Expand Up @@ -49,12 +55,6 @@ export interface IReanimatedModule {

unsubscribeFromKeyboardEvents(listenerId: number): void;

makeShareableClone<TValue>(
value: TValue,
shouldPersistRemote: boolean,
nativeStateSource?: object
): ShareableRef<TValue>;

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

executeOnUIRuntimeSync<TValue, TResult>(
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-reanimated/src/shareables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -283,7 +283,7 @@ Offending code was: \`${getWorkletCode(value)}\``);
return adapted;
}
}
return ReanimatedModule.makeShareableClone(
return WorkletsModule.makeShareableClone(
value,
shouldPersistRemote,
undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T>(): ShareableRef<T> {
throw new ReanimatedError(
'makeShareableClone should never be called in JSReanimated.'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
}
this.#workletsModuleProxy = global.__workletsModuleProxy;
}

makeShareableClone<T>(
value: T,
shouldPersistRemote: boolean,
nativeStateSource?: object
) {
return this.#workletsModuleProxy.makeShareableClone(
value,
shouldPersistRemote,
nativeStateSource
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
'use strict';

import type { ShareableRef } from '../../commonTypes';

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