Skip to content
Merged
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
6 changes: 4 additions & 2 deletions Common/cpp/NativeModules/NativeReanimatedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ jsi::Value NativeReanimatedModule::scheduleOnRuntime(
jsi::Value NativeReanimatedModule::makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote) {
return reanimated::makeShareableClone(rt, value, shouldRetainRemote);
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) {
return reanimated::makeShareableClone(
rt, value, shouldRetainRemote, nativeStateSource);
}

jsi::Value NativeReanimatedModule::registerEventHandler(
Expand Down
3 changes: 2 additions & 1 deletion Common/cpp/NativeModules/NativeReanimatedModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote) override;
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)
Expand Down
3 changes: 2 additions & 1 deletion Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ static jsi::Value SPEC_PREFIX(makeShareableClone)(
const jsi::Value *args,
size_t) {
return static_cast<NativeReanimatedModuleSpec *>(&turboModule)
->makeShareableClone(rt, std::move(args[0]), std::move(args[1]));
->makeShareableClone(
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
}

// scheduler
Expand Down
3 changes: 2 additions & 1 deletion Common/cpp/NativeModules/NativeReanimatedModuleSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class JSI_EXPORT NativeReanimatedModuleSpec : public TurboModule {
virtual jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote) = 0;
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) = 0;

// Scheduling
virtual void scheduleOnUI(jsi::Runtime &rt, const jsi::Value &worklet) = 0;
Expand Down
9 changes: 7 additions & 2 deletions Common/cpp/ReanimatedRuntime/WorkletRuntimeDecorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ void WorkletRuntimeDecorator::decorate(
});

jsi_utils::installJsiFunction(
rt, "_makeShareableClone", [](jsi::Runtime &rt, const jsi::Value &value) {
rt,
"_makeShareableClone",
[](jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &nativeStateSource) {
auto shouldRetainRemote = jsi::Value::undefined();
return reanimated::makeShareableClone(rt, value, shouldRetainRemote);
return reanimated::makeShareableClone(
rt, value, shouldRetainRemote, nativeStateSource);
});

jsi_utils::installJsiFunction(
Expand Down
33 changes: 29 additions & 4 deletions Common/cpp/SharedItems/Shareables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ jsi::Function getCallGuard(jsi::Runtime &rt) {
jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote) {
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) {
std::shared_ptr<Shareable> shareable;
if (value.isObject()) {
auto object = value.asObject(rt);
Expand Down Expand Up @@ -76,10 +77,11 @@ jsi::Value makeShareableClone(
std::make_shared<ShareableHostObject>(rt, object.getHostObject(rt));
} else {
if (shouldRetainRemote.isBool() && shouldRetainRemote.getBool()) {
shareable =
std::make_shared<RetainingShareable<ShareableObject>>(rt, object);
shareable = std::make_shared<RetainingShareable<ShareableObject>>(
rt, object, nativeStateSource);
} else {
shareable = std::make_shared<ShareableObject>(rt, object);
shareable =
std::make_shared<ShareableObject>(rt, object, nativeStateSource);
}
}
} else if (value.isString()) {
Expand Down Expand Up @@ -198,6 +200,24 @@ ShareableObject::ShareableObject(jsi::Runtime &rt, const jsi::Object &object)
auto value = extractShareableOrThrow(rt, object.getProperty(rt, key));
data_.emplace_back(key.utf8(rt), value);
}
#if REACT_NATIVE_MINOR_VERSION >= 71
if (object.hasNativeState(rt)) {
nativeState_ = object.getNativeState(rt);
}
#endif
}

ShareableObject::ShareableObject(
jsi::Runtime &rt,
const jsi::Object &object,
const jsi::Value &nativeStateSource)
: ShareableObject(rt, object) {
#if REACT_NATIVE_MINOR_VERSION >= 71
if (nativeStateSource.isObject() &&
nativeStateSource.asObject(rt).hasNativeState(rt)) {
nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt);
}
#endif
}

jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) {
Expand All @@ -206,6 +226,11 @@ jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) {
obj.setProperty(
rt, data_[i].first.c_str(), data_[i].second->getJSValue(rt));
}
#if REACT_NATIVE_MINOR_VERSION >= 71
if (nativeState_ != nullptr) {
obj.setNativeState(rt, nativeState_);
}
#endif
return obj;
}

Expand Down
11 changes: 10 additions & 1 deletion Common/cpp/SharedItems/Shareables.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ class ShareableJSRef : public jsi::HostObject {
jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote);
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource);

std::shared_ptr<Shareable> extractShareableOrThrow(
jsi::Runtime &rt,
Expand Down Expand Up @@ -182,10 +183,18 @@ class ShareableObject : public Shareable {
public:
ShareableObject(jsi::Runtime &rt, const jsi::Object &object);

ShareableObject(
jsi::Runtime &rt,
const jsi::Object &object,
const jsi::Value &nativeStateSource);

jsi::Value toJSValue(jsi::Runtime &rt) override;

protected:
std::vector<std::pair<std::string, std::shared_ptr<Shareable>>> data_;
#if REACT_NATIVE_MINOR_VERSION >= 71
std::shared_ptr<jsi::NativeState> nativeState_;
#endif
};

class ShareableHostObject : public Shareable {
Expand Down
12 changes: 9 additions & 3 deletions src/reanimated2/NativeReanimated/NativeReanimated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import ReanimatedModule from '../../specs/NativeReanimatedModule';
export interface NativeReanimatedModule {
makeShareableClone<T>(
value: T,
shouldPersistRemote: boolean
shouldPersistRemote: boolean,
nativeStateSource?: object
): ShareableRef<T>;
scheduleOnUI<T>(shareable: ShareableRef<T>): void;
executeOnUIRuntimeSync<T, R>(shareable: ShareableRef<T>): R;
Expand Down Expand Up @@ -99,10 +100,15 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
this.InnerNativeModule = global.__reanimatedModuleProxy;
}

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

Expand Down
5 changes: 4 additions & 1 deletion src/reanimated2/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ declare global {
) => void;
var _notifyAboutEnd: (tag: number, removeView: boolean) => void;
var _setGestureState: (handlerTag: number, newState: number) => void;
var _makeShareableClone: <T>(value: T) => FlatShareableRef<T>;
var _makeShareableClone: <T>(
value: T,
nativeStateSource?: object
) => FlatShareableRef<T>;
var _scheduleOnJS: (fun: (...args: A) => R, args?: A) => void;
var _scheduleOnRuntime: (
runtime: WorkletRuntime,
Expand Down
21 changes: 15 additions & 6 deletions src/reanimated2/shareables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,19 @@ Offending code was: \`${getWorkletCode(value)}\``);
}
const adopted = NativeReanimatedModule.makeShareableClone(
toAdapt,
shouldPersistRemote
shouldPersistRemote,
value
);
shareableMappingCache.set(value, adopted);
shareableMappingCache.set(adopted);
return adopted;
}
}
return NativeReanimatedModule.makeShareableClone(value, shouldPersistRemote);
return NativeReanimatedModule.makeShareableClone(
value,
shouldPersistRemote,
undefined
);
}

const WORKLET_CODE_THRESHOLD = 255;
Expand Down Expand Up @@ -305,7 +310,10 @@ export function makeShareableCloneOnUIRecursive<T>(
if (isHostObject(value)) {
// We call `_makeShareableClone` to wrap the provided HostObject
// inside ShareableJSRef.
return global._makeShareableClone(value) as FlatShareableRef<T>;
return global._makeShareableClone(
value,
undefined
) as FlatShareableRef<T>;
}
if (isRemoteFunction<T>(value)) {
// RemoteFunctions are created by us therefore they are
Expand All @@ -315,16 +323,17 @@ export function makeShareableCloneOnUIRecursive<T>(
}
if (Array.isArray(value)) {
return global._makeShareableClone(
value.map(cloneRecursive)
value.map(cloneRecursive),
undefined
) as FlatShareableRef<T>;
}
const toAdapt: Record<string, FlatShareableRef<T>> = {};
for (const [key, element] of Object.entries(value)) {
toAdapt[key] = cloneRecursive(element);
}
return global._makeShareableClone(toAdapt) as FlatShareableRef<T>;
return global._makeShareableClone(toAdapt, value) as FlatShareableRef<T>;
}
return global._makeShareableClone(value);
return global._makeShareableClone(value, undefined);
}
return cloneRecursive(value);
}
Expand Down