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

Fix passing remote functions as arguments to runOnJS #4617

Merged
merged 16 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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: 5 additions & 1 deletion src/reanimated2/mutables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NativeReanimatedModule from './NativeReanimated';
import type { SharedValue, ShareableSyncDataHolderRef } from './commonTypes';
import {
ShareableClone,
makeShareableCloneOnUIRecursive,
makeShareableCloneRecursive,
registerShareableMapping,
Expand Down Expand Up @@ -36,7 +37,10 @@ export function makeUIMutable<T>(
if (syncDataHolder) {
_updateDataSynchronously(
syncDataHolder,
makeShareableCloneOnUIRecursive(newValue)
makeShareableCloneOnUIRecursive(
// this is a scam
newValue as unknown as ShareableClone<object>
)
);
}
listeners.forEach((listener) => {
Expand Down
45 changes: 38 additions & 7 deletions src/reanimated2/shareables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const _shareableFlag = Symbol('shareable flag');
const MAGIC_KEY = 'REANIMATED_MAGIC_KEY';

function isHostObject(value: any): boolean {
'worklet';
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
// We could use JSI to determine whether an object is a host object, however
// the below workaround works well and is way faster than an additional JSI call.
// We use the fact that host objects have broken implementation of `hasOwnProperty`
Expand Down Expand Up @@ -229,28 +230,58 @@ export function makeShareableCloneRecursive<T>(
return NativeReanimatedModule.makeShareableClone(value, shouldPersistRemote);
}

export function makeShareableCloneOnUIRecursive<T>(value: T): ShareableRef<T> {
type RemoteFunctionShareableClone<T> = {
__remoteFunction: ShareableRef<T>;
};

export type ShareableClone<T> =
| T
| RemoteFunctionShareableClone<T>
| ShareableRef<T>[];

function isRemoteFunctionShareableClone<T extends object>(
value: ShareableClone<T>
): value is RemoteFunctionShareableClone<T> {
return '__remoteFunction' in value;
}

export function makeShareableCloneOnUIRecursive<T extends object>(
value: ShareableClone<T>
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
): ShareableRef<T> {
'worklet';
if (USE_STUB_IMPLEMENTATION) {
// @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread
// see more details in the comment where USE_STUB_IMPLEMENTATION is defined.
return value;
}
function cloneRecursive<T>(value: T): ShareableRef<T> {
const type = typeof value;
if ((type === 'object' || type === 'function') && value !== null) {
let toAdapt: any;
function cloneRecursive<T extends object>(
value: ShareableClone<T>
): ShareableRef<T> {
if (
(typeof value === 'object' && value !== null) ||
typeof value === 'function'
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
) {
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
if (isRemoteFunctionShareableClone(value)) {
return value.__remoteFunction;
}
if (isHostObject(value)) {
return value as ShareableRef<T>;
}
let toAdapt:
| ShareableRef<ShareableRef<T>>[]
| Record<string, ShareableRef<T>>;
if (Array.isArray(value)) {
toAdapt = value.map((element) => cloneRecursive(element));
return _makeShareableClone(toAdapt);
} else if (value !== undefined) {
toAdapt = {};
for (const [key, element] of Object.entries(
value as Record<string, unknown>
value as Record<string, ShareableClone<T>>
)) {
toAdapt[key] = cloneRecursive(element);
}
return _makeShareableClone(toAdapt);
}
return _makeShareableClone(toAdapt);
}
return _makeShareableClone(value);
}
Expand Down