Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krivega/events-constructor
Events emitter
https://github.com/krivega/events-constructor
Last synced: 26 days ago
JSON representation
Events emitter
- Host: GitHub
- URL: https://github.com/krivega/events-constructor
- Owner: Krivega
- License: mit
- Created: 2020-01-09T10:55:37.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-31T15:00:06.000Z (2 months ago)
- Last Synced: 2024-12-01T15:49:37.371Z (about 1 month ago)
- Language: TypeScript
- Size: 717 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# events-constructor
[![npm](https://img.shields.io/npm/v/events-constructor?style=flat-square)](https://www.npmjs.com/package/events-constructor)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/events-constructor?style=flat-square)Class for emitting events
## Install
npm
```sh
npm install events-constructor
```yarn
```sh
yarn add events-constructor
```## Usage
```js
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
```js
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
```js
events.once('event1', data => {
console.log('event1 is called with data:', data);
});
```### events.off
Remove listener from event
```js
events.off('event1', data => {
console.log('event1 is called with data:', data);
});
```### events.trigger
Trigger event with data
```js
events.trigger('event1', 'some data');
```### events.removeEventHandlers
Remove all listeners
```js
events.removeEventHandlers();
```### events.triggers
Get all triggers
```js
events.triggers;
```### events.deactivate()
Disable event handlers. The trigger method will not produce any effect.
```js
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.
```js
events.on('event1', data => {
console.log('event1 is called with data:', data);
});events.deactivate();
events.trigger('event1', 'some data'); // no effectevents.activate();
events.trigger('event1', 'some data'); // handler is called
```### events.eachTriggers()
Method for delegate triggers to otherEventsSource(for example)
```js
events.eachTriggers((trigger, eventName) => otherEventsSource.on(eventName, trigger));
```## Run tests
```sh
npm test
```## Maintainer
👤 **Krivega Dmitriy**
- Website: https://krivega.com
- Github: [@Krivega](https://github.com/Krivega)## Contributing
Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/Krivega/events-constructor/issues). You can also take a look at the [contributing guide](https://github.com/Krivega/events-constructor/blob/master/CONTRIBUTING.md).## 📝 License
Copyright © 2020 [Krivega Dmitriy](https://github.com/Krivega).
This project is [MIT](https://github.com/Krivega/events-constructor/blob/master/LICENSE) licensed.