# promise-observer

> An observer / event emitter implementation with promise support

Latest version **1.0.13** (published 2015-10-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-observer
pnpm add promise-observer
yarn add promise-observer
bun add promise-observer
```

## 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.13 |
| Published | 2015-10-14 |
| First published | 2014-11-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | spion |
| Maintainers | spion |
| Keywords | promise, observer, observable, event, events |

## Links

- npm: https://www.npmjs.com/package/promise-observer
- Repository: https://github.com/doxout/promise-observer
- Issues: https://github.com/doxout/promise-observer/issues
- npm.io page: https://npm.io/package/promise-observer

## Dependencies (1)

- [bluebird](https://npm.io/package/bluebird.md) ^2.3.11

## 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.0.13 (latest) — 2015-10-14
- 1.0.12 — 2015-01-26
- 1.0.11 — 2015-01-26
- 1.0.10 — 2015-01-26
- 1.0.9 — 2015-01-20
- 1.0.8 — 2014-11-18
- 1.0.7 — 2014-11-14
- 1.0.6 — 2014-11-13
- 1.0.5 — 2014-11-12
- 1.0.4 — 2014-11-12
- 1.0.3 — 2014-11-11
- 1.0.2 — 2014-11-11
- 1.0.1 — 2014-11-11

## README

# promise-observer

An observer implementation with promise support.

Its meant to behave similarly to typical synchronous
[Java / NET observers](https://msdn.microsoft.com/en-us/library/ff648108.aspx),
where once you notify your subscribers you are able to wait for their update
functions to execute before proceeding

# Example

Given a blog post creator:

```typescript
import po = require('promise-observer')
function BlogPostCreator() {
    this.onCreated = po.create(emit => this.emit = emit);
}
BlogPostCreator.prototype.create = function(blogPost) {
    actuallyCreateBlogpost()
        .then(post => this.emit(post))
        // wait for all attached events to complete before commiting.
        .then(commitTransaction);
}
```

a categorizer can attach to its events


```typescript
onPostCategorized = blogPostCreator.onCreated(post =>
  categorize(post).then(saveCategory).thenReturn(post));
```

an indexer can add search terms to the index for that post

```typescript
onPostIndexed = blogPostCreator.onCreated(post =>
  index(post).then(saveIndex).thenReturn(post));
```

Then, the email notification system can wait for the post to be
categorized and indexed before sending a notification to all subscribers:

```typescript
onPostNotification = blogPostCreator.onCreated(post => {
  var categorized = onPostCategorized.next(categorizedPost => categorizedPost.id == post.id);
  var indexed = onPostIndexed.next(indexedPost => indexedPost.id == post.id);
  return Promise.join(categorized, indexed, _ => sendEmailNotification(post))
});
```

# API


### po.create(emit):Observable

`po.create(emit: (val:T) => Promise<void>):Observable<T>`

Creates a new observable. The observable exposes its emit function through the
revealing constructor pattern. Use the emit function to notify all subscribers
of new events.

The emit function returns a promise that resolves when all subscribers and
their dependents finish processing the event.


### Observable<T>

```typescript
interface Observable<T> {
    <U>(listener: (t: T) => U): LinkedObservable<U>;
    <U>(listener: (t: T) => Promise<U>): LinkedObservable<U>;
    next(predicate?: (t: T) => boolean): Promise<T>;
    remove<U>(o: Observable<U>): void;
}
```

#### observable(listener):LinkedObservable

Creates a listener for the observable. A listener is a mapping function that returns
either a new value or a promise.

Returns a linked observable  that emits whenever the returned  promises or values
resolve.

#### observable.next(predicate?):Promise

Waits for the next event that satisfies the specified predicate. Returns a
promise for the value contained in that event.

The predicate is optional.

#### observable.remove(linkedObservable)

Removes a listener (linked observable).

### linkedObservable.unlink()

Same as `parentObservable.remove(linkedObservable)`

# Building

    npm install
    npm run build

# License

MIT

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