@nguyentc21/event-listener
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@nguyentc21/event-listener

A simple Event Listener for JavaScript projects

Installation

yarn add @nguyentc21/event-listener

Usage

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');
  };
};
// ...

Contributing

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

License

MIT


Package Sidebar

Install

npm i @nguyentc21/event-listener

Weekly Downloads

4

Version

1.0.2

License

MIT

Unpacked Size

20.4 kB

Total Files

10

Last publish

Collaborators

  • nguyentc21