Subscribe to store events.
Store
objects fire events before and after each action.
When an action is dispatched, an event object is created and passed to any handlers.
store
(Store): The store object the action was dispatched to.action
(Action): Theaction object
that was dispatched.context
(any): The context that was passed toevents.on
.
Add an event subscriber to a Store object.
-
subscriber
(Object): An object with the following properties:-
handler
(Function): The event handler. This will be called any time a matching action has been called. -
[type]
(String): The action type to listen for. -
[tag]
(any): A reference that can be used when removing event listeners. For example, if I use a string'my-events'
for several events, passing the tag into theevents.off()
function will remove all events with that tag. -
[before=false]
(Boolean): True if the handler should fire before the action is executed. -
[after=true]
(Boolean): True if the handler should fire after the action is executed. -
[bubble=false]
(Boolean): True if the handler should fire for actions meant for child stores. -
[context]
(any): Custom data to be passed to the event handler. Will be accessible throughevent.context
.
-
(subscription): A reference you can pass to events.off
to remove the
handler.
Remove an event subscriber from a Store object.
-
blueprint
(Object): An object with any of the following properties:-
[handler]
(Function): Remove any subscribers with the same handler function. -
[type]
(String): Remove any subscribers with the same type. -
[tag]
(any): Remove any subscribers with the same tag. -
[before=false]
(Boolean): Remove any subscribers with the same value. -
[after=true]
(Boolean): Remove any subscribers with the same value. -
[bubble=false]
(Boolean): Remove any subscribers with the same value.
-
(undefined)
This is a convenience function for adding an event handler for all events, with
bubble
set to true
.
It also fires a special @init
action right after subscribing.
handler
(Function): The event handler.
(subscription): A reference you can pass to events.off
to remove the
handler.
import { Store, snapshot } from 'megalith';
class App extends Store { ... }
const app = new App();
// Fires right away, and after every action
app.events.all(event => renderMyApp(snapshot.create(event.store)));