# event-ag

> Typed event aggregator implementation for Typescript.

Latest version **1.0.6** (published 2022-05-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install event-ag
pnpm add event-ag
yarn add event-ag
bun add event-ag
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2022-05-31 |
| First published | 2021-11-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Kye Purkis |
| Maintainers | kye44 |
| Keywords | pub sub, event aggregator, typescript, typed |

## Links

- npm: https://www.npmjs.com/package/event-ag
- Homepage: https://github.com/kye44/event-ag
- Issues: https://github.com/kye44/event-ag/issues
- npm.io page: https://npm.io/package/event-ag

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2022-05-31
- 1.0.4-beta.3 (beta) — 2022-05-31
- 1.0.5 — 2022-05-31
- 1.0.4-beta.2 — 2022-05-31
- 1.0.4-beta.1 — 2022-05-31
- 1.0.4-beta.0 — 2022-05-31
- 1.0.4 — 2021-11-10
- 1.0.3 — 2021-11-06
- 1.0.2 — 2021-11-06
- 1.0.1 — 2021-11-04
- 1.0.0 — 2021-11-04

## README

# Event-Ag
Typed event aggregator implementation for Typescript.

# Installation
Install via npm using `npm i --save-dev event-ag`

# Usage
## Creating an event
First create an event that extends `PubSubBase` as shown below; this event will be used as event arguments when publishing.
```typescript
export default class NewUserEvent extends PubSubBase<NewUserEvent> {
  private userId: number;
  
  public get UserId: number {
    return this.userId;
  }
  
  public set PackId(value: number) {
    this.userId = value;
  }
}
```
## Creating a Query
A query should be used when a return value is required, queries should not edit the state of the application i.e. be free of side effects.

To create a query, create a class that extends `Query` as shown below; this query class will represent return type of the query.
```typescript
export default class UserQuery extends Query<UsernameQuery> {
  private user: IUser;

  public get User: IUser {
    return this.user;
  }

  public set User(value: IUser){
    this.user = value;
  }
}
```
Args can be passed when making a query, to do this, create an interface that extends the `QueryArgs` interface.
```typescript
export interface UserQueryArgs implements QueryArgs<TestQuery> {
  Username: string;
}
```
## The Event Aggregator
The `EventAggregator` is responsible for managing all of the available events, events are added to the `EventAggregator` the first time `GetEvent` is called with a new event.
To get started with using the `EventAggregator`, create an instance of `EventAggregator`, then get the newly created `PubSubBase` event from the `EventAggregator`.
```typescript
const eventAggregator = new EventAggregator();
const newUserEvent = eventAggregator.GetEvent(NewUserEvent);
```
## Subscribing to events
Events can be subscribed to using the following method.
```typescript
const eventAggregator = new EventAggregator();
const newUserEvent = eventAggregator.GetEvent(NewUserEvent);

newUserEvent.Subscribe((eventArgs: NewUserEvent) => console.log(`New user - UserId: ${eventArgs.UserId}`);
```
## Publishing events
To publish an event, an instance of the event must be passed as event arguments.
```typescript
const eventAggregator = new EventAggregator();
const newUserEvent = eventAggregator.GetEvent(NewUserEvent);

newUserEvent.Subscribe((eventArgs: NewUserEvent) => console.log(`New user - UserId: ${eventArgs.UserId}`);
newUserEvent.Publish({UserId: 0} as NewUserEvent);
```

## Handling Queries
Unlike events, a query can only have one handler. A function can be registered as a handler of a query with the following code.
```typescript
  const eventAggregator = new EventAggregator();
  eventAggregator.GetQuery(UserQuery).Handle((query: QueryArgs<UserQuery> | undefined) => {
    const userQueryArgs = query as UserQueryArgs;
    const user = // Code to find user with **userQuery.Username** goes here.
    return {
      User: user
    } as UserQuery;
  });
```
`NOTE: parameter of handler must inlude Query<T> | undefined as query args may not always be passed.`
## Executing Queries
Executing a query is similar to publishing an event.
```typescript
  const userQuery = eventAggregator.GetQuery(UserQuery);

  const user = userQuery.Execute({
    Username: 'test_username'
  } as UserQueryArgs);
```

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