# @rbxts/emitter

> Roblox Typescript package for creating event-emitting objects and classes.

Latest version **2.0.2** (published 2023-01-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install @rbxts/emitter
pnpm add @rbxts/emitter
yarn add @rbxts/emitter
bun add @rbxts/emitter
```

## 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 | 2.0.2 |
| Published | 2023-01-17 |
| First published | 2021-09-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | hero-ku |

## Links

- npm: https://www.npmjs.com/package/@rbxts/emitter
- Repository: https://github.com/hero-ku/emitter
- Homepage: https://github.com/hero-ku/emitter#readme
- Issues: https://github.com/hero-ku/emitter/issues
- npm.io page: https://npm.io/package/@rbxts/emitter

## Recent versions

- 2.0.2 (latest) — 2023-01-17
- 2.0.1 — 2021-09-17
- 2.0.0 — 2021-09-17
- 1.0.1 — 2021-09-15
- 1.0.0 — 2021-09-15

## README

# Emitter
Emitter is a Roblox Typescript package for creating event-emitting objects and classes.

`npm install @rbxts/emitter`

## Usage

### Creating Objects
To create an object, call the `createEmitter` function with the object you want to create the emitter out of, the type for the object, and the types for the events.

```ts
import { CreateEmitter } from "@rbxts/emitter";

interface Chef {
	Foods: string[];
}

interface ChefEvents {
	FoodCooked: (Food: string) => any;
}

const myChef = CreateEmitter<Chef, ChefEvents>({
	Foods: ["Pizza", "Macaroni", "Lasagna"]
});
```

### Receiving Events
Events are connected to through a single method: `Subscribe`, with the first argument being the name of the event and the second argument being the callback.
```ts
myChef.Subscribe("FoodCooked", Food => {
	print(`Chef has cooked ${Food}!`);
});
```
### Firing Events
Once again, triggering events goes through a single method: `Fire`, with the first argument being the name of the event and the following arguments being the parameters for that event.
```ts
while (true) {
	wait(1);
	const Food = MyChef.Foods[math.random(0, Foods.size - 1)];
	MyChef.Fire("FoodCooked", Food);
}
```

### Unsubscribing from Events
To unsubscribe from an event, take the ID of the callback returned from the the `Subscribe` and pass it to the `Unsubscribe` method along with the name of the event you want to unsubscribe from.

```ts
const Subscription = MyChef.Subscribe("FoodCooked", print);
MyChef.Unsubscribe("FoodCooked", Subscription);
```

### Creating Classes
Often times you'll want every instance of a custom class to have different events that can be connected to. For this, extend the `Emitter` class and pass in your event types.

Note: Keep in mind that the `Fire` method is private on classes, so you'll have to write wrapper methods to trigger events from the outside.

```ts
import { Emitter } from "@rbxts/emitter";

interface ChefEvents {
	FoodCooked: () => any;
}

class Chef extends Emitter<ChefEvents> {
	static Foods = ["Pizza", "Macaroni", "Lasagna"];
	
	public CookFood() {
		const Food = Chef.Foods[math.random(0, Chef.Foods.size - 1)];
		this.Fire("FoodCooked", Food);
	}
}

const MyChef = new Chef();
MyChef.Subscribe("FoodCooked", Food => {
	print(`This chef has just cooked ${Food}!`);
});

MyChef.CookFood();
```

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