1.0.2 • Published 1 month ago

@nguyentc21/event-listener v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

@nguyentc21/event-listener

A simple Event Listener for JavaScript projects

Installation

yarn add @nguyentc21/event-listener

Usage

MethodReturndescription
addListener onEventId | undefinedreturn undefined means error
removeListener rmbooleantrue on success otherwise false
removeAllListeners rmAllundefined
emitListener emitany
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');
  };
};
// ...

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT