Skip to content

Krivega/events-constructor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

events-constructor

npm npm bundle size

Class for emitting events

Install

npm

npm install events-constructor

yarn

yarn add events-constructor

Usage

import Events from 'events-constructor';

const eventNames = ['event1', 'event2'];
const events = new Events(initEventNames);

events.on('event1', data => {
  console.log('event1 is called with data:', data);
});

events.trigger('event1', 'some data');

API

events.on

Add listener to event

events.on('event1', data => {
  console.log('event1 is called with data:', data);
});

events.once

Add a listener that will be called only once per event

events.once('event1', data => {
  console.log('event1 is called with data:', data);
});

events.off

Remove listener from event

events.off('event1', data => {
  console.log('event1 is called with data:', data);
});

events.trigger

Trigger event with data

events.trigger('event1', 'some data');

events.removeEventHandlers

Remove all listeners

events.removeEventHandlers();

events.triggers

Get all triggers

events.triggers;

events.deactivate()

Disable event handlers. The trigger method will not produce any effect.

events.on('event1', data => {
  console.log('event1 is called with data:', data); //it's not called
});

events.deactivate();
events.trigger('event1', 'some data');

events.activate()

Enable event handlers.

events.on('event1', data => {
  console.log('event1 is called with data:', data);
});

events.deactivate();
events.trigger('event1', 'some data'); // no effect

events.activate();
events.trigger('event1', 'some data'); // handler is called

events.eachTriggers()

Method for delegate triggers to otherEventsSource(for example)

events.eachTriggers((trigger, eventName) => otherEventsSource.on(eventName, trigger));

Run tests

npm test

Maintainer

👤 Krivega Dmitriy

Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

📝 License

Copyright © 2020 Krivega Dmitriy.
This project is MIT licensed.

About

Events emitter

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages