rili::CancelablePromise< PromisedType > Class Template Reference
Inheritance diagram for rili::CancelablePromise< PromisedType >:
[legend]
Collaboration diagram for rili::CancelablePromise< PromisedType >:
[legend]

Public Types

typedef std::exception_ptr FailureType
 
typedef std::function< void(std::function< void()>)> OnCancel
 
typedef std::function< void(PromisedType const &)> OnResolveHandler
 
typedef std::function< void(FailureType const &)> OnRejectHandler
 
typedef std::function< void(OnResolveHandler const &, OnCancel const &)> BasicInitializer
 
typedef std::function< void(OnResolveHandler const &, OnRejectHandler const &, OnCancel const &)> ComplexInitializer
 
- Public Types inherited from rili::Promise< PromisedType >
typedef std::exception_ptr FailureType
 
typedef std::function< void(PromisedType const &)> OnResolveHandler
 
typedef std::function< void(FailureType const &)> OnRejectHandler
 
typedef std::function< void(OnResolveHandler const &)> BasicInitializer
 
typedef std::function< void(OnResolveHandler const &, OnRejectHandler const &)> ComplexInitializer
 

Public Member Functions

 CancelablePromise (BasicInitializer const &initializer) noexcept
 
 CancelablePromise (ComplexInitializer const &initializer) noexcept
 
void cancel () noexcept
 
- Public Member Functions inherited from rili::Promise< PromisedType >
 Promise (PromisedType const &promised) noexcept
 
 Promise (FailureType const &rejected) noexcept
 
 Promise (BasicInitializer const &initializer) noexcept
 
 Promise (ComplexInitializer const &initializer) noexcept
 
void resolve (PromisedType const &resolved)
 
void reject (FailureType const &rejected)
 
template<typename OnSuccess , typename PromiseType = typename std::result_of<OnSuccess(PromisedType const&)>::type>
detail::GetPromiseType< PromiseType >::type Then (OnSuccess const &onSuccess) noexcept
 
template<typename OnSuccess , typename OnFailure , typename PromiseType1 = typename std::result_of<OnSuccess(PromisedType const&)>::type, typename PromiseType2 = typename std::result_of<OnFailure(FailureType const&)>::type>
detail::GetPromiseType< PromiseType1 >::type Then (OnSuccess const &onSuccess, OnFailure const &onFailure) noexcept
 
template<typename OnFailure , typename PromiseType = typename std::result_of<OnFailure(FailureType const&)>::type>
detail::GetPromiseType< PromiseType >::type Catch (OnFailure const &onFailure) noexcept
 
template<typename OnCompleted , typename PromiseType = typename std::result_of<OnCompleted()>::type>
detail::GetPromiseType< PromiseType >::type Finally (OnCompleted const &onCompleted) noexcept
 

Additional Inherited Members

- Static Public Member Functions inherited from rili::Promise< PromisedType >
static rili::Promise< std::list< PromisedType > > all (std::list< rili::Promise< PromisedType >> const &promises) noexcept
 
static rili::Promise< PromisedType > any (std::list< rili::Promise< PromisedType >> const &promises) noexcept
 
static rili::Promise< std::shared_ptr< std::list< std::pair< std::unique_ptr< PromisedType >, FailureType > > > > fetch (std::list< rili::Promise< PromisedType >> const &promises) noexcept
 

Detailed Description

template<typename PromisedType>
class rili::CancelablePromise< PromisedType >

rili::CancelablePromise API allow easily chaining asynchronous actions to avoid "callback hell". Major difference between rili::CancelablePromise and A+ JS Promise is chaining behavior using Catch method.

rili::CancelablePromise add to rili::Promise posibility to cancel it. Canceled promise will be rejected with rili::promise::PromiseCanceled error.

Remarks
have specialization for void type: rili::CancelablePromise<void>
is copiable, but copy share state with original instance.
is generally thread safe and instances can be pass throught and shared by multiple threads.
all operations (even Construction) are made asynchronous way and are non blocking.
Warning
if onCancel callback will be used, then registered handler will be invoked in case of any rejection including but ont only in case when rili::CancelablePromise::cancel will be invoked. In addition it will be threated as plain error handler. When registered, if rili::CancelablePromise will be rejected then std::terminate will not be called (even in case when other "outside" handlers are missing).
Note
PromisedType must be copyable and copy assignable.
rili::CancelablePromise use rili::Context aquired during construction to dispatch asynchronously all further tasks(including chained children promises), so thread in which rili::CancelablePromise is created may be important in some cases.
rili::CancelablePromise can be rejected or resolved only once - further reject or resolve will be skipped.
Warning
There cannot be rejected rili::CancelablePromise which don't have (now or in future) rejection handlers, or rili::CancelablePromise, which is not rejected nor resolved. Check for this is made in debug builds (assert is used) in rili::CancelablePromise destructor.
See also
rili::Context
rili::Promise
rili::Promise<void>
rili::CancelablePromise<void>

Member Typedef Documentation

template<typename PromisedType >
typedef std::function<void(OnResolveHandler const&, OnCancel const&)> rili::CancelablePromise< PromisedType >::BasicInitializer

type of user callback for two parameter 'pending' rili::CancelablePromise constructor

template<typename PromisedType >
typedef std::function<void(OnResolveHandler const&, OnRejectHandler const&, OnCancel const&)> rili::CancelablePromise< PromisedType >::ComplexInitializer

type of user callback for three parameter 'pending' rili::CancelablePromise constructor

template<typename PromisedType >
typedef std::exception_ptr rili::CancelablePromise< PromisedType >::FailureType

is type in which all kinds of rejections / exceptions are stored in rili::CancelablePromise

template<typename PromisedType >
typedef std::function<void(std::function<void()>)> rili::CancelablePromise< PromisedType >::OnCancel

can be used to register cancelation handler for pending rili::CancelablePromise

template<typename PromisedType >
typedef std::function<void(FailureType const&)> rili::CancelablePromise< PromisedType >::OnRejectHandler

can be used to reject rili::CancelablePromise

template<typename PromisedType >
typedef std::function<void(PromisedType const&)> rili::CancelablePromise< PromisedType >::OnResolveHandler

can be used to resolve rili::CancelablePromise

Constructor & Destructor Documentation

template<typename PromisedType >
rili::CancelablePromise< PromisedType >::CancelablePromise ( BasicInitializer const &  initializer)
explicitnoexcept

Create 'pending' rili::CancelablePromise.

Call user provided function with callback as paramether which should be used to resolve rili::CancelablePromise and second one, which can be used to register handler for cancelations.

Remarks
User provided callback will be executed asynchronously
is not blocking.
is thread safe.
Note
If user provided function throw, then rili::CancelablePromise is moved to rejected state with error equal to thrown exception.
Warning
If callback 'resolve' will not be called nor exception thrown then rili::CancelablePromise will stay in 'pending' state infinitely which will block rili::Context::run infinitely (eventualy it can be canceled which is equal to 'reject').
Parameters
initializeris function which is executed to resolve or reject rili::CancelablePromise. It provide as parameter another function which can be used to resolve rili::CancelablePromise and second to register cancelation handler.

Example:

rili::CancelablePromise<int> promise([](auto const& resolve, auto const&){ resolve(5); });
template<typename PromisedType >
rili::CancelablePromise< PromisedType >::CancelablePromise ( ComplexInitializer const &  initializer)
explicitnoexcept

Create 'pending' rili::CancelablePromise.

Call user provided function with first parameter as callback which should be used to resolve rili::CancelablePromise, second which should be used to reject it and third which can be used to register handler for cancelations.

Remarks
User provided callback will be executed asynchronously
is not blocking.
is thread safe.
Note
If user provided function throw, then rili::CancelablePromise is moved to rejected state with error equal to thrown exception.
Warning
If callback 'resolve' or 'reject' will not be called nor exception thrown, then rili::CancelablePromise will stay in 'pending' state infinitely, which will block rili::Context::run infinitely (eventualy it can be canceled which is equal to 'reject').
Parameters
initializeris function which is executed to resolve or reject rili::CancelablePromise. It provide as first parameter another function which can be used to resolve rili::CancelablePromise, as second function which can be used to reject rili::CancelablePromise and third to register cancelation handler.

Example:

rili::CancelablePromise<int> promise([](auto const&, auto const& reject, auto const&){
reject(std::make_exception_ptr("Huston, we have a problem!!!"));
});

Member Function Documentation

template<typename PromisedType >
void rili::CancelablePromise< PromisedType >::cancel ( )
inlinenoexcept

can be used to cancel rili::CancelablePromise.

As result it will be immediately rejected with rili::promise::PromiseCanceled error, if is not already 'rejected' or 'resolved'

Remarks
is not blocking.
is thread safe.

The documentation for this class was generated from the following file: