revived.store) module documentation¶
This module implements the global state store, and the
INIT action and
action_creator. This is the entry point of the revived module.
Rationale behind the
revived.store.Store is the object that brings
reducers. The store has the following responsibilities:
- Holds application state;
- Allows access to state via
- Allows state to be updated via
- Registers listeners via
- Handles unregistering of listeners via the function returned by
revived.store.Store.subscribeor via the property
It’s important to note that you’ll only have a single store in your application.
When you want to split your data handling logic, you’ll use
composition instead of many stores.
# create the store object store = Store(root_reducer) # register subscribers # ... # dispatch an action using the action_creator <an_action_creator> store.dispatch(an_action_creator(a_parameter, another_parameter))
Subscribe and unsubscribe to state changes¶
There are two ways to subscribe and usubscribe to store changes: using
revived.store.Store.subscribe method or the
revived.store.Store.subscriber decorator. Both approaches are equivalent
and the choice should be just made based on your taste.
# create the store object store = Store(root_reducer) # define the function def a_subscriber(): # do something! pass # subscribe the function unsubscribe = store.subscribe(a_subscriber) # unsubscribe the function unsubscribe()
# create the store object store = Store(root_reducer) # define and subscribe the function @store.subscriber def a_subscriber(): # do something! pass # unsubscribe the function a_subscriber.unsubscribe()
Action types for the store module.
Basically the only type here is the
INITone. Reducers should wait for this action to create the initial state for the state subpath they are responsible of.
revived.store.Store.dispatchis called in a reducer.
Container object for the global state.
This object is responsible of the global state. Its main responsibilities are:
- Keeping track of all the subscribers, and call them on state changes.
- Keeping reference to the reducer to be used and call it to properly handle state changes.
Creates the store, using the given function as
reducer. At the beginning no callback is subscribed to store changes. It is possible to add subscribers later, while there is no way - at the moment - to replace the reducer.
Parameters: reducer (
Module]) – The root reducer.
This is the only piece of code responsible of dispatching actions. When an
actionis dispatched, the state is changed according to the defined root reducer and all the subscribers are called.
The calling order is not guaranteed.
Parameters: action (
Action) – The
actionthat should be dispatched.
Return type: None
Getter for the global state.
Returns: The global state contained into the store.
Subscribes a callback to state changes.
Every time the state changes, the callback is called. No parameters are passed to the callback. It is responsibility of the callback to actually connect the store with the caller. The returned function can be called without arguments to unsubscribe the callback.
Parameters: callback (
None]) – The callback to be subscribed.
Returns: The unsubscribe function.
Decorator function to subscribe a function to store changes.
The subscribed function will be called every time the internal state of the store changes.
NOTE: The decorator function will return the function itself. To unsubscribe the callback the user should use the
revived.store.Subscriber.unsubscribefunction attached into the callback.
Parameters: callback (
None]) – The callback to be subscribed. :returns: The callback itself.
Returns: The wrapping subscriber.
Wrapper around a subscriber function with the
While creating a subscriber using the decorator it is not possible to return the
unsubscribefunction. So a
revived.store.Subscriberis created wrapping the callback, that contains the
revived.store.Subscriber.unsubscribefunction to be used to properly unregister the subscriber.
- callback (
None]) – The callback to be wrapped into the subscriber.
- unsubscribe (
None]) – The unsubscribe function for the subscriber.
Property containing the
unsubscribefunction for the subscriber.
- callback (
Action creator for the init action.