1515#include " WKTJsiSetImmediateDecorator.h"
1616#include " WKTJsiJsDecorator.h"
1717#include " WKTFunctionInvoker.h"
18+ #include " WKTJsiPromise.h"
1819
1920#include < exception>
2021#include < functional>
@@ -182,33 +183,24 @@ JsiWorkletContext::createCallOnJS(jsi::Runtime &runtime, const jsi::Value &maybe
182183 const jsi::Value *arguments,
183184 size_t count) -> jsi::Value {
184185 // Runs the given function on the default main JS runtime
185- auto callOnTargetRuntime = [jsCallInvoker](std::function<void (jsi::Runtime& runtime)> callback) {
186+ JSCallInvoker callOnTargetRuntime = [jsCallInvoker](std::function<void (jsi::Runtime& runtime)> callback) {
186187 jsCallInvoker (std::move (callback));
187188 };
188189
189- // Runs the given function on the Runtime that originally invoked this method call (to resolve/reject the Promise)
190- JSCallInvoker callbackToOriginalRuntime;
191- JsiWorkletContext* currentContext = getCurrent (runtime);
192- if (currentContext != nullptr ) {
193- // This function is called from a Worklet context, so we wanna schedule back to that Worklet context to resolve the Promise.
194- std::weak_ptr<JsiWorkletContext> weakContext = currentContext->shared_from_this ();
195- callbackToOriginalRuntime = [weakContext](std::function<void (jsi::Runtime& toRuntime)> callback) {
196- auto context = weakContext.lock ();
197- if (context == nullptr ) [[unlikely]] {
198- throw std::runtime_error (" Cannot call back to JS - the calling context has already been destroyed!" );
199- }
200- context->invokeOnWorkletThread ([callback = std::move (callback)](JsiWorkletContext*, jsi::Runtime& originalRuntime) {
201- callback (originalRuntime);
202- });
203- };
204- } else {
205- // This functio nis called from the default JS Context, so we wanna just schedule back to JS to resolve the Promise.
206- callbackToOriginalRuntime = jsCallInvoker;
207- }
208-
209- // Create and run Promise.
210- std::shared_ptr<JsiPromiseWrapper> promise = invoker->call (runtime, thisValue, arguments, count, std::move (callOnTargetRuntime), std::move (callbackToOriginalRuntime));
211- return jsi::Object::createFromHostObject (runtime, promise);
190+ // Run function.
191+ auto promise = JsiPromise::createPromise (runtime,
192+ jsCallInvoker,
193+ [invoker,
194+ &thisValue,
195+ &arguments,
196+ &count,
197+ callOnTargetRuntime = std::move (callOnTargetRuntime)
198+ ](jsi::Runtime& runtime,
199+ JsiPromise::Resolver resolve,
200+ JsiPromise::Rejecter reject) {
201+ invoker->callAndForget (runtime, thisValue, arguments, count, std::move (callOnTargetRuntime), std::move (resolve), std::move (reject));
202+ });
203+ return promise;
212204 };
213205}
214206
@@ -229,35 +221,31 @@ jsi::HostFunctionType JsiWorkletContext::createCallInContext(jsi::Runtime &runti
229221 });
230222 };
231223
232- // Runs the given function on the Runtime that originally invoked this method call (to resolve/reject the Promise)
233- JSCallInvoker callbackToOriginalRuntime;
234- JsiWorkletContext* currentContext = getCurrent (runtime);
235- if (currentContext != nullptr ) {
236- // This function is called from a Worklet context, so we wanna schedule back to that Worklet context to resolve the Promise.
237- std::weak_ptr<JsiWorkletContext> weakContext = currentContext->shared_from_this ();
238- callbackToOriginalRuntime = [weakContext](std::function<void (jsi::Runtime& toRuntime)> callback) {
239- auto context = weakContext.lock ();
240- if (context == nullptr ) [[unlikely]] {
241- throw std::runtime_error (" Cannot call back to JS - the calling context has already been destroyed!" );
242- }
243- context->invokeOnWorkletThread ([callback = std::move (callback)](JsiWorkletContext*, jsi::Runtime& originalRuntime) {
244- callback (originalRuntime);
245- });
246- };
247- } else {
248- // This functio nis called from the default JS Context, so we wanna just schedule back to JS to resolve the Promise.
249- callbackToOriginalRuntime = [weakSelf](std::function<void (jsi::Runtime& toRuntime)> callback) {
250- auto self = weakSelf.lock ();
251- if (self == nullptr ) [[unlikely]] {
252- throw std::runtime_error (" Cannot call back to JS - the target context has already been destroyed!" );
253- }
254- self->invokeOnJsThread (std::move (callback));
255- };
256- }
257224
258- // Create and run Promise.
259- std::shared_ptr<JsiPromiseWrapper> promise = invoker->call (runtime, thisValue, arguments, count, std::move (runOnTargetRuntime), std::move (callbackToOriginalRuntime));
260- return jsi::Object::createFromHostObject (runtime, promise);
225+ // Run function.
226+ JSCallInvoker jsCallInvoker = [weakSelf](std::function<void (jsi::Runtime& toRuntime)> callback) {
227+ auto self = weakSelf.lock ();
228+ if (self == nullptr ) [[unlikely]] {
229+ throw std::runtime_error (" Cannot call Worklet - the target context has already been destroyed!" );
230+ }
231+ self->_jsCallInvoker ([self, callback = std::move (callback)]() {
232+ callback (*self->_jsRuntime );
233+ });
234+ };
235+ auto promise = JsiPromise::createPromise (runtime,
236+ jsCallInvoker,
237+ [invoker,
238+ &thisValue,
239+ &arguments,
240+ &count,
241+ runOnTargetRuntime = std::move (runOnTargetRuntime)
242+ ](jsi::Runtime& runtime,
243+ JsiPromise::Resolver resolve,
244+ JsiPromise::Rejecter reject) {
245+ invoker->callAndForget (runtime, thisValue, arguments, count, std::move (runOnTargetRuntime), std::move (resolve), std::move (reject));
246+ });
247+
248+ return promise;
261249 };
262250}
263251
0 commit comments