A simple Event Listener for JavaScript projects
yarn add @nguyentc21/event-listener
Method | Return | description |
---|---|---|
addListener on | EventId | undefined | return undefined means error |
removeListener rm | boolean | true on success otherwise false |
removeAllListeners rmAll | undefined | |
emitListener emit | any | |
getListenerList | ({ id: EventId, description?: string } | undefined)[] |
import EventListener from '@nguyentc21/event-listener';
type EventDataType = {
'open-modal': ModalDataType;
'open-toast': undefined;
/// ...
};
const MyEventListener = new EventListener<EventDataType>('MY-LISTENERS');
export default MyEventListener;
// ...
import MyEventListener from '../MyEventListener';
// ...
const MyToast = (props: Props) => {
// ...
useEffect(() => {
// Add event listener "open-toast"
MyEventListener.on('open-toast', (data) => {
// ... Do "open-toast" action
});
() => {
// Remove event listener "open-toast"
MyEventListener.rm('open-toast');
};
}, []);
};
// ...
// ...
const OtherView = (props: Props) => {
// ...
const _openToast = () => {
MyEventListener.emit('open-toast', 'Hello');
};
};
// ...
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT