-
Hi! This might really be a generic C++ question, but I have this code in a C++ only TurboModule: facebook::react::AsyncPromise<std::string> NativeSampleModule::promise(jsi::Runtime &rt) {
auto promise = facebook::react::AsyncPromise<std::string>(rt, jsInvoker_);
std::thread t([p = std::move(promise)]() {
});
t.detach();
return promise;
} Upon running this code, I get panic:
Is it not possible to move an
Similar code using void NativeSampleModule::callback(jsi::Runtime &rt, AsyncCallback<std::string> callback) {
std::thread t([callback = std::move(callback)]() {
callback.call("works without any issues");
});
t.detach();
} I can get promises to work by dynamically allocating it with I've tried looking at various C++ TurboModules in the wild but I couldn't figure things out. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hi @hsjoberg - Im trying to solve the same issue, have you made any progress? |
Beta Was this translation helpful? Give feedback.
-
Hi @NathHorrigan. I did, yes. I forgot to get back to this thread so thank you for pinging me. For the example code I gave in this post, the solution I found was that you can mark the lambda as auto promise = facebook::react::AsyncPromise<std::string>(rt, jsInvoker_);
std::thread t([promise]() mutable {
promise.resolve("test");
});
t.detach();
return promise; Note: you may want resolve within a The However, I found out that in complex scenarios this isn't sufficient to keep the promise alive. This can be used like this: Basically you allocate a new promise with Does this help? Otherwise I can try to explain further. EDIT: Fixed first example code. |
Beta Was this translation helpful? Give feedback.
-
This is a great resource, thank you!
…On Sat, 7 Sep 2024 at 13:26, Hampus Sjöberg ***@***.***> wrote:
Hi @NathHorrigan <https://github.com/NathHorrigan>. I did, yes. I forgot
to get back to this thread so thank you for pinging me.
For the example code I gave in this post, the solution I found was that
you can mark the lambda as mutable like this:
std::thread t([p = std::move(promise)]() mutable {
p.resolve();
});
t.detach();
Note: you may want resolve within a jsInvoker_->invokeAsync() to make
sure it runs on the main thread.
The mutable keyword makes sure you're allowed to mutate things within the
lambda, and you're able to call resolve on the promise (it will
de-allocate itself afterwards).
------------------------------
However, I found out that in complex scenarios this isn't sufficient to
keep the promise alive.
So in those situations my solution was to create a singleton class that
keeps the promises alive:
https://github.com/hsjoberg/react-native-turbo-lnd/blob/master/cpp/utils/PromiseKeeper.h
This can be used like this:
https://github.com/hsjoberg/react-native-turbo-lnd/blob/3a125f66f5e17f3954a9b3e5f34b46ea71df6b8f/cpp/TurboLndModule.cpp#L43-L58
Basically you allocate a new promise with uint64_t promiseId =
PromiseKeeper::getInstance().addPromise(promise);, and then when you want
to resolve it you just do: PromiseKeeper::getInstance().resolvePromise(promiseId,
std::move(<data goes here>));.
Does this help? Otherwise I can try to explain further.
—
Reply to this email directly, view it on GitHub
<#216 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADEV6NYP64AKCUFSGAZ2VO3ZVLWIDAVCNFSM6AAAAABM6OM3HSVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANJXGY4DKNQ>
.
You are receiving this because you were mentioned.Message ID:
<reactwg/react-native-new-architecture/repo-discussions/216/comments/10576856
@github.com>
|
Beta Was this translation helpful? Give feedback.
-
Updated the first solution, it was wrong. 😅 Now it should be all good. Cheers. |
Beta Was this translation helpful? Give feedback.
Hi @NathHorrigan. I did, yes. I forgot to get back to this thread so thank you for pinging me.
For the example code I gave in this post, the solution I found was that you can mark the lambda as
mutable
, and make a copy of the promise like this:Note: you may want resolve within a
jsInvoker_->invokeAsync()
to make sure it runs on the main thread.The
mutable
keyword makes sure you're allowed to copy it and mutate things within the lambda, and you're able to callresolve
on the promise (it will de-allocate itself afterwar…