55#include < tuple>
66#include < vector>
77
8+ #include < ReactCommon/CallInvoker.h>
9+
810#include < rnexecutorch/Log.h>
911#include < rnexecutorch/host_objects/JsiConversions.h>
1012#include < rnexecutorch/jsi/JsiHostObject.h>
11- #include < rnexecutorch/jsi/JsiPromise .h>
13+ #include < rnexecutorch/jsi/Promise .h>
1214
1315namespace rnexecutorch {
1416
1517template <typename Model> class ModelHostObject : public JsiHostObject {
1618public:
17- explicit ModelHostObject (
18- const std::shared_ptr<Model> &model, jsi::Runtime *runtime,
19- const std::shared_ptr<react::CallInvoker> &callInvoker)
20- : model(model), promiseVendor(runtime, callInvoker) {
21- addFunctions (JSI_EXPORT_FUNCTION (ModelHostObject, forward));
19+ explicit ModelHostObject (const std::shared_ptr<Model> &model,
20+ std::shared_ptr<react::CallInvoker> callInvoker)
21+ : model(model), callInvoker(callInvoker) {
22+ addFunctions (JSI_EXPORT_FUNCTION (ModelHostObject<Model>, forward));
2223 }
2324
2425 JSI_HOST_FUNCTION (forward) {
25- auto promise = promiseVendor.createPromise (
26+ auto promise = Promise::createPromise (
27+ runtime, callInvoker,
2628 [this , count, args, &runtime](std::shared_ptr<Promise> promise) {
2729 constexpr std::size_t forwardArgCount =
2830 jsiconversion::getArgumentCount (&Model::forward);
@@ -37,27 +39,29 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
3739 return ;
3840 }
3941
40- // Do the asynchronous work
41- std::thread ([this , promise = std::move (promise), args, &runtime]() {
42+ // We need to dispatch a thread if we want the forward to be
43+ // asynchronous
44+ std::thread ([args, &runtime, this , promise = std::move (promise)]() {
4245 try {
4346 auto argsConverted = jsiconversion::createArgsTupleFromJsi (
4447 &Model::forward, args, runtime);
4548 auto result = std::apply (std::bind_front (&Model::forward, model),
4649 argsConverted);
4750
48- promise->resolve ([result =
49- std::move (result)](jsi::Runtime &runtime) {
50- return jsiconversion::getJsiValue (std::move (result), runtime);
51- });
51+ promise->resolve (
52+ jsiconversion::getJsiValue (std::move (result), runtime));
5253 } catch (const std::runtime_error &e) {
53- // This catch should be merged with the next one
54- // (std::runtime_error inherits from std::exception) HOWEVER react
55- // native has broken RTTI which breaks proper exception type
56- // checking. Remove when the following change is present in our
57- // version:
54+ // This catch should be merged with the next two
55+ // (std::runtime_error and jsi::JSError inherits from
56+ // std::exception) HOWEVER react native has broken RTTI which
57+ // breaks proper exception type checking. Remove when the
58+ // following change is present in our version:
5859 // https://github.com/facebook/react-native/commit/3132cc88dd46f95898a756456bebeeb6c248f20e
5960 promise->reject (e.what ());
6061 return ;
62+ } catch (const jsi::JSError &e) {
63+ promise->reject (e.what ());
64+ return ;
6165 } catch (const std::exception &e) {
6266 promise->reject (e.what ());
6367 return ;
@@ -73,7 +77,7 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
7377
7478private:
7579 std::shared_ptr<Model> model;
76- PromiseVendor promiseVendor ;
80+ std::shared_ptr<react::CallInvoker> callInvoker ;
7781};
7882
7983} // namespace rnexecutorch
0 commit comments