Aria 2.0.0
C++23 MVVM framework (C++20 minimum) — reactive, coroutine-first, ABI-layered
Loading...
Searching...
No Matches
aria::async::AsyncResource< T, Key > Class Template Reference

#include <async_resource.hpp>

Public Types

using Fetcher = std::function<Task<T>(Key)>

Public Member Functions

 AsyncResource (IExecutor &ui, IExecutor &worker, Fetcher fetcher)
 ~AsyncResource ()
 AsyncResource (const AsyncResource &)=delete
AsyncResourceoperator= (const AsyncResource &)=delete
void fetch (Key key)
 Fetch for key.
void refresh ()
void invalidate () noexcept
void clear ()
void cancel ()
 Cancel any in-flight fetch and drop its pending write-back, WITHOUT destroying the resource.
bool has_data () const

Public Attributes

Property< bool > & is_loading
Property< std::optional<::aria::Error > > & error
Property< std::string > & error_message
Property< std::optional< T > > & data
Property<::aria::Loadable< T > > & loadable
 Five-state loadable view-model – Idle / Loading / Refreshing / Success / Error.

Member Typedef Documentation

◆ Fetcher

template<typename T, typename Key = int>
using aria::async::AsyncResource< T, Key >::Fetcher = std::function<Task<T>(Key)>

Constructor & Destructor Documentation

◆ AsyncResource() [1/2]

template<typename T, typename Key = int>
aria::async::AsyncResource< T, Key >::AsyncResource ( IExecutor & ui,
IExecutor & worker,
Fetcher fetcher )
inline

◆ ~AsyncResource()

template<typename T, typename Key = int>
aria::async::AsyncResource< T, Key >::~AsyncResource ( )
inline

◆ AsyncResource() [2/2]

template<typename T, typename Key = int>
aria::async::AsyncResource< T, Key >::AsyncResource ( const AsyncResource< T, Key > & )
delete

Member Function Documentation

◆ operator=()

template<typename T, typename Key = int>
AsyncResource & aria::async::AsyncResource< T, Key >::operator= ( const AsyncResource< T, Key > & )
delete

◆ fetch()

template<typename T, typename Key = int>
void aria::async::AsyncResource< T, Key >::fetch ( Key key)
inline

Fetch for key.

If the same key already has fresh data and no invalidate() has been called since, this is a no-op. If a fetch for the same key is already in-flight, it's deduped.

◆ refresh()

template<typename T, typename Key = int>
void aria::async::AsyncResource< T, Key >::refresh ( )
inline

◆ invalidate()

template<typename T, typename Key = int>
void aria::async::AsyncResource< T, Key >::invalidate ( )
inlinenoexcept

◆ clear()

template<typename T, typename Key = int>
void aria::async::AsyncResource< T, Key >::clear ( )
inline

◆ cancel()

template<typename T, typename Key = int>
void aria::async::AsyncResource< T, Key >::cancel ( )
inline

Cancel any in-flight fetch and drop its pending write-back, WITHOUT destroying the resource.

This is the third lifetime axis from the cancellation model (see docs/reference/lifecycle.md L-38 and ROADMAP P1-H): wire it to a view-destroy hook so navigating away mid-request stops the coroutine from resuming against a dead view.

AsyncResource<UserProfile> profile{ui, net, fetch};
engine.bind_view_lifetime(view, [&profile]{ profile.cancel(); });

Stale-while-revalidate is preserved: the last successful data is kept (only the loading flag is cleared), so a re-mounted view still renders the previous value while a fresh fetch()/refresh() runs. The resource remains fully usable afterwards — a cancelled CancellationSource stays cancelled forever, so we re-arm a fresh one here.

Thread: must be called on the graph thread (it writes the observable Properties), exactly like clear() / fetch().

◆ has_data()

template<typename T, typename Key = int>
bool aria::async::AsyncResource< T, Key >::has_data ( ) const
inlinenodiscard

Member Data Documentation

◆ is_loading

template<typename T, typename Key = int>
Property<bool>& aria::async::AsyncResource< T, Key >::is_loading

◆ error

template<typename T, typename Key = int>
Property<std::optional<::aria::Error> >& aria::async::AsyncResource< T, Key >::error

◆ error_message

template<typename T, typename Key = int>
Property<std::string>& aria::async::AsyncResource< T, Key >::error_message

◆ data

template<typename T, typename Key = int>
Property<std::optional<T> >& aria::async::AsyncResource< T, Key >::data

◆ loadable

template<typename T, typename Key = int>
Property<::aria::Loadable<T> >& aria::async::AsyncResource< T, Key >::loadable

Five-state loadable view-model – Idle / Loading / Refreshing / Success / Error.

Always consistent with the four primitive Properties above. UI code typically prefers this surface (one bind, one switch) over the four-handle surface.


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