Skip to content

Commit 24af6ac

Browse files
committed
Fix JS dispatch
1 parent 9859d41 commit 24af6ac

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

cpp/RNMultithreadingInstaller.cpp

+10-15
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,22 @@ void install(jsi::Runtime& runtime,
4040
jsi::PropNameID::forAscii(runtime, "spawnThreadCallback"),
4141
2,
4242
[](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
43-
auto resolver = [&runtime, &arguments](jsi::Value value) {
44-
manager->scheduler->scheduleOnJS([&runtime, &arguments, &value] () {
45-
arguments[0]
46-
.asObject(runtime)
47-
.asFunction(runtime)
48-
.call(runtime, value);
43+
auto resolverValue = std::make_shared<jsi::Value>((arguments[0].asObject(runtime)));
44+
auto rejecterValue = std::make_shared<jsi::Value>((arguments[1].asObject(runtime)));
45+
46+
auto resolver = [&runtime, resolverValue](jsi::Value value) {
47+
manager->scheduler->scheduleOnJS([&runtime, resolverValue, &value] () {
48+
resolverValue->asObject(runtime).asFunction(runtime).call(runtime, value);
4949
});
5050
};
51-
auto rejecter = [&runtime, &arguments](std::string message) {
52-
manager->scheduler->scheduleOnJS([&runtime, &arguments, message] () {
53-
auto label = runtime.global().getProperty(runtime, "_LABEL");
54-
auto l = label.asString(runtime);
55-
arguments[1]
56-
.asObject(runtime)
57-
.asFunction(runtime)
58-
.call(runtime, jsi::JSError(runtime, message).value());
51+
auto rejecter = [&runtime, rejecterValue](std::string message) {
52+
manager->scheduler->scheduleOnJS([&runtime, rejecterValue, message] () {
53+
rejecterValue->asObject(runtime).asFunction(runtime).call(runtime, jsi::JSError(runtime, message).value());
5954
});
6055
};
6156
auto run = reanimated::ShareableValue::adapt(runtime, arguments[0], manager.get());
6257

63-
pool.enqueue([&resolver, &rejecter, run]() {
58+
pool.enqueue([resolver, rejecter, run]() {
6459
try {
6560
auto& runtime = *manager->runtime;
6661
auto funcValue = run->getValue(runtime);

example/src/App.tsx

+14-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Text,
77
TextInput,
88
ActivityIndicator,
9+
Alert,
910
} from 'react-native';
1011
import { spawnThread } from 'react-native-multithreading';
1112

@@ -21,13 +22,19 @@ export default function App() {
2122

2223
const run = React.useCallback(async () => {
2324
setIsRunning(true);
24-
const fib = await spawnThread(() => {
25-
const parsedInput = Number.parseInt(input, 10);
26-
const value = fibonacci(parsedInput);
27-
return value;
28-
});
29-
setResult(fib);
30-
setIsRunning(false);
25+
try {
26+
const fib = await spawnThread(() => {
27+
const parsedInput = Number.parseInt(input, 10);
28+
const value = fibonacci(parsedInput);
29+
return value;
30+
});
31+
setResult(fib);
32+
} catch (e) {
33+
const msg = e instanceof Error ? e.message : JSON.stringify(e);
34+
Alert.alert('Error', msg);
35+
} finally {
36+
setIsRunning(false);
37+
}
3138
}, [input]);
3239

3340
React.useEffect(() => {

0 commit comments

Comments
 (0)