# observer-pattern-js

> Observer pattern implementation in js

Latest version **1.1.0** (published 2020-11-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install observer-pattern-js
pnpm add observer-pattern-js
yarn add observer-pattern-js
bun add observer-pattern-js
```

## 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.1.0 |
| Published | 2020-11-05 |
| First published | 2020-10-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ikrom Murodov |
| Maintainers | ikrom |
| Keywords | Observer, emitter, pattern-observer |

## Links

- npm: https://www.npmjs.com/package/observer-pattern-js
- Homepage: https://github.com/Ikrom-Murodov/observer-pattern-js
- npm.io page: https://npm.io/package/observer-pattern-js

## 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.1.0 (latest) — 2020-11-05
- 1.0.1 — 2020-10-15
- 1.0.0 — 2020-10-15

## README

# Observer pattern implementation in js.

## Installation.
```npm
npm i observer-pattern-js
```

# Usage example.
```ts
import {IEventEmitter, EventEmitter, IEmitterSubscriber} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

const subscriber: IEmitterSubscriber = emitter.subscribe(
  'event-name',
  (name, surname, age): void => {
    console.log(name); // Ikrom.
    console.log(surname); // Murodov.
    console.log(age); // 18.
  },
);

emitter.emit('event-name', 'Ikrom', 'Murodov', 18);

// If you want to unsubscribe from an event you can call the unsubscribe method.
subscriber.unsubscribe();
```

# EventEmitter (API).

### subscribe - Adds the listener function to the end of the listeners array for the event named eventName.

   * Adds the listener function to the end of the listeners array for the event named eventName.
   * @param { string } eventName - name of events to subscribe.
   * @param { IEmitterCallBack } cb - listener function.
   * @public - This method is available to all instances of the  EventEmitter class.
   * @return { IEmitterSubscriber } - Will return the subscriber object.
   * method with which you can unsubscribe from an event.

```ts
import {IEventEmitter, EventEmitter, IEmitterSubscriber} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

const subscriber: IEmitterSubscriber = emitter.subscribe('event-name', (...args) => {});
```


### emit - Synchronously calls each of the listeners registered for the event named eventName.

  * @param { string } eventName.
  * @param { ...* } args - You can pass as many arguments as you like.
  * @public - This method is available to all instances of the  EventEmitter class.
  * @throws Throws an error if no listener has been registered for an event named eventName.
  * @return { void } - This method returns nothing.

```ts
import {IEventEmitter, EventEmitter} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

emitter.emit('eventName', '...args');
```

### getListeners - This method returns a list of listeners.

  * @return { IListeners } - list of listeners
  * @public - This method is available to all instances of the  EventEmitter class.


```ts
import {IEventEmitter, EventEmitter, IListeners} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

const listeners: IListeners = emitter.getListeners();

console.log(listeners);
```

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