# protected-event-emitter

> An event emitter that can only be called through the protected interface.

Latest version **1.0.0** (published 2016-04-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install protected-event-emitter
pnpm add protected-event-emitter
yarn add protected-event-emitter
bun add protected-event-emitter
```

Provides the command `protected-event-emitter`.

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-04-01 |
| First published | 2016-04-01 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | James Speirs |
| Maintainers | gi60s |
| Keywords | event, emit, protect, private, namespace, on, off, once |

## Links

- npm: https://www.npmjs.com/package/protected-event-emitter
- Repository: https://github.com/Gi60s/protected-event-emitter
- Homepage: https://github.com/Gi60s/protected-event-emitter#readme
- Issues: https://github.com/Gi60s/protected-event-emitter/issues
- npm.io page: https://npm.io/package/protected-event-emitter

## Dependencies (1)

- [custom-error-instance](https://npm.io/package/custom-error-instance.md) ^2.1.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.0 (latest) — 2016-04-01

## README

# protected-event-emitter

## How is this event library different?

Any code can listen to any events but not any code can emit any event. This library allows you to control what code is allowed to emit what events.

## Installation

```sh
$ npm install protected-event-emitter
```

## API

### emitter ( namespace )

Create an event emitter that will emit events on the specified namespace. The namespace must be unique.

**Parameters**

* **namespace** [optional, string] - The namespace to emit events on.

**Returns** an emit function that takes two parameters: 1) the type of event being emitted, and 2) the data for the event.

```js
const event = require('protected-event-emmitter');
const emit = event.emitter('my-emitter');

emit('ready', 'Good to go');    // type is "ready", data is "Good to go"
```

### listenerCount ( namespace [, event ] )

Count how many item are listening the the namespace, optionally limiting results to a specific event type.

**Parameters**

* **namespace** [required, string] - The namespace to count events on.
* **event** [optional, string] - The type of event to filter count to.

**Returns** a number.

### namespace ( namespace )

Get an object that has shortcut functions to the off, on, and once event handler functions for this namespace.

**Parameters**

* **namespace** [required, string] - The namespace to get the shortcut functions for.

**Returns** an object with event handler functions that are scoped to the namespace provided.

```js
const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

const listeners = event.namespace('my-emitter');
listeners.on('ready', readyHandler);    // equivalent to: event.on('my-emitter', 'ready', readyHandler);
listeners.off('ready', readyHandler);   // equivalent to: event.off('my-emitter', 'ready', readyHandler);
listeners.once('ready', readyHandler);  // equivalent to: event.once('my-emitter', 'ready', readyHandler);
```

### namespaces

A getter to get all namespaces that have been registered using the [emitter](#emitter) function. Returns an array of strings.

### off ( namespace, event, callback )

Remove an event handler from the specific namespace and event type.

**Parameters**

* **namespace** [required, string] - The namespace to stop listening on.
* **event** [required, string] - The event type to stop listening for.
* **callback** [required, function] - The function to remove.

**Returns** undefined

```js
const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

event.off('my-emitter', 'ready', readyHandler);
```

### on ( namespace, event, callback )

Add an event handler from the specific namespace and event type.

**Parameters**

* **namespace** [required, string] - The namespace to start listening on.
* **event** [required, string] - The event type to start listening for.
* **callback** [required, function] - The function to call when an event on the namespace and of type occurs.

**Returns** undefined

```js
const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

event.on('my-emitter', 'ready', readyHandler);
```

### once ( namespace, event, callback )

Add an event handler from the specific namespace and event type that will only handle the event once.

**Parameters**

* **namespace** [required, string] - The namespace to start listening on.
* **event** [required, string] - The event type to start listening for.
* **callback** [required, function] - The function to call when an event on the namespace and of type occurs.

**Returns** undefined

```js
const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

event.once('my-emitter', 'ready', readyHandler);
```

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