JS Event System with redux integration
yarn add eventsy
import React from 'react';
import {useListener} from 'eventsy';
const foo = () => {
useListener('foo/bar', data => {
// react to 'foo/bar' action
}, [/* anyValuesItShouldUpdateOn*/]);
return (
<div></div>
)
}
export default foo;
...
import Event from 'eventsy';
class Foo extends Component {
componentDidMount() {
this.listener = Event.sub('foo', this.handleFoo);
}
componentWillUnmount() {
Event.unsub(this.listener);
}
somethingThatTriggeredFoo = () => {
Event.publish('foo', data);
}
handleFoo = () => {
// Run your code here
};
}
If you want the Event system to listen to and broadcast redux actions just import the redux middleware.
//store.js
import {eventSystemReduxMiddleware} from 'eventsy';
const store = createStore(
reducers,
applyMiddleware(eventSystemReduxMiddleware)
);
Now you can execute code in your components based on dispatched redux actions
event
(string|array) supports either a string or an array of strings that will all execute the same callback.callback
(function) expects a function that will be executed when the event triggersreturns
(string) the method will return a subscription key that can be used to unsubscribe
- shorthand version of
subscribe()
key
(string|array) subscription key (or array of keys) returned fromsubscribe
- shorthand version of
unsubscribe()
event
(string) event that will be broadcasted to all relevant subscriberspayload
(mixed) payload that will be passed to the subscribers callback
- clears all subscribers
3 different wildcards levels are supported
- Would trigger on
login
,login/success
,login/failed
,login/failed/wrong_password
- Would trigger on
login/success
,login/failed
... - Would not trigger on
login
,login/failed/wrong_pass
- Would trigger on
login/success
,login/failed
,login/failed/wrong_password
- Would not trigger on
login