0.1.0 • Published 4 years ago

emyt v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Install

npm:

npm install mitter

Yarn:

yarn add mitter

GitHub:

git clone https://github.com/mazecodes/mitter.git

Usage

Make an instance:

const Mitter = require('mitter');

const mitter = new Mitter();

You can also make Mitter case-insensitive if you want:

const mitter = new Mitter({
  caseSensitive: false,
});

Listen to an event:

mitter.on('event', message => {
  console.log(`Event: ${message}`);
});

You can also attach multiple handlers to an event at the same time:

mitter.on(
  'event',
  () => console.log('Handler 1'),
  () => console.log('Handler 2'),
  () => console.log('Handler 3')
);

Trigger an event:

mitter.emit('event');

You can pass multiple values to handlers:

mitter.emit('event', 'Message 1', 'Message 2');

Remove a specific event handler:

mitter.off('event', handler);

Remove all handlers for an event:

mitter.clear('event');

All events

Listen to all events:

mitter.onAll((type, message) => {
  console.log(`Event type: ${type}`);
});

Trigger all events:

mitter.emitAll();

Remove all events:

mitter.clearAll();

RegExps

You can listen to RegExps to catch specific events:

mitter.on(/^event/, type => {
  console.log(`Event type: ${type}`);
});

You can also trigger events with RegExp:

mitter.emit(/foo$/, 'bar');

Handler Category

Handlers have two categories: normal and regex. You can clear a specific category like this:

mitter.clearAll('normal');

Contributing

All contributions, issues and feature requests are welcome! Please feel free to check issues page.

  1. Fork the project
  2. Create your feature branch (git checkout -b awesome-feature)
  3. Commit your changes (git commit -m "Add Awesome Feature")
  4. Push to the branch (git push origin awesome-feature)
  5. Open a Pull Request

Author

Maze Peterson:

Show your support

Give a ⭐ if you liked this project!

License

MIT © Maze Peterson

0.1.0

4 years ago