Actions
Introduction
Actions build up the API that will shape how to interact with your Conan State.
Actions & Reducers are concepts strongly interlinked with each other, we would strongly recommend to be familiar with the general concepts as for most use cases you won't need to create custom actions.
ConanJs provides automatically with an action for each reducer, and two out of the box actions update and updateAsap to deal with sync and async updates (explained in further detail later)
Actions are defined when creating state via a function that
Receives a thread to allow you accessing the reducers and other useful resources to describe your actions
A Thread is like a Conan State half baked, it has all you need to add additional logic to your actions, like the getData method, the monitor and the reducers already defined. Everything available from a thread is documented in the API
Returns the actual actions.
Imagine that you want to add an async action in your Conan State using the monitor, this could retrieve an authentication token and then you want to set the property of your state authenticated to the response
Note that this action is handling async operation with the monitor, which is further explained below.
The Actions Interface
Following the same principles as to the reducers interface, having the Actions described as a function that takes a thread and returns the actual Actions was also a meditated decision.
The result of this is that you can define a clear business interface that describes the actions that can be performed against your state, so a little bit of boiler plate upfront saves a lot of boilerplate down the line.
You can access all the actions through the 'do' property of your Conan State
Chaining Actions
Another property of an action in ConanJs is that they are chainable, they return an ASAP of the state this action is going to generate.
Note that this is the case no matter if doing async or sync operations (as long as you use the monitor or other async techniques, like auto-binding)
This helps solving complex logic, for instance
Typescript users should note that they are likely going to have to create two interfaces one for the Reducers and for the Actions.