# @yadah/domain-listener

> Yadah domain class mixin to allow registering event handlers

Latest version **0.2.0** (published 2023-04-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install @yadah/domain-listener
pnpm add @yadah/domain-listener
yarn add @yadah/domain-listener
bun add @yadah/domain-listener
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2023-04-08 |
| First published | 2023-04-08 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM |
| Node | >=18.3.0 |
| Dependencies | 1 |
| Unpacked size | 3.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ttoohey |
| Maintainers | ttoohey |
| Keywords | yadah |

## Links

- npm: https://www.npmjs.com/package/@yadah/domain-listener
- Repository: https://github.com/ttoohey/yadah
- Homepage: https://github.com/ttoohey/yadah/tree/master/packages/domain-listener#readme
- Issues: https://github.com/ttoohey/yadah/issues
- npm.io page: https://npm.io/package/@yadah/domain-listener

## Dependencies (1)

- [@yadah/mixin](https://npm.io/package/@yadah/mixin.md) 0.2.0

## Recent versions

- 0.2.0 (latest) — 2023-04-08

## README

# Yadah Listener mixin

A mixin for [Yadah](https://www.npmjs.com/package/@yadah/yadah) Domain classes
adding the `registerListeners()` method. It provides a standardised way for
Domain classes to register event handlers.

## registerListeners()

Hook for derived classes to register event listeners.

`registerListeners()` is called during the `boot()` lifecycle method
so access to other domains and subsystems is available.

`registerListeners()` should return synchronously.

```js
import ListenerMixin from "@yadah/domain-listener";
import { Domain } from "@yadah/data-manager";
import { pipe } from "@yadah/mixin";

class MyDomain extends pipe(Domain, ListenerMixin) {
  registerListeners() {
    // ensure superclass event listeners are registered
    super.registerListeners();

    // listen for 'hello' events
    this.on("hello", (name) => this.hello(name));
  }

  hello(name) {
    console.log(`Hello ${name}`);
  }
}

// after domain boot
dataManager.domains.MyDomain.emit("hello", "world"); // logs "Hello world"
```

It is common to listen to events from other domains:

```js
registerListeners() {
    // ensure superclass event listeners are registered
  super.registerListeners();

  // listen for 'hello' events
  this.on("hello", (name) => this.hello(name));

  // listen for 'hola' events on another domain class
  const { OtherDomain } = this.domains;
  OtherDomain.on("hola", (name) => this.hello(`${name} (OtherDomain)`));
}

dataManager.domains.OtherDomain.emit("hola", "mundo"); // logs "Hello mundo (OtherDomain)"
```

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