# @talesoft/event

> A minimalistic event library not bound to the DOM

Latest version **0.1.0** (published 2019-10-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @talesoft/event
pnpm add @talesoft/event
yarn add @talesoft/event
bun add @talesoft/event
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2019-10-15 |
| First published | 2019-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 55 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Talesoft |
| Maintainers | simonekruegel, torbenkoehn, torben-koehn |
| Keywords | event, dispatcher, event-dispatcher |

## Links

- npm: https://www.npmjs.com/package/@talesoft/event
- Repository: https://github.com/Talesoft/event-js
- Issues: https://github.com/Talesoft/event-js/issues
- npm.io page: https://npm.io/package/@talesoft/event

## 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

- 0.1.0 (latest) — 2019-10-15

## README

Event JS
========

[![Build status](https://img.shields.io/travis/talesoft/event-js/master.svg?style=flat-square)](https://travis-ci.org/talesoft/event-js)
[![Coverage](https://img.shields.io/codeclimate/coverage/Talesoft/event-js.svg)](https://codecov.io/github/Talesoft/event-js?branch=master)
[![Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/@talesoft/event.svg)](https://snyk.io/package/npm/@talesoft/event)

A minimalistic implementation of the event pattern.


Installation
------------

```bash
npm i @talesoft/event
```

TypeScript supported out-of-the-box.


Usage
-----

The library comes with a base class `Event` that will represent any kind of event
you want to hook and with an `EventDispatcher` that will manage listeners for events
and dispatch event instances to these listeners.

```js
import { Event, EventDispatcher } from '@talesoft/event';

const dispatcher = new EventDispatcher();

class ClickEvent extends Event {}

dispatcher.addListener(ClickEvent, () => {
    console.log('Clicked!');
});

dispatcher.dispatch(new ClickEvent());
```

### Passing Event Data

```js
import { Event, EventDispatcher } from '@talesoft/event';

const dispatcher = new EventDispatcher();

class ResponseEvent extends Event {
    public content: string = '';
}

dispatcher.addListener(ResponseEvent, event => {
    event.content = 'Hello from Response!';
});

const event = new ResponseEvent();

dispatcher.dispatch(event);

console.log(event.content); // "Hello from Response!"
```

### Cancelling events

```js
import { Event, EventDispatcher } from '@talesoft/event';

const dispatcher = new EventDispatcher();

class WalkEvent extends Event {}

dispatcher.addListener(WalkEvent, () => {
    console.log('I will be called!');
});

dispatcher.addListener(WalkEvent, event => {
    console.log('I will be called, too!');

    event.cancel();
});

dispatcher.addListener(WalkEvent, () => {
    console.log('I won\'t be called anymore.');
});

const event = new WalkEvent();

const success = dispatcher.dispatch(event);

console.log(success); // false

console.log(event.cancelled); // true
```

### Default action prevention

```js
import { Event, EventDispatcher } from '@talesoft/event';

const dispatcher = new EventDispatcher();

class ClickEvent extends Event {}

dispatcher.addListener(ClickEvent, event => {
    event.preventDefault();
    // This is the only thing that will happen, as we'll prevent the default dispatch action
    window.location.href = 'http://other.example.com';
});

const event = new ClickEvent();
if (dispatcher.dispatch(event)) {
    // This won't happen, as dispatch will return false when the default action was prevented
    window.location.href = 'http://example.com';
}

console.log(event.defaultPrevented); // true
```


Contributing
------------

Before contributing, check out the [Contribution Guidelines][contribution-guidelines]

Requires: [npm][nodejs-download]

```bash
// Pull project
git clone https://github.com/Talesoft/event-js

// Enter project directory
cd event-js

// Install development dependencies
npm install

// ... make your changes ...

// Run tests
npm run test

// Lint
npm run lint

// Fix linting problems
npm run lint:fix

// Build
npm run build

// ... create branch, commit, push, merge request etc. ...
```

[contribution-guidelines]: https://github.com/Talesoft/event-js/blob/master/CONTRIBUTING.md
[nodejs-download]: https://nodejs.org/en/

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