1+ #pragma once
2+
13#include < cstdio>
24#include < string>
35#include < tuple>
46#include < vector>
57
8+ #include < ReactCommon/CallInvoker.h>
9+
610#include < rnexecutorch/Log.h>
711#include < rnexecutorch/host_objects/JsiConversions.h>
812#include < rnexecutorch/jsi/JsiHostObject.h>
9- #include < rnexecutorch/jsi/JsiPromise .h>
13+ #include < rnexecutorch/jsi/Promise .h>
1014
1115namespace rnexecutorch {
1216
1317template <typename Model> class ModelHostObject : public JsiHostObject {
1418public:
15- explicit ModelHostObject (
16- const std::shared_ptr<Model> &model, jsi::Runtime *runtime,
17- const std::shared_ptr<react::CallInvoker> &callInvoker)
18- : model(model), promiseVendor(runtime, callInvoker) {
19- 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));
2023 }
2124
2225 JSI_HOST_FUNCTION (forward) {
23- auto promise = promiseVendor.createPromise (
26+ auto promise = Promise::createPromise (
27+ runtime, callInvoker,
2428 [this , count, args, &runtime](std::shared_ptr<Promise> promise) {
2529 constexpr std::size_t forwardArgCount =
2630 jsiconversion::getArgumentCount (&Model::forward);
@@ -35,27 +39,29 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
3539 return ;
3640 }
3741
38- // Do the asynchronous work
39- 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)]() {
4045 try {
4146 auto argsConverted = jsiconversion::createArgsTupleFromJsi (
4247 &Model::forward, args, runtime);
4348 auto result = std::apply (std::bind_front (&Model::forward, model),
4449 argsConverted);
4550
46- promise->resolve ([result =
47- std::move (result)](jsi::Runtime &runtime) {
48- return jsiconversion::getJsiValue (std::move (result), runtime);
49- });
51+ promise->resolve (
52+ jsiconversion::getJsiValue (std::move (result), runtime));
5053 } catch (const std::runtime_error &e) {
51- // This catch should be merged with the next one
52- // (std::runtime_error inherits from std::exception) HOWEVER react
53- // native has broken RTTI which breaks proper exception type
54- // checking. Remove when the following change is present in our
55- // 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:
5659 // https://github.com/facebook/react-native/commit/3132cc88dd46f95898a756456bebeeb6c248f20e
5760 promise->reject (e.what ());
5861 return ;
62+ } catch (const jsi::JSError &e) {
63+ promise->reject (e.what ());
64+ return ;
5965 } catch (const std::exception &e) {
6066 promise->reject (e.what ());
6167 return ;
@@ -71,7 +77,7 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
7177
7278private:
7379 std::shared_ptr<Model> model;
74- PromiseVendor promiseVendor ;
80+ std::shared_ptr<react::CallInvoker> callInvoker ;
7581};
7682
7783} // namespace rnexecutorch
0 commit comments