mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 08:46:44 +08:00
Pass promises to type erased continuables instead of the raw callbacks
This commit is contained in:
parent
a685d9234a
commit
6b9efad602
@ -52,24 +52,15 @@ namespace cti {
|
||||
template <template <typename...> class CallbackWrapper,
|
||||
template <typename...> class ContinuationWrapper, typename... Args>
|
||||
struct continuable_trait {
|
||||
/// The callback type which is passed to continuations
|
||||
// TODO This should be void(promise<...>) I think
|
||||
using callback =
|
||||
CallbackWrapper<void(Args...), void(detail::types::dispatch_error_tag,
|
||||
detail::types::error_type)>;
|
||||
|
||||
/// The continuation type which is used to erase the internal data.
|
||||
using continuation = ContinuationWrapper<void(
|
||||
CallbackWrapper<void(Args...), void(detail::types::dispatch_error_tag,
|
||||
detail::types::error_type)>)>;
|
||||
|
||||
/// The promise type which is used to resolve continuations
|
||||
using promise =
|
||||
promise_base<callback, detail::hints::signature_hint_tag<Args...>>;
|
||||
using promise = promise_base<
|
||||
CallbackWrapper<void(Args...), void(detail::types::dispatch_error_tag,
|
||||
detail::types::error_type)>,
|
||||
detail::hints::signature_hint_tag<Args...>>;
|
||||
|
||||
/// The continuable type for the given parameters.
|
||||
using continuable =
|
||||
continuable_base<continuation,
|
||||
continuable_base<ContinuationWrapper<void(promise)>,
|
||||
detail::hints::signature_hint_tag<Args...>>;
|
||||
};
|
||||
} // namespace cti
|
||||
|
||||
@ -41,7 +41,7 @@ namespace cti {
|
||||
namespace detail {
|
||||
template<typename... Args>
|
||||
using trait_of = continuable_trait<
|
||||
fu2::function,
|
||||
fu2::unique_function,
|
||||
fu2::function,
|
||||
Args...
|
||||
>;
|
||||
|
||||
@ -35,16 +35,6 @@ static cti::continuable<std::string> http_request(std::string url) {
|
||||
};
|
||||
}
|
||||
|
||||
static cti::continuable<std::string> http_request3(std::string url) {
|
||||
return [url = std::move(url)](auto promise) {
|
||||
if (false) {
|
||||
promise.set_value("");
|
||||
promise("");
|
||||
}
|
||||
promise.set_exception(std::error_condition{});
|
||||
};
|
||||
}
|
||||
|
||||
static auto http_request2(std::string url) {
|
||||
return cti::make_continuable<std::string>(
|
||||
// ...
|
||||
@ -57,6 +47,16 @@ static auto http_request2(std::string url) {
|
||||
});
|
||||
}
|
||||
|
||||
static cti::continuable<std::string> http_request3(std::string url) {
|
||||
return [url = std::move(url)](auto&& promise) {
|
||||
if (false) {
|
||||
promise.set_value("");
|
||||
promise("");
|
||||
}
|
||||
promise.set_exception(std::error_condition{});
|
||||
};
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
http_request("github.com")
|
||||
.then([](std::string) {
|
||||
@ -82,7 +82,7 @@ int main(int, char**) {
|
||||
// ...
|
||||
});
|
||||
|
||||
(http_request("github.com") && http_request("github.com"))
|
||||
(http_request("github.com") && http_request3("github.com"))
|
||||
.then([](std::string, std::string) {
|
||||
// ...
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user