Skip to content

Latest commit

 

History

History
145 lines (89 loc) · 3.73 KB

events.md

File metadata and controls

145 lines (89 loc) · 3.73 KB

megalith.Store.events

Subscribe to store events.

Store objects fire events before and after each action.

Objects

Functions


Objects

When an action is dispatched, an event object is created and passed to any handlers.

Properties
  • store (Store): The store object the action was dispatched to.
  • action (Action): The action object that was dispatched.
  • context (any): The context that was passed to events.on.

Functions

Add an event subscriber to a Store object.

Arguments
  1. 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 the events.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 through event.context.

Returns

(subscription): A reference you can pass to events.off to remove the handler.


Remove an event subscriber from a Store object.

Arguments
  1. 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.

Returns

(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.

Arguments
  1. handler (Function): The event handler.
Returns

(subscription): A reference you can pass to events.off to remove the handler.

Example
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)));



Documentation