mobx-event-bus ·
Tiny library allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another. Based on original implementation by 7upcat, this fork includes Typescript bindings. Additional enhancements are planned in the future.
Installation
npm install mobx-event-bus2 --save
Usage
Basic
{ // Registers subscriber the receive events eventBus } // Mark listener1 as a event subscriber handle all 'topic1' events @ { console } // Mark listener2 as a event subscriber handle 'topic1' events payload match 'Martin' @ { console } const demoStore = // post event with topic 'topic1' and payload 'Tommy'eventBus // post event with topic 'topic1' and payload 'Martin'eventBus // unregister from receiving future eventseventBus;
Integration with react-router
RouterStore.js
{ thishistory } history =
DomainStore.js
{ eventBus } @ { // init data }
Handle Keyboard Events
React Component
// { const handleKeyPress = { eventBus } return <input type="text" id="one" onKeyPress=handleKeyPress />}
DomainStore.js
{ eventBus } @ { // your business code. }
API Documents
eventBus
Dispatch events to listeners, and provides ways for subscribers register themselves.
register(store, options?)
Registers all subscribers methods on store instances to receive events. Accepts optional config with the following options:
Options
fireImmediately
: Boolean that indicates that the subscriber function should immediately be triggered if event for that topic were fired before.false
by default.
unregister(store)
Unregister store from receiving future events.
post(topic , payload)
Posts specified topic events to registered subscribers.
// event data structure topic: 'theTopic' payload: 'the data'
@subscribe
Mark a store class method as an event subscriber.
Usage:
@subscribe (topic, selector)
handler() {
}