# @grund/events

> This package contains two key concepts: `Emitter` and `EventService`. But before we go into these implementations, we will just define the difference between a `listener` and `hook` in grund,

Latest version **0.0.85** (published 2021-05-11) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.85 |
| Published | 2021-05-11 |
| First published | 2020-09-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 278.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | schlagerkhan |

## Links

- npm: https://www.npmjs.com/package/@grund/events
- npm.io page: https://npm.io/package/@grund/events

## Dependencies (1)

- [@grund/tools](https://npm.io/package/@grund/tools.md) ^0.0.85

## Recent versions

- 0.0.85 (latest) — 2021-05-11
- 0.0.84 — 2021-05-09
- 0.0.83 — 2021-05-07
- 0.0.82 — 2021-05-07
- 0.0.81 — 2021-05-02
- 0.0.80 — 2021-05-02
- 0.0.79 — 2021-05-02
- 0.0.78 — 2021-05-02
- 0.0.77 — 2021-04-19
- 0.0.76 — 2021-04-19
- 0.0.75 — 2021-04-13
- 0.0.73 — 2021-04-13
- 0.0.72 — 2021-04-11
- 0.0.71 — 2021-04-11
- 0.0.70 — 2021-04-11
- … 44 more at https://npm.io/package/@grund/events/versions

## README

# @grund/events

This package contains two key concepts: `Emitter` and `EventService`. But before we go into these implementations, we will just define the difference between a `listener` and `hook` in grund,

-   `listener`
    -   A listener is a subscribing function that listens to a specific event. The keyword here is that is only _listens_ to the emitter. It should not and cannot alter the outcome of the emitted process.
-   `hook`
    -   A hook is something that is called in a specific lifecycle process, e.g. _beforeCreate_ and _afterUpdate_. This usually takes in one or several objects that is the target of the function (e.g. the object that should be created) and mutates them or returns a formatted version of the object. In other words, a hook can alter the outcome of the emitted process.

## Emitter

The emitter is an object that stores events and callbacks. When creating an emitter one should pass along an `EventSchema` which determines what events are available.

A bit simplified, this is the interface exposed by an emitter.

```ts
interface EventSchema {
    log: (...args: any[]) => any;
}

interface Emitter<EventSchema> {
    on: <Key extends EventSchema>(key: Key, cb: EventSchema[Key]) => any
    once: <Key extends EventSchema>(key: Key, cb: EventSchema[Key]) => any

    off: <Key extends EventSchema>(key: Key, cb: EventSchema[Key]) => any
    offAll: <Key extends EventSchema>(key: Key) => any

    emit: <Key extends EventSchema> => Promise<Array<ReturnType<EventSchema[Key]>>>
}
```

## Event service

The event service is a helper class for other services to extend. The only thing it does is to create two emitters called `listeners` and `hooks`.

So whenever another service wants to implement listeners and hooks, it just extends the `EventService` and can then start using the events. This is used in e.g. `LoggingService` and many of the auth services.

### Usage

```ts
import { Event } from '@grund/events';
import { EventSchema } from './events';

class AnyService extends EventService<EventSchema> {}

const service = new AnyService();

service.listeners.on('test', () => console.log('test fired'));
service.listeners.emit('test');
```

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