diff --git a/example/src/getTests.ts b/example/src/getTests.ts index 76cf9a989..e51c68981 100644 --- a/example/src/getTests.ts +++ b/example/src/getTests.ts @@ -851,12 +851,14 @@ export function getTests( .didNotThrow() .equals('hello') ), - createTest('getValueFromJSCallbackAndWait(...)', async () => - (await it(() => testObject.getValueFromJSCallbackAndWait(() => 73))) - .didNotThrow() - .equals(73) - ), - createTest('callAll(...)', async () => + createTest( + 'Single callback can be called and awaited: getValueFromJSCallbackAndWait(...)', + async () => + (await it(() => testObject.getValueFromJSCallbackAndWait(() => 73))) + .didNotThrow() + .equals(73) + ), + createTest('Multiple callbacks are all called: callAll(...)', async () => ( await it(async () => { return timeoutedPromise((complete) => { @@ -872,10 +874,28 @@ export function getTests( .didNotThrow() .equals(3) ), - createTest('callSumUpNTimes(...)', async () => - (await it(async () => await testObject.callSumUpNTimes(() => 7, 5))) - .didNotThrow() - .equals(7 * 5 /* = 35 */) + createTest( + 'Callback can be called multiple times: callSumUpNTimes(...)', + async () => + (await it(async () => await testObject.callSumUpNTimes(() => 7, 5))) + .didNotThrow() + .equals(7 * 5 /* = 35 */) + ), + createTest( + 'Async callback can be awaited and returned on native side: callbackAsyncPromise(...)', + async () => + ( + await it(async () => { + return timeoutedPromise(async (complete) => { + const result = await testObject.callbackAsyncPromise(async () => { + return 13 + }) + complete(result) + }) + }) + ) + .didNotThrow() + .equals(13) ), // Objects diff --git a/packages/react-native-nitro-image/android/src/main/java/com/margelo/nitro/image/HybridTestObjectKotlin.kt b/packages/react-native-nitro-image/android/src/main/java/com/margelo/nitro/image/HybridTestObjectKotlin.kt index e1001476e..e49b5ca55 100644 --- a/packages/react-native-nitro-image/android/src/main/java/com/margelo/nitro/image/HybridTestObjectKotlin.kt +++ b/packages/react-native-nitro-image/android/src/main/java/com/margelo/nitro/image/HybridTestObjectKotlin.kt @@ -202,6 +202,14 @@ class HybridTestObjectKotlin: HybridTestObjectSwiftKotlinSpec() { } } + override fun callbackAsyncPromise(callback: () -> Promise>): Promise { + return Promise.async { + val promise = callback().await() + val result = promise.await() + return@async result + } + } + override fun getCar(): Car { return Car(2018.0, "Lamborghini", "Huracán", 640.0, Powertrain.GAS, null, true) } diff --git a/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.cpp b/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.cpp index ed1087fdf..943d21016 100644 --- a/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.cpp +++ b/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.cpp @@ -288,6 +288,17 @@ std::shared_ptr> HybridTestObjectCpp::callSumUpNTimes(const std: }); } +std::shared_ptr> +HybridTestObjectCpp::callbackAsyncPromise(const std::function>>>()>& callback) { + return Promise::async([=]() { + std::future>> future = callback()->await(); + std::shared_ptr> promise = future.get(); + std::future innerFuture = promise->await(); + double innerResult = innerFuture.get(); + return innerResult; + }); +} + std::shared_ptr> HybridTestObjectCpp::getValueFromJSCallbackAndWait(const std::function>()>& getValue) { return Promise::async([=]() -> double { diff --git a/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.hpp b/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.hpp index 902ca171f..8dfa3e14d 100644 --- a/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.hpp +++ b/packages/react-native-nitro-image/cpp/HybridTestObjectCpp.hpp @@ -113,6 +113,8 @@ class HybridTestObjectCpp : public HybridTestObjectCppSpec { getValueFromJSCallbackAndWait(const std::function>()>& getValue) override; void callAll(const std::function& first, const std::function& second, const std::function& third) override; std::shared_ptr> callSumUpNTimes(const std::function>()>& callback, double n) override; + std::shared_ptr> + callbackAsyncPromise(const std::function>>>()>& callback) override; std::shared_ptr> getValueFromJsCallback(const std::function>()>& callback, const std::function& andThenCall) override; diff --git a/packages/react-native-nitro-image/ios/HybridTestObjectSwift.swift b/packages/react-native-nitro-image/ios/HybridTestObjectSwift.swift index 8bed69cbd..8b0dfe3f7 100644 --- a/packages/react-native-nitro-image/ios/HybridTestObjectSwift.swift +++ b/packages/react-native-nitro-image/ios/HybridTestObjectSwift.swift @@ -82,6 +82,14 @@ class HybridTestObjectSwift : HybridTestObjectSwiftKotlinSpec { } } + func callbackAsyncPromise(callback: @escaping (() -> Promise>)) throws -> Promise { + return Promise.async { + let promise = try await callback().await() + let result = try await promise.await() + return result + } + } + func bounceStrings(array: [String]) throws -> [String] { return array diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp index 4a5f9c616..e54fd6c8d 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp @@ -19,6 +19,7 @@ #include "JFunc_void.hpp" #include "JFunc_void_std__optional_double_.hpp" #include "JFunc_std__shared_ptr_Promise_double__.hpp" +#include "JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.hpp" #include "JFunc_std__shared_ptr_Promise_std__string__.hpp" #include "JHybridBaseSpec.hpp" #include "JHybridChildSpec.hpp" @@ -46,6 +47,7 @@ int initialize(JavaVM* vm) { margelo::nitro::image::JFunc_void::registerNatives(); margelo::nitro::image::JFunc_void_std__optional_double_::registerNatives(); margelo::nitro::image::JFunc_std__shared_ptr_Promise_double__::registerNatives(); + margelo::nitro::image::JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____::registerNatives(); margelo::nitro::image::JFunc_std__shared_ptr_Promise_double__::registerNatives(); margelo::nitro::image::JFunc_std__shared_ptr_Promise_std__string__::registerNatives(); margelo::nitro::image::JFunc_void_std__string::registerNatives(); diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.hpp b/packages/react-native-nitro-image/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.hpp new file mode 100644 index 000000000..3a98e3463 --- /dev/null +++ b/packages/react-native-nitro-image/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.hpp @@ -0,0 +1,73 @@ +/// +/// JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © 2024 Marc Rousavy @ Margelo +/// + +#pragma once + +#include +#include + +#include +#include +#include + +namespace margelo::nitro::image { + + using namespace facebook; + + /** + * C++ representation of the callback Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____. + * This is a Kotlin `() -> Promise>`, backed by a `std::function<...>`. + */ + struct JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ final: public jni::HybridClass { + public: + static jni::local_ref fromCpp(const std::function>>>()>& func) { + return JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____::newObjectCxxArgs(func); + } + + public: + jni::local_ref call() { + std::shared_ptr>>> __result = _func(); + return [&]() { + jni::local_ref __localPromise = JPromise::create(); + jni::global_ref __promise = jni::make_global(__localPromise); + __result->addOnResolvedListener([=](const std::shared_ptr>& __result) { + __promise->cthis()->resolve([&]() { + jni::local_ref __localPromise = JPromise::create(); + jni::global_ref __promise = jni::make_global(__localPromise); + __result->addOnResolvedListener([=](const double& __result) { + __promise->cthis()->resolve(jni::JDouble::valueOf(__result)); + }); + __result->addOnRejectedListener([=](const std::exception_ptr& __error) { + auto __jniError = jni::getJavaExceptionForCppException(__error); + __promise->cthis()->reject(__jniError); + }); + return __localPromise; + }()); + }); + __result->addOnRejectedListener([=](const std::exception_ptr& __error) { + auto __jniError = jni::getJavaExceptionForCppException(__error); + __promise->cthis()->reject(__jniError); + }); + return __localPromise; + }(); + } + + public: + static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/image/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____;"; + static void registerNatives() { + registerHybrid({makeNativeMethod("call", JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____::call)}); + } + + private: + explicit JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(const std::function>>>()>& func): _func(func) { } + + private: + friend HybridBase; + std::function>>>()> _func; + }; + +} // namespace margelo::nitro::image diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.cpp b/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.cpp index 87c8370e5..b68af6d82 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.cpp @@ -60,6 +60,7 @@ namespace margelo::nitro::image { class HybridBaseSpec; } #include "JFunc_void.hpp" #include "JFunc_void_std__optional_double_.hpp" #include "JFunc_std__shared_ptr_Promise_double__.hpp" +#include "JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.hpp" #include "JFunc_std__shared_ptr_Promise_std__string__.hpp" #include "JFunc_void_std__string.hpp" @@ -537,6 +538,22 @@ namespace margelo::nitro::image { return __promise; }(); } + std::shared_ptr> JHybridTestObjectSwiftKotlinSpec::callbackAsyncPromise(const std::function>>>()>& callback) { + static const auto method = _javaPart->getClass()->getMethod(jni::alias_ref /* callback */)>("callbackAsyncPromise"); + auto __result = method(_javaPart, JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_double____::fromCpp(callback)); + return [&]() { + auto __promise = Promise::create(); + __result->cthis()->addOnResolvedListener([=](const jni::alias_ref& __boxedResult) { + auto __result = jni::static_ref_cast(__boxedResult); + __promise->resolve(__result->value()); + }); + __result->cthis()->addOnRejectedListener([=](const jni::alias_ref& __throwable) { + jni::JniException __jniError(__throwable); + __promise->reject(std::make_exception_ptr(__jniError)); + }); + return __promise; + }(); + } std::shared_ptr> JHybridTestObjectSwiftKotlinSpec::getValueFromJSCallbackAndWait(const std::function>()>& getValue) { static const auto method = _javaPart->getClass()->getMethod(jni::alias_ref /* getValue */)>("getValueFromJSCallbackAndWait"); auto __result = method(_javaPart, JFunc_std__shared_ptr_Promise_double__::fromCpp(getValue)); diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.hpp b/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.hpp index 563e68d0e..4150f9f82 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/c++/JHybridTestObjectSwiftKotlinSpec.hpp @@ -104,6 +104,7 @@ namespace margelo::nitro::image { void callAll(const std::function& first, const std::function& second, const std::function& third) override; void callWithOptional(std::optional value, const std::function /* maybe */)>& callback) override; std::shared_ptr> callSumUpNTimes(const std::function>()>& callback, double n) override; + std::shared_ptr> callbackAsyncPromise(const std::function>>>()>& callback) override; std::shared_ptr> getValueFromJSCallbackAndWait(const std::function>()>& getValue) override; std::shared_ptr> getValueFromJsCallback(const std::function>()>& callback, const std::function& andThenCall) override; Car getCar() override; diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.kt b/packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.kt new file mode 100644 index 000000000..989d47190 --- /dev/null +++ b/packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.kt @@ -0,0 +1,46 @@ +/// +/// Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.kt +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © 2024 Marc Rousavy @ Margelo +/// + +package com.margelo.nitro.image + +import androidx.annotation.Keep +import com.facebook.jni.HybridData +import com.facebook.proguard.annotations.DoNotStrip +import com.margelo.nitro.core.* +import dalvik.annotation.optimization.FastNative + +/** + * Represents the JavaScript callback `() => std::shared_ptr>>>`. + * This is implemented in C++, via a `std::function<...>`. + */ +@DoNotStrip +@Keep +@Suppress("RedundantSuppression", "ConvertSecondaryConstructorToPrimary", "RedundantUnitReturnType", "KotlinJniMissingFunction", "ClassName", "unused") +class Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ { + @DoNotStrip + @Keep + private val mHybridData: HybridData + + @DoNotStrip + @Keep + private constructor(hybridData: HybridData) { + mHybridData = hybridData + } + + /** + * Converts this function to a Kotlin Lambda. + * This exists purely as syntactic sugar, and has minimal runtime overhead. + */ + fun toLambda(): () -> Promise> = this::call + + /** + * Call the given JS callback. + * @throws Throwable if the JS function itself throws an error, or if the JS function/runtime has already been deleted. + */ + @FastNative + external fun call(): Promise> +} diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridTestObjectSwiftKotlinSpec.kt b/packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridTestObjectSwiftKotlinSpec.kt index f92dce3be..d0c5f55e0 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridTestObjectSwiftKotlinSpec.kt +++ b/packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridTestObjectSwiftKotlinSpec.kt @@ -266,6 +266,17 @@ abstract class HybridTestObjectSwiftKotlinSpec: HybridObject() { return __result } + @DoNotStrip + @Keep + abstract fun callbackAsyncPromise(callback: () -> Promise>): Promise + + @DoNotStrip + @Keep + private fun callbackAsyncPromise(callback: Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____): Promise { + val __result = callbackAsyncPromise(callback.toLambda()) + return __result + } + @DoNotStrip @Keep abstract fun getValueFromJSCallbackAndWait(getValue: () -> Promise): Promise diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.cpp b/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.cpp index f8ab3605c..b8a327634 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.cpp @@ -138,6 +138,23 @@ namespace margelo::nitro::image::bridge::swift { }; } + // pragma MARK: std::function>>>()> + Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ create_Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(void* _Nonnull swiftClosureWrapper) { + auto swiftClosure = NitroImage::Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____::fromUnsafe(swiftClosureWrapper); + return [swiftClosure = std::move(swiftClosure)]() mutable -> std::shared_ptr>>> { + auto __result = swiftClosure.call(); + return __result; + }; + } + + // pragma MARK: std::function>& /* result */)> + Func_void_std__shared_ptr_Promise_double__ create_Func_void_std__shared_ptr_Promise_double__(void* _Nonnull swiftClosureWrapper) { + auto swiftClosure = NitroImage::Func_void_std__shared_ptr_Promise_double__::fromUnsafe(swiftClosureWrapper); + return [swiftClosure = std::move(swiftClosure)](const std::shared_ptr>& result) mutable -> void { + swiftClosure.call(result); + }; + } + // pragma MARK: std::function>()> Func_std__shared_ptr_Promise_std__string__ create_Func_std__shared_ptr_Promise_std__string__(void* _Nonnull swiftClosureWrapper) { auto swiftClosure = NitroImage::Func_std__shared_ptr_Promise_std__string__::fromUnsafe(swiftClosureWrapper); diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.hpp b/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.hpp index 881dd86c2..dbaca0d69 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/NitroImage-Swift-Cxx-Bridge.hpp @@ -530,6 +530,63 @@ namespace margelo::nitro::image::bridge::swift { return Func_std__shared_ptr_Promise_double___Wrapper(std::move(value)); } + // pragma MARK: std::function>>>()> + /** + * Specialized version of `std::function>>>()>`. + */ + using Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ = std::function>>>()>; + /** + * Wrapper class for a `std::function>>>()>`, this can be used from Swift. + */ + class Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double_____Wrapper final { + public: + explicit Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double_____Wrapper(std::function>>>()>&& func): _function(std::make_shared>>>()>>(std::move(func))) {} + inline std::shared_ptr>>> call() const { + auto __result = _function->operator()(); + return __result; + } + private: + std::shared_ptr>>>()>> _function; + }; + Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ create_Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(void* _Nonnull swiftClosureWrapper); + inline Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double_____Wrapper wrap_Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ value) { + return Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double_____Wrapper(std::move(value)); + } + + // pragma MARK: std::shared_ptr>>> + /** + * Specialized version of `std::shared_ptr>>>`. + */ + using std__shared_ptr_Promise_std__shared_ptr_Promise_double____ = std::shared_ptr>>>; + inline std::shared_ptr>>> create_std__shared_ptr_Promise_std__shared_ptr_Promise_double____() { + return Promise>>::create(); + } + inline PromiseHolder>> wrap_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(std::shared_ptr>>> promise) { + return PromiseHolder>>(std::move(promise)); + } + + // pragma MARK: std::function>& /* result */)> + /** + * Specialized version of `std::function>&)>`. + */ + using Func_void_std__shared_ptr_Promise_double__ = std::function>& /* result */)>; + /** + * Wrapper class for a `std::function>& / * result * /)>`, this can be used from Swift. + */ + class Func_void_std__shared_ptr_Promise_double___Wrapper final { + public: + explicit Func_void_std__shared_ptr_Promise_double___Wrapper(std::function>& /* result */)>&& func): _function(std::make_shared>& /* result */)>>(std::move(func))) {} + inline void call(std::shared_ptr> result) const { + _function->operator()(result); + } + private: + std::shared_ptr>& /* result */)>> _function; + }; + Func_void_std__shared_ptr_Promise_double__ create_Func_void_std__shared_ptr_Promise_double__(void* _Nonnull swiftClosureWrapper); + inline Func_void_std__shared_ptr_Promise_double___Wrapper wrap_Func_void_std__shared_ptr_Promise_double__(Func_void_std__shared_ptr_Promise_double__ value) { + return Func_void_std__shared_ptr_Promise_double___Wrapper(std::move(value)); + } + // pragma MARK: std::function>()> /** * Specialized version of `std::function>()>`. diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridTestObjectSwiftKotlinSpecSwift.hpp b/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridTestObjectSwiftKotlinSpecSwift.hpp index c2ea565a1..2f449a724 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridTestObjectSwiftKotlinSpecSwift.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridTestObjectSwiftKotlinSpecSwift.hpp @@ -391,6 +391,14 @@ namespace margelo::nitro::image { auto __value = std::move(__result.value()); return __value; } + inline std::shared_ptr> callbackAsyncPromise(const std::function>>>()>& callback) override { + auto __result = _swiftPart.callbackAsyncPromise(callback); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } inline std::shared_ptr> getValueFromJSCallbackAndWait(const std::function>()>& getValue) override { auto __result = _swiftPart.getValueFromJSCallbackAndWait(getValue); if (__result.hasError()) [[unlikely]] { diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.swift new file mode 100644 index 000000000..09980f873 --- /dev/null +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.swift @@ -0,0 +1,61 @@ +/// +/// Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____.swift +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © 2024 Marc Rousavy @ Margelo +/// + +import NitroModules + +/** + * Wraps a Swift `(() -> Promise>)` as a class. + * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`. + */ +public final class Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ { + public typealias bridge = margelo.nitro.image.bridge.swift + + private let closure: (() -> Promise>) + + public init(_ closure: @escaping (() -> Promise>)) { + self.closure = closure + } + + @inline(__always) + public func call() -> bridge.std__shared_ptr_Promise_std__shared_ptr_Promise_double____ { + let __result: Promise> = self.closure() + return { () -> bridge.std__shared_ptr_Promise_std__shared_ptr_Promise_double____ in + let __promise = bridge.create_std__shared_ptr_Promise_std__shared_ptr_Promise_double____() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(__promise) + __result + .then({ __result in __promiseHolder.resolve({ () -> bridge.std__shared_ptr_Promise_double__ in + let __promise = bridge.create_std__shared_ptr_Promise_double__() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_double__(__promise) + __result + .then({ __result in __promiseHolder.resolve(__result) }) + .catch({ __error in __promiseHolder.reject(__error.toCpp()) }) + return __promise + }()) }) + .catch({ __error in __promiseHolder.reject(__error.toCpp()) }) + return __promise + }() + } + + /** + * Casts this instance to a retained unsafe raw pointer. + * This acquires one additional strong reference on the object! + */ + @inline(__always) + public func toUnsafe() -> UnsafeMutableRawPointer { + return Unmanaged.passRetained(self).toOpaque() + } + + /** + * Casts an unsafe pointer to a `Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____`. + * The pointer has to be a retained opaque `Unmanaged`. + * This removes one strong reference from the object! + */ + @inline(__always) + public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____ { + return Unmanaged.fromOpaque(pointer).takeRetainedValue() + } +} diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_double__.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_double__.swift new file mode 100644 index 000000000..e51aa3dac --- /dev/null +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_double__.swift @@ -0,0 +1,66 @@ +/// +/// Func_void_std__shared_ptr_Promise_double__.swift +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © 2024 Marc Rousavy @ Margelo +/// + +import NitroModules + +/** + * Wraps a Swift `((_ value: Promise) -> Void)` as a class. + * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`. + */ +public final class Func_void_std__shared_ptr_Promise_double__ { + public typealias bridge = margelo.nitro.image.bridge.swift + + private let closure: ((_ value: Promise) -> Void) + + public init(_ closure: @escaping ((_ value: Promise) -> Void)) { + self.closure = closure + } + + @inline(__always) + public func call(value: bridge.std__shared_ptr_Promise_double__) -> Void { + self.closure({ () -> Promise in + let __promise = Promise() + let __resolver = { (__result: Double) in + __promise.resolve(withResult: __result) + } + let __rejecter = { (__error: Error) in + __promise.reject(withError: __error) + } + let __resolverCpp = { () -> bridge.Func_void_double in + let __closureWrapper = Func_void_double(__resolver) + return bridge.create_Func_void_double(__closureWrapper.toUnsafe()) + }() + let __rejecterCpp = { () -> bridge.Func_void_std__exception_ptr in + let __closureWrapper = Func_void_std__exception_ptr(__rejecter) + return bridge.create_Func_void_std__exception_ptr(__closureWrapper.toUnsafe()) + }() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_double__(value) + __promiseHolder.addOnResolvedListenerCopy(__resolverCpp) + __promiseHolder.addOnRejectedListener(__rejecterCpp) + return __promise + }()) + } + + /** + * Casts this instance to a retained unsafe raw pointer. + * This acquires one additional strong reference on the object! + */ + @inline(__always) + public func toUnsafe() -> UnsafeMutableRawPointer { + return Unmanaged.passRetained(self).toOpaque() + } + + /** + * Casts an unsafe pointer to a `Func_void_std__shared_ptr_Promise_double__`. + * The pointer has to be a retained opaque `Unmanaged`. + * This removes one strong reference from the object! + */ + @inline(__always) + public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__shared_ptr_Promise_double__ { + return Unmanaged.fromOpaque(pointer).takeRetainedValue() + } +} diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift index 5db0b8460..b6efb0c0c 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift @@ -55,6 +55,7 @@ public protocol HybridTestObjectSwiftKotlinSpec_protocol: AnyObject { func callAll(first: @escaping (() -> Void), second: @escaping (() -> Void), third: @escaping (() -> Void)) throws -> Void func callWithOptional(value: Double?, callback: @escaping ((_ maybe: Double?) -> Void)) throws -> Void func callSumUpNTimes(callback: @escaping (() -> Promise), n: Double) throws -> Promise + func callbackAsyncPromise(callback: @escaping (() -> Promise>)) throws -> Promise func getValueFromJSCallbackAndWait(getValue: @escaping (() -> Promise)) throws -> Promise func getValueFromJsCallback(callback: @escaping (() -> Promise), andThenCall: @escaping ((_ valueFromJs: String) -> Void)) throws -> Promise func getCar() throws -> Car diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec_cxx.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec_cxx.swift index 7a102a67c..07a4f36e5 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec_cxx.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec_cxx.swift @@ -905,6 +905,51 @@ public class HybridTestObjectSwiftKotlinSpec_cxx { } } + @inline(__always) + public func callbackAsyncPromise(callback: bridge.Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____) -> bridge.Result_std__shared_ptr_Promise_double___ { + do { + let __result = try self.__implementation.callbackAsyncPromise(callback: { () -> (() -> Promise>) in + let __wrappedFunction = bridge.wrap_Func_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(callback) + return { () -> Promise> in + let __result = __wrappedFunction.call() + return { () -> Promise> in + let __promise = Promise>() + let __resolver = { (__result: Promise) in + __promise.resolve(withResult: __result) + } + let __rejecter = { (__error: Error) in + __promise.reject(withError: __error) + } + let __resolverCpp = { () -> bridge.Func_void_std__shared_ptr_Promise_double__ in + let __closureWrapper = Func_void_std__shared_ptr_Promise_double__(__resolver) + return bridge.create_Func_void_std__shared_ptr_Promise_double__(__closureWrapper.toUnsafe()) + }() + let __rejecterCpp = { () -> bridge.Func_void_std__exception_ptr in + let __closureWrapper = Func_void_std__exception_ptr(__rejecter) + return bridge.create_Func_void_std__exception_ptr(__closureWrapper.toUnsafe()) + }() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__shared_ptr_Promise_double____(__result) + __promiseHolder.addOnResolvedListener(__resolverCpp) + __promiseHolder.addOnRejectedListener(__rejecterCpp) + return __promise + }() + } + }()) + let __resultCpp = { () -> bridge.std__shared_ptr_Promise_double__ in + let __promise = bridge.create_std__shared_ptr_Promise_double__() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_double__(__promise) + __result + .then({ __result in __promiseHolder.resolve(__result) }) + .catch({ __error in __promiseHolder.reject(__error.toCpp()) }) + return __promise + }() + return bridge.create_Result_std__shared_ptr_Promise_double___(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_Promise_double___(__exceptionPtr) + } + } + @inline(__always) public func getValueFromJSCallbackAndWait(getValue: bridge.Func_std__shared_ptr_Promise_double__) -> bridge.Result_std__shared_ptr_Promise_double___ { do { diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.cpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.cpp index aa69c3c4c..b26ce76fd 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.cpp @@ -77,6 +77,7 @@ namespace margelo::nitro::image { prototype.registerHybridMethod("callAll", &HybridTestObjectCppSpec::callAll); prototype.registerHybridMethod("callWithOptional", &HybridTestObjectCppSpec::callWithOptional); prototype.registerHybridMethod("callSumUpNTimes", &HybridTestObjectCppSpec::callSumUpNTimes); + prototype.registerHybridMethod("callbackAsyncPromise", &HybridTestObjectCppSpec::callbackAsyncPromise); prototype.registerHybridMethod("getValueFromJSCallbackAndWait", &HybridTestObjectCppSpec::getValueFromJSCallbackAndWait); prototype.registerHybridMethod("getValueFromJsCallback", &HybridTestObjectCppSpec::getValueFromJsCallback); prototype.registerHybridMethod("getCar", &HybridTestObjectCppSpec::getCar); diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.hpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.hpp index da3feec32..397833656 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectCppSpec.hpp @@ -144,6 +144,7 @@ namespace margelo::nitro::image { virtual void callAll(const std::function& first, const std::function& second, const std::function& third) = 0; virtual void callWithOptional(std::optional value, const std::function /* maybe */)>& callback) = 0; virtual std::shared_ptr> callSumUpNTimes(const std::function>()>& callback, double n) = 0; + virtual std::shared_ptr> callbackAsyncPromise(const std::function>>>()>& callback) = 0; virtual std::shared_ptr> getValueFromJSCallbackAndWait(const std::function>()>& getValue) = 0; virtual std::shared_ptr> getValueFromJsCallback(const std::function>()>& callback, const std::function& andThenCall) = 0; virtual Car getCar() = 0; diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.cpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.cpp index b462bb6cd..73e25b491 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.cpp @@ -68,6 +68,7 @@ namespace margelo::nitro::image { prototype.registerHybridMethod("callAll", &HybridTestObjectSwiftKotlinSpec::callAll); prototype.registerHybridMethod("callWithOptional", &HybridTestObjectSwiftKotlinSpec::callWithOptional); prototype.registerHybridMethod("callSumUpNTimes", &HybridTestObjectSwiftKotlinSpec::callSumUpNTimes); + prototype.registerHybridMethod("callbackAsyncPromise", &HybridTestObjectSwiftKotlinSpec::callbackAsyncPromise); prototype.registerHybridMethod("getValueFromJSCallbackAndWait", &HybridTestObjectSwiftKotlinSpec::getValueFromJSCallbackAndWait); prototype.registerHybridMethod("getValueFromJsCallback", &HybridTestObjectSwiftKotlinSpec::getValueFromJsCallback); prototype.registerHybridMethod("getCar", &HybridTestObjectSwiftKotlinSpec::getCar); diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.hpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.hpp index bb2462db1..fca5c2871 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSwiftKotlinSpec.hpp @@ -134,6 +134,7 @@ namespace margelo::nitro::image { virtual void callAll(const std::function& first, const std::function& second, const std::function& third) = 0; virtual void callWithOptional(std::optional value, const std::function /* maybe */)>& callback) = 0; virtual std::shared_ptr> callSumUpNTimes(const std::function>()>& callback, double n) = 0; + virtual std::shared_ptr> callbackAsyncPromise(const std::function>>>()>& callback) = 0; virtual std::shared_ptr> getValueFromJSCallbackAndWait(const std::function>()>& getValue) = 0; virtual std::shared_ptr> getValueFromJsCallback(const std::function>()>& callback, const std::function& andThenCall) = 0; virtual Car getCar() = 0; diff --git a/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts b/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts index 27ee3cee6..186844857 100644 --- a/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts +++ b/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts @@ -104,6 +104,7 @@ interface SharedTestObjectProps { callback: (maybe: number | undefined) => void ): void callSumUpNTimes(callback: () => number, n: number): Promise + callbackAsyncPromise(callback: () => Promise): Promise // Callbacks that return values getValueFromJSCallbackAndWait(getValue: () => number): Promise