# @nano-utils/evt-src

> A lightweight event source library inspired by the nanoevents library

Latest version **1.3.1** (published 2022-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nano-utils/evt-src
pnpm add @nano-utils/evt-src
yarn add @nano-utils/evt-src
bun add @nano-utils/evt-src
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2022-07-21 |
| First published | 2021-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 74.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jason Xu |
| Maintainers | jason-xu |
| Keywords | Events, EventEmitter, EventSource, Event |

## Links

- npm: https://www.npmjs.com/package/@nano-utils/evt-src
- Repository: https://github.com/JasonXu314/nano-utils.git#master
- Homepage: https://github.com/JasonXu314/nano-utils/tree/master#readme
- Issues: https://github.com/JasonXu314/nano-utils/issues
- npm.io page: https://npm.io/package/@nano-utils/evt-src

## 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

- 1.3.1 (latest) — 2022-07-21
- 1.3.0 — 2022-07-20
- 1.2.4 — 2021-10-14
- 1.2.3 — 2021-10-14
- 1.2.2 — 2021-10-14
- 1.2.1 — 2021-10-14
- 1.2.0 — 2021-10-14
- 1.1.1 — 2021-10-13
- 1.1.0 — 2021-10-13
- 1.0.3 — 2021-10-13
- 1.0.2 — 2021-10-13
- 1.0.1 — 2021-10-11
- 1.0.0 — 2021-10-11

## README

## Description

A lightweight event source library inspired by the nanoevents library

## Installation

```
npm i @nano-utils/event-src
```

or

```
yarn add @nano-utils/event-src
```

## Usage

```js
import { EventSrc } from '@nano-utils/event-src';

const src = new EventSrc();

src.on('MY_EVENT', (evt) => {
	console.log(`Foo: ${evt.foo}`);
});

src.dispatch('MY_EVENT', { foo: 'bar' }); // Foo: bar
```

This package comes fully typed:

```ts
import { EventSrc } from '@nano-utils/event-src';

type Events = {
	MY_EVENT: [{ foo: string }];
};

const src = new EventSrc<Events>();

src.on('MY_EVENT', (evt) => {
	console.log(`Foo: ${evt.foo}`);
});

src.dispatch('MY_EVENT', { foo: 'baz' }); // Foo: baz
```

The type `Events` may also be declared as an interface:

```ts
import { EventSrc } from '@nano-utils/event-src';

interface Events {
	MY_EVENT: [{ foo: string }];
}

const src = new EventSrc<Events>();

src.on('MY_EVENT', (evt) => {
	console.log(`Foo: ${evt.foo}`);
});

src.dispatch('MY_EVENT', { foo: 'fizz' }); // Foo: fizz
```

## EventSrc.wrap

EventSrc may also be used to "wrap" existing event sources with an addEventListener method (ie. DOM Elements). An array of the underlying events to listen to must be passed in.

```ts
import { EventSrc } from '@nano-utils/event-src';

const btn = document.getElementById<HTMLButtonElement>('my-btn');

const src = EventSrc.wrap<HTMLElementEventMap>(btn, 'click');

src.on('click', (evt) => console.log(evt));
```

## Usage Notes:

-   All listeners for each event are run synchronously, in the order in which they are added

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