# obseriot

> Simple observer pattern

Latest version **0.3.9** (published 2017-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install obseriot
pnpm add obseriot
yarn add obseriot
bun add obseriot
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.9 |
| Published | 2017-05-24 |
| First published | 2016-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | aggre |
| Maintainers | aggre |

## Links

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

## Dependencies (1)

- [riot-observable](https://npm.io/package/riot-observable.md) 3.0.0

## Recent versions

- 0.3.9 (latest) — 2017-05-24
- 0.3.8 — 2017-05-21
- 0.3.7 — 2017-03-11
- 0.3.6 — 2017-01-26
- 0.3.5 — 2016-12-11
- 0.3.4 — 2016-12-10
- 0.3.3 — 2016-12-06
- 0.3.2 — 2016-12-03
- 0.3.1 — 2016-11-28
- 0.3.0 — 2016-11-28
- 0.2.0 — 2016-11-27
- 0.1.16 — 2016-08-13
- 0.1.15 — 2016-08-01
- 0.1.14 — 2016-07-28
- 0.1.13 — 2016-07-28
- … 5 more at https://npm.io/package/obseriot/versions

## README

Simple observer pattern

# Install

```
npm install obseriot
```

# Usage

## Define event

Object with a `handler.name` and `handler.action` .

- `handler.name` => Event name
- `handler.action` => Return the parameters to be provided to the listener
  - Array is provided as a variadic argument
  - Can be provide a variety of types. String, Object, Function, whatever

```js
const urlChange = {
    handler: {
        name: 'url_change',
        action ( collection, id, action ) {
            // Some processing and formatting
            return [ collection, id, action ]
        }
    }
}
```

## Listen and Notify

```js
// obseriot.listen( event object , callback function )
obseriot.listen( urlChange, ( ...arg ) => {
    console.log( arg ) // => 'shop', 1, 'detail'
} )

// obseriot.notify( event object , parameters )
obseriot.notify( urlChange, 'shop', 1, 'detail' )
```

One time listener

```js
// obseriot.once( event object , callback function )
obseriot.once( urlChange, ( ...arg ) => {
    console.log( arg )
} )
```

## Remove listeners

Remove all registered listeners.

```js
// obseriot.remove( event object )
obseriot.remove( urlChange )
```

Remove one registered listener.

```js
// obseriot.remove( event object, callback function )
const callback = ( ...arg ) => {
    console.log( arg )
}
obseriot.listen( urlChange, callback ) // Listen to the named function.
obseriot.remove( urlChange, callback ) // Remove!
```

# How to use like Flux

Define Action

```js
export const increment = {
    handler: {
        name: 'action_increment',
        action ( num = 1 ) {
            return [ num ]
        }
    }
}
```

Define Store

```js
import {increment} from './action/increment'
import obseriot from 'obseriot'

export const count = {
    state: 0,
    handler: {
        name: 'store_count',
        action () {
            return [ count.state ]
        }
    }
}

obseriot.listen( increment, num => {
    count.state = count.state + num
    obseriot.notify( count )
} )
```

Your Component

```js
import {increment} from './action/increment'
import {count} from './store/count'
import obseriot from 'obseriot'

// Action in somewhere components
obseriot.notify( increment, 1 )

// Listen to Store update
obseriot.listen( count, newCount => {
    console.log( newCount ) // => 1
} )
```

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