# event-interface-mixin

> Light-weight event interface that can be integrated into any class, or used independently.

Latest version **0.1.3** (published 2021-07-17) · ISC license · 0 weekly downloads

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

## Install

```sh
npm install event-interface-mixin
pnpm add event-interface-mixin
yarn add event-interface-mixin
bun add event-interface-mixin
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2021-07-17 |
| First published | 2021-03-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 17.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Remi Alkemade |
| Maintainers | rremigius |
| Keywords | events |

## Links

- npm: https://www.npmjs.com/package/event-interface-mixin
- Repository: https://github.com/rremigius/event-interface-mixin
- Issues: https://github.com/rremigius/event-interface-mixin/issues
- npm.io page: https://npm.io/package/event-interface-mixin

## Dependencies (5)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [typescript](https://npm.io/package/typescript.md) ^4.2.2
- [log-control](https://npm.io/package/log-control.md) 0.0.2
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.168
- [validation-kit](https://npm.io/package/validation-kit.md) 0.0.7

## 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.3 (latest) — 2021-07-17
- 0.1.2 — 2021-07-14
- 0.1.1 — 2021-07-12
- 0.1.0 — 2021-07-10
- 0.0.8 — 2021-03-10
- 0.0.7 — 2021-03-10
- 0.0.6 — 2021-03-09
- 0.0.5 — 2021-03-04
- 0.0.3 — 2021-03-03
- 0.0.2 — 2021-03-01
- 0.0.1 — 2021-03-01

## README

EventInterface mixin
===

EventInterface is a class that implements the basic event subscription and firing. It is made to become part of classes
and objects that need their own event interface, but which should be consistent throughout the code.

EventInterface has Typescript support, making it possible to fire and listen to strongly-typed events.

## Usage

### Basic usage

First the EventInterface needs some events registered. These can be any class. 
Then those events can be fired and listened to.

```javascript
class MyEvent {}
let events = new EventInterface();
events.$event(MyEvent); // register event class

events.on(MyEvent, event => { // event is a MyEvent
    // event.payload will be 123 (see below)
});
events.fire(MyEvent, 123);
events.fire(new MyEvent(123)); // equivalent
```

In Typescript, you can only use the class-based approach, to ensure the type-safety of events:

```typescript
class MyEvent extends Event<number> {}
let events = new EventInterface();
events.$event(MyEvent); // register event class

events.on(MyEvent, event => { // event is strongly-typed as a MyEvent
    // event.payload will be 123 (see below)
});
events.fire(MyEvent, 123); // will complain if 2nd argument is the wrong type
events.fire(new MyEvent(123)); // equivalent
```

In plain javascript, EventInterface can fire events called by name, with anything as payload.

```javascript
// create EventInterface with `allowDynamicEvents` parameter `true`, so it also accepts unregistered events
let events = new EventInterface(true); 
events.on('myEvent', event => {
    // event will be 123 (see below)
});
events.fire('myEvent', 123);
```

In TypeScript, listening and firing unregistered events is also possible, except the event parameter must be a class:
```typescript
let events = new EventInterface(true); 
events.on(MyEvent, event => {
    // event will be a MyEvent with number 123 (see below)
});
events.fire(new MyEvent(123));
```

### Integrate into classes

EventInterface provides a convenient integration with classes, allowing developers and their IDEs to easily understand
what events can be fired and listened to:

**Javascript**:

```javascript
class MoveEvent {
	constructor(from, to){
		this.from = from;
		this.to = to;
    }
}
class CarEvents extends EventInterface {
	constructor() {
		this.move = this.$event(MoveEvent);
    }
}
class Car {
	constructor() {
		this.events = new CarEvents();
	}

	move() {
		// (move logic)
		// ...
		this.events.move.fire(new MoveEvent(oldCoordinates, newCoordinates));
	}
}

let car = new Car();
car.events.move.on(event => {
	console.log(`Car moved from ${event.from} to ${event.to}.`);
});
```

**Typescript**:

```typescript
class MoveEvent extends Event<{from:Coordinates, to:Coordinates}>{}
class CarEvents extends EventInterface {
	move = this.$event(MoveEvent);
}

class Car {
	events = new CarEvents();
    
    move() {
        // (move logic)
        // ...
        this.events.move.fire(new MoveEvent({from: oldCoordinates, to: newCoordinates}));
    }
}

let car = new Car();
car.events.move.on((event:MoveEvent) => {
    console.log(`Car moved from ${event.payload.from} to ${event.payload.to}.`);
});
car.move();
```

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