Action (revived.action
) module documentation¶
This module implements helper functions and classes that can be used to define
actions
and action creators
, in the same fashion of redux ones, but
using decorators instead of anonymous functions.
Actions and action creators¶
Actions are payloads of information that send data from your application to your
store
. They are the only source of information for the store
. You send
them to the store using revived.store.Store.dispatch
.
Actions are instances of revived.action.Action
. They have a type
property. Types should be defined in an enum inheriting
revived.action.ActionType
. Once your app is large enough, you may want to
move them into a separate module.
Action creators are exactly that: functions that create actions
. It’s easy
to conflate the terms action
and action creator
, so do your best to use
the proper term.
Define action types¶
While you are free to define the action type enum as he prefers, it is strongly suggested to write them down in this way:
from revived.actions import ActionType as BaseActionType
# custom action types enum
class ActionType(BaseActionType):
AN_ACTION_TYPE = 'an_action_type'
Define action creators¶
While it is possible to explicitly build revived.action.Action
instances
directly, it is strongly suggested to create actions
using action
creators
.
Assuming you are in the same module of the action types
defined previously,
you can define action creators
in this way:
# define the action creator that takes two arguments and returns a
# dictionary with those arguments in an arbitrary way.
@action(ActionTypes.AN_ACTION_TYPE)
def an_action_type_with_parameters(param1, param2):
return {'1': param1, '2': param2}
# create the action object
action_obj = an_action_type_with_parameters(1, 2)
-
class
revived.action.
Action
(action_type, data=None)[source]¶ Structure that stores all the required data for an action.
Redux actions are plain objects - ie: python dicts - but having a specific class here helps for type hinting. The rationale behind this is that we store the type as
metadata
instead of part of the action data itself.While
action_type
is going to be stored asmetadata
, therevived.action.Action
instance itself is going to behave exactly as a dict, with all the action data inside.Parameters: - action_type (
ActionType
) – The type of the action. - data (
Optional
[Dict
[str
,Any
]]) – An optional dict containing data. No restriction on depth and members type, as long as the keys are strings.
- action_type (
-
class
revived.action.
ActionType
(*args, **kwargs)[source]¶ Action type base class.
The idea behind this class is to use an unique enum to store action types for each module. Usually there would be no need for such a feature-less class, but it is pretty handy while using type hints.
-
revived.action.
action
(action_type)[source]¶ Decorator function to use as an
action creator
factory.This helper function is used to create action creators. The idea behind this is that we just want to define the relevant data as a
dict
, instead of complex objects. This decorator will take care of simple-dict-returning functions preparing the properrevived.action.Action
instance that is needed by the revived API.Parameters: action_type ( ActionType
) – The type of the action.Return type: Callable
[[Callable
],Callable
]Returns: The action creator
.