UseAsyncState
UseAsyncState
is a hook that allows to declare state variables and manipulate its value
asynchronously, which in turn notifies about its changes to listeners.
Syntax
UseAsyncState
accepts these arguments:
initialValue
: Initial value ofT
type that it will hold.-
asyncFunction
: A function that returns aFuture<T>
to update the state asynchronously. This function is called by theresolve
method and sets thevalue
property.
Properties & Methods
UseAsyncState
provides the following properties and methods:
value
: A getter that allows to read its state.status
: A getter that allows to read its status. It can be:UseAsyncStateStatus.standby
: Represents the standby status, indicating that the state is idle.UseAsyncStateStatus.loading
: Denotes the loading status, indicating that an asynchronous operation is in progress.UseAsyncStateStatus.done
: Indicates that the asynchronous operation has been successfully completed.UseAsyncStateStatus.error
: Signifies that an error has occurred during the asynchronous operation.
error
: A getter that allows to get the error object when theasyncFunction
fails.-
resolve
: A method that updates the state asynchronously by calling theasyncFunction
function.- Syntax:
-
when
: A method that allows to computed a value depending on its status.- Syntax:
- Arguments:
-
standby
: A function that returns a value when the state isUseAsyncStateStatus.standby
. -
loading
: A function that returns a value when the state isUseAsyncStateStatus.loading
. -
done
: A function that returns a value when the state isUseAsyncStateStatus.done
. -
error
: A function that returns a value when the state isUseAsyncStateStatus.error
.
-
-
Methods inherited from
RtState
(Learn more here):-
update
: A method to notify changes after run a set of instructions. -
refresh
: A method to force to notify changes. - *
bind
: A method to bind an instance to it. - *
unbind
: A method to unbind an instance to it. - *
dispose
: A method to remove all listeners and free resources.
-
Usage
Declaration
UseAsyncState
can be initialized using the constructor class:
Resolving & reading the state
UseAsyncState
has a resolve
method that updates the state asynchronously by calling the asyncFunction
function.
After resolving the state, you can read the value
, like this:
Using with argument
UseAsyncState
can be used with arguments using the withArg
constructor:
To resolve the state with an argument, you can use the resolve
method with the argument.
After resolving the state, you can read the value
, like this:
If you want to add more arguments, you can supply it using the Record
(if your proyect support)
or Args
(A generic arguments provided by Reactter), e.g.:
Using with Memo
UseAsyncState
does not cache the resolving value
, meaning that it will resolve the value
every time resolve
is called, potentially impacting performance, especially if the asyncFunction
is expensive. In this case, you should consider using Memo
to cache the resolving value
, e.g.:
Using when
method
UseAsyncState
provides a when
method that allows to computed a value depending on its status:
Updating the value
Use update
method to notify changes after run a set of instructions:
Use refresh
method to force to notify changes.
Listening to changes
When value
has changed, the UseAsyncState
will emit the following events(learn about it here):
Lifecycle.willUpdate
event is triggered before thevalue
change orupdate
,refresh
methods have been invoked.Lifecycle.didUpdate
event is triggered after thevalue
change orupdate
,refresh
methods have been invoked.
Example of listening to changes: