# with-observable

> Lightweight observable mixin

Latest version **1.2.0** (published 2017-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install with-observable
pnpm add with-observable
yarn add with-observable
bun add with-observable
```

## 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.2.0 |
| Published | 2017-09-07 |
| First published | 2017-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | sueddeutsche |

## Links

- npm: https://www.npmjs.com/package/with-observable
- Repository: https://github.com/sueddeutsche/with-observable
- Homepage: https://github.com/sueddeutsche/with-observable#readme
- Issues: https://github.com/sueddeutsche/with-observable/issues
- npm.io page: https://npm.io/package/with-observable

## Recent versions

- 1.2.0 (latest) — 2017-09-07
- 1.0.1 — 2017-01-16
- 1.0.0 — 2017-01-13

## README

# withObservable

> Small and simple observable pattern.

- enforces event-name registration on init
- observable pattern is used to clearly distinct from EventEmitter and sometimes ambigious _on_-methods
- lightweight solution


## Installation

`npm install with-observable --save`


## Usage


```js

// mixin the observable behaviour:
// myobject.observers;
// myobject.addObserver;
// myobject.addObserverOnce;
// myobject.removeObserver;
// myobject.notify;
myObject = withObservable.call(myObject, ["event_name", "another_event"]);

// register to an event
myObject.addObserver("event_name", function () {
	// do something
});

// register to an event once
myObject.addObserver("event_name", function () {
	// do something, will never be called again
});

// prioritize an event listener (gets executed first)
myObject.addObserver("event_name", function () {
	// do something
}, true);

// stop observing given callback
myObject.addObserver("event_name", callback);

// register event (should be used within myobject)
myObject.notify("event_name", "and", "params");

// contains callbacks by events
myObject.observers

// remove observable
withObservable.dispose.call(myObject);

```


## Recommendations

Within a Constructor use the mixin as follows

```
AClass.UPDATE_EVENT = "update";
AClass.END_EVENT = "end";

function AClass() {
    withObservable.call(this, [AClass.UPDATE_EVENT, AClass.END_EVENT]);
}
```

This ensures that

- The event names are accessible from outside
- refactoring of eventnames is simple
- better overview of actual events
- calling withObservable in constructor prevents conflicts

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