# @uxland/event-aggregator

> Event Aggregator

Latest version **1.0.1** (published 2022-02-03) · BSD-4-Clause license · 0 weekly downloads

## Install

```sh
npm install @uxland/event-aggregator
pnpm add @uxland/event-aggregator
yarn add @uxland/event-aggregator
bun add @uxland/event-aggregator
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-02-03 |
| First published | 2020-02-10 |
| Weekly downloads | 0 |
| License | BSD-4-Clause |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 24.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | UXLand |
| Maintainers | avizcaino, uxland-admin |

## Links

- npm: https://www.npmjs.com/package/@uxland/event-aggregator
- Repository: https://github.com/uxland/uxland
- Homepage: https://github.com/uxland/uxland/tree/master/packages/event-aggregator#readme
- Issues: https://github.com/uxland/uxland/issues
- npm.io page: https://npm.io/package/@uxland/event-aggregator

## Dependencies (1)

- [ramda](https://npm.io/package/ramda.md) ^0.27.0

## Recent versions

- 1.0.1 (latest) — 2022-02-03
- 1.0.1-alpha.22 (alpha) — 2021-12-02
- 1.0.1-alpha.12 (beta) — 2021-03-12
- 1.0.1-alpha.21 — 2021-12-02
- 1.0.1-alpha.20 — 2021-06-17
- 1.0.1-alpha.19 — 2021-06-03
- 1.0.1-alpha.18 — 2021-06-03
- 1.0.1-alpha.17 — 2021-06-03
- 1.0.1-alpha.16 — 2021-06-03
- 1.0.1-alpha.15 — 2021-06-03
- 1.0.1-alpha.14 — 2021-06-02
- 1.0.1-alpha.13 — 2021-05-31
- 1.0.1-alpha.11 — 2021-03-12
- 1.0.1-alpha.10 — 2021-03-11
- 1.0.1-alpha.9 — 2021-03-11
- … 30 more at https://npm.io/package/@uxland/event-aggregator/versions

## README

# UXL Event Aggregator [![npm version](https://badge.fury.io/js/%40uxland%2Fevent-aggregator.svg)](https://badge.fury.io/js/%40uxland%2Fevent-aggregator)

| Build Status                                    | Statements                                    | Branches                                  | Functions                                   | Lines                               |
| ----------------------------------------------- | --------------------------------------------- | ----------------------------------------- | ------------------------------------------- | ----------------------------------- |
| ![BuildStatus](#buildstatus# "Building Status") | ![Statements](#statements# "Make me better!") | ![Branches](#branches# "Make me better!") | ![Functions](#functions# "Make me better!") | ![Lines](#lines# "Make me better!") |

## Installation

`npm i @uxland/event-aggregator`

## Usage

### Event Aggregator instance

It is possible to define numerous instances of the Event Aggregator within a project, each one with the same or different events subscribed, simulating each instance as a new channel of communication.

```typescript
const EA = new EventAggregator();
const EA2 = new EventAggregator();

EA.subscribe('event', () => {...});
EA2.subscribe('event2', () => {...});
EA.subscribe('common-event', () => {...});
EA2.subscribe('common-event', () => {...});
```

### Event publish

To publish an event you must specify an eventId and an optional payload that will be collected by subscriber.

```typescript
const payload = { foo: "bar" };
publish("EVENT-ID", payload);
```

It is possible also to provide a class instead of a string event

```typescript
publish(new Klass(), callback);
```

If a subscription is done with provided class, the callback provided at the subscription moment will be called.

### Event subscription

In order to subscribe to an event you must specify an eventId and a callback function that will be called when that event is received.

```typescript
const callback = (payload: any): void => {};
subscribe("EVENT-ID", callback);
```

It is possible also to provide a class instead of a string event

```typescript
subscribe(Klass, callback);
```

### Event subscription (only once)

In order to subscribe to an event, only once, you must specify an eventId and a callback function that will be called when that event is received. For the next publishes of that event, this subscriber will not receive that event.

```typescript
const callback = (payload: any): void => {};
subscribeOnce("EVENT-ID", callback);
```

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