# @dweomercraft/events

> Lightweight event listening library

Latest version **2.4.2** (published 2019-02-22) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @dweomercraft/events
pnpm add @dweomercraft/events
yarn add @dweomercraft/events
bun add @dweomercraft/events
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.4.2 |
| Published | 2019-02-22 |
| First published | 2019-01-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | jikurata |
| Maintainers | jikurata |
| Keywords | event, listener, javascript, es6 |

## Links

- npm: https://www.npmjs.com/package/@dweomercraft/events
- Repository: https://github.com/dweomercraft/events
- Homepage: https://github.com/dweomercraft/events#readme
- Issues: https://github.com/dweomercraft/events/issues
- npm.io page: https://npm.io/package/@dweomercraft/events

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 2.4.2 (latest) — 2019-02-22
- 2.4.1 — 2019-02-22
- 2.4.0 — 2019-02-16
- 2.3.3 — 2019-01-23
- 2.3.2 — 2019-01-22
- 2.3.1 — 2019-01-15
- 2.3.0 — 2019-01-14
- 2.2.0 — 2019-01-08
- 2.1.2 — 2019-01-08
- 2.0.1 — 2019-01-03
- 2.0.0 — 2019-01-03
- 1.0.0 — 2019-01-03

## README

# events v2.4.2
Lightweight javascript event listening library
---
## Install
```
npm install @dweomercraft/events
```
## Usage
```
const EventEmitter = require('@dweomercraft/events');

const emitter = new EventEmitter();
emitter.on('ping', () => {
  console.log('pong!');
});

emitter.emit('ping'); // pong!
```
Pass as many arguments as needed for your event handlers
```
emitter.on('val', (val1, val2) => {
  console.log(`I have ${val1} and ${val2}`);
});

emitter.emit('val', 3, 5); // I have 3 and 5;
```
Make an instance of EventEmitter available globally
```
const firstEmitter = new EventEmitter({id: 'foo'});
const secondEmitter = EventEmitter.instanceOf('foo');
const thirdEmitter = new EventEmitter({id: 'foo'});
const localEmitter = new EventEmitter();

firstEmitter.on('bar', () => console.log('foobar'));
localEmitter.on('bar', () => console.log('not foo'));
secondEmitter.emit('bar');  // foobar
thirdEmitter.emit('bar');   // foobar
localEmitter.emit('bar');   // not foo
```
## API
---
**Class** EventEmitter(*EventEmitterOptions*)
#### Properties
- **id**: *String* Defines the EventEmitter at the global scope. When id is a falsy value, that instance of EventEmitter will not be available.
- **events**: Object containing all Events registered by the EventEmitter. Object keys are the event names.
- **isEnabled**: *Boolean*. When disabled, the emitter will suppress all emits. Registering and unregistering events will still occur regardless of this property.
#### Methods
- **register**(*eventName*)<br>
  *Arguments*:<br>
    {*String*} eventName: The name of the *Event* to be registered<br>
  *Description*:<br>
    Registers an *Event* with the name *eventName*<br>

- **unregister**(*eventName*)<br>
  *Arguments*:<br>
    {*String*} eventName<br>
  *Description*:<br>
    Removes the Event object with the name *eventName*<br>

- **subscribe**(*eventName*)<br>
  *Arguments*:<br>
    {*String*} eventName<br>
  *Description*:<br>
    Sets the Event's isActive property to true.<br>
    Registered events are subscribed to by default.<br>

- **unsubscribe**(*eventName*)<br>
  *Arguments*:<br>
    {*String*} eventName<br>
  *Description*:<br>
    Sets the Event's isActive property to false.<br>
    If the event does not exist yet, it will register the event and then unsubscribe from it.<br>

- **enable**()<br>
  *Description*:<br>
    Toggles isEnabled to be true<br>

- **disable**()<br>
  *Description*:<br>
    Toggles isEnabled to be false<br>

- **addEventListener**(*eventName*, *handler*, *options*)<br>
  *Arguments*:<br>
    {*String*} eventName<br>
    {*Function*} handler: A function to be called when the *Event* is triggered<br>
    {*EventHandlerOptions*} options (Optional):<br>
  *Description*:<br>
    Registers the event if it does not exist yet, and adds the handler to the *Event* object.<br>
    Returns the id of the assigned handler.<br>


- **on**(*eventName*, *handler*, *options*)<br>
  *Description*: Wrapper for addEventListener. Passes {once: false} as the option.<br>

- **once**(*eventName*, *handler*)<br>
  *Description*: Wrapper for addEventListener. Passes {once: true} as the option.<br>

- **dispatchEvent**(*eventName*, *...args*)<br>
  *Arguments*:<br>
    {*String*} eventName<br>
    {*any*} arg1, arg2, argN...: Arguments to be passed into the *Event*'s *EventHandlers*<br>
  *Description*:<br>
    Triggers the event, calls all of the handlers assigned to the *Event*.<br>

- **emit**(*eventName*, *...args*)<br>
  *Description*:<br>
    Wrapper for dispatchEvent. Exists for semantic purposes.<br>

- **removeEventListener**(*eventName*, *id*)<br>
  *Arguments*:<br>
    {*String*} eventName<br>
    {*String*} id: The id of the *EventHandler*<br>
  *Description*:<br>
    Removes a handler from the specified *Event*<br>
    
**Object** *EventEmitterOptions*<br>
  - id {*String*} (*Default: ''*): *Optional*. When id is a truthy value, the EventEmitter will be available globally.<br>
  - enable {*Boolean*} (*Default: true*): *Optional*. Sets the initial state of the EventEmitter.

**Object** *EventHandlerOptions*<br>
  -  id: {*String*}: Defines the id of the EventHandler<br>
  -  once: {*Boolean*} (*Default: false*): Tells the EventEmitter to call the handler only once<br>
  -  priority: {*String*}: Setting this property to **'first'** will add the handler to the front of the queue. Default behavior adds handlers to the end.
## Version Log
---
**v2.4.2**
- Fixed a bug that allowed a null handler argument to bypass a validation check

**v2.4.1**
- Fixed a bug with ```options.priority = 'first'``` that caused the Event to remove an event from the list, instead of adding one.

**v2.4.0**
- Event's handler property is now an Array queue. id referencing now occurs at the EventHandler level.
- Implement option to add a new handler to the front or end of the handler queue.
- Implement option to define the handler id.

**v2.3.3**
- EventEmitter can now be set as a global reference by providing an id in its constructor.<br>

**v2.3.2**
- Implemented *isEnabled* property for EventEmitter. When isEnabled is falsy, EventEmitter will suppress all emits. Registering and unregistering events will still occur regardless of this setting.<br>

**v2.3.1**
- An issue where certain EventEmitter methods would throw an error when passed an invalid event name has been fixed.<br>

**v2.3.0**
- Event objects now have the property *isActive* to determine whether an Event should execute its handlers or not. This property is set to true by default.
- The EventEmitter can toggle an Event's *isActive* property by using the *subscribe*() and *unsubscribe*() methods.
- The EventEmitter can now kill Events using the *unregister*() method. This will remove an Event, along with all of its handlers.<br>

---
_Source: https://npm.io/package/@dweomercraft/events · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
