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

Better error message when trying to call non-worklet from UI thread #3821

Merged
merged 5 commits into from
Nov 30, 2022
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
7 changes: 7 additions & 0 deletions Common/cpp/SharedItems/Shareables.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,14 @@ class ShareableRemoteFunction
function_(std::move(function)) {}
jsi::Value toJSValue(jsi::Runtime &rt) override {
if (runtimeHelper_->isUIRuntime(rt)) {
#ifdef DEBUG
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
return runtimeHelper_->valueUnpacker->call(
rt,
ShareableJSRef::newHostObject(rt, shared_from_this()),
jsi::String::createFromAscii(rt, "RemoteFunction"));
#else
return ShareableJSRef::newHostObject(rt, shared_from_this());
#endif
} else {
return jsi::Value(rt, function_);
}
Expand Down
1 change: 1 addition & 0 deletions src/reanimated2/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface NativeEvent<T> {
export interface ComplexWorkletFunction<A extends any[], R>
extends WorkletFunction {
(...args: A): R;
__remoteFunction?: (...args: A) => R;
}

export interface NestedObject<T> {
Expand Down
12 changes: 11 additions & 1 deletion src/reanimated2/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function getViewProp<T>(viewTag: string, propName: string): Promise<T> {
});
}

function valueUnpacker(objectToUnpack: any): any {
function valueUnpacker(objectToUnpack: any, category?: string): any {
'worklet';
let workletsCache = global.__workletsCache;
let handleCache = global.__handleCache;
Expand Down Expand Up @@ -142,6 +142,16 @@ function valueUnpacker(objectToUnpack: any): any {
handleCache!.set(objectToUnpack, value);
}
return value;
} else if (category === 'RemoteFunction') {
const fun = () => {
throw new Error(`Tried to synchronously call a non-worklet function on the UI thread.

Possible solutions are:
a) If you want to synchronously execute this method, mark it as a worklet
b) If you want to execute this function on the JS thread, wrap it using \`runOnJS\``);
};
fun.__remoteFunction = objectToUnpack;
return fun;
} else {
throw new Error('data type not recognized by unpack method');
}
Expand Down
7 changes: 7 additions & 0 deletions src/reanimated2/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export function runOnJS<A extends any[], R>(
fun: ComplexWorkletFunction<A, R>
): (...args: A) => void {
'worklet';
if (fun.__remoteFunction) {
// in development mode the function provided as `fun` throws an error message
// such that when someone accidently calls it directly on the UI runtime, they
// see that they should use `runOnJS` instead. To facilitate that we purt the
// reference to the original remote function in the `__remoteFunction` property.
fun = fun.__remoteFunction;
}
if (!_WORKLET) {
return fun;
}
Expand Down