# synthetic-event

> A collection of base classes for custom events and event targets.

Latest version **2.0.0** (published 2026-01-10) · Zlib license · 0 weekly downloads

## Install

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

## Health

**Score 65/100 (B)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-01-10 |
| First published | 2017-02-07 |
| Weekly downloads | 0 |
| License | Zlib |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 21 |
| Dependencies | 0 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 2 |
| Author | Raoul van Rüschen |
| Maintainers | vanruesc |
| Keywords | event, target, emitter, listener, dispatcher, synthetic, custom |

## Links

- npm: https://www.npmjs.com/package/synthetic-event
- Repository: https://github.com/vanruesc/synthetic-event
- Issues: https://github.com/vanruesc/synthetic-event/issues
- npm.io page: https://npm.io/package/synthetic-event

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

- 2.0.0 (latest) — 2026-01-10
- 1.1.2 — 2020-08-17
- 1.1.1 — 2020-08-10
- 1.1.0 — 2020-08-10
- 1.0.0 — 2019-08-08
- 0.0.4 — 2018-07-02
- 0.0.3 — 2018-07-02
- 0.0.2 — 2017-08-18
- 0.0.1 — 2017-08-02
- 0.0.0 — 2017-02-07

## README

# Synthetic Event

[![CI](https://github.com/vanruesc/synthetic-event/actions/workflows/ci.yml/badge.svg)](https://github.com/vanruesc/synthetic-event/actions/workflows/ci.yml)
[![Version](https://badgen.net/npm/v/synthetic-event?color=green)](https://www.npmjs.com/package/synthetic-event)

This library provides a simplified implementation of the [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) interface. The included classes and interfaces can be used to create [synthetic events](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Events#creating_and_dispatching_events) and custom event targets in any environment.

If your focus lies on DOM events, please refer to the native [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) class. An alternative way to create custom event targets in a browser environment is to use a [DocumentFragment](https://developer.mozilla.org/en/docs/Web/API/Document/createDocumentFragment) as a dummy target.

*[Documentation](https://vanruesc.github.io/synthetic-event)*

## Installation

```sh
npm install synthetic-event
``` 

## Usage

##### Basics

```ts
import { Event, EventTarget } from "synthetic-event";

export interface MyEventMap {

	test: Event<"test">;

}

const eventTarget = new EventTarget<MyEventMap>();

eventTarget.addEventListener("test", (event) => {

	console.log("listener function", event.target);

});

eventTarget.addEventListener("test", {

	handleEvent(event) {

		console.log("listener object", event.target);

	}

});

eventTarget.dispatchEvent({ type: "test" });
```

##### Custom EventTarget

```ts
import { Event, EventTarget } from "synthetic-event";

export interface MyEventMap {

	tick: Event<"tick">;

}

export class MyEventTarget extends EventTarget<MyEventMap> {

	private readonly myEvent: Event<"tick">;

	constructor() {

		super();

		this.myEvent = { type: "tick" };
		setInterval(() => this.dispatchEvent(this.myEvent), 1000);

	}

}
```

## Contributing

Maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

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