# @ktsn/ob

> A tiny and extensible FRP library

Latest version **0.1.4** (published 2016-07-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ktsn/ob
pnpm add @ktsn/ob
yarn add @ktsn/ob
bun add @ktsn/ob
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2016-07-03 |
| First published | 2016-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 1 |
| Author | katashin |
| Maintainers | ktsn |
| Keywords | FRP, stream, observable, tiny, extensible |

## Links

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

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.1.4 (latest) — 2016-07-03
- 0.1.3 — 2016-07-03
- 0.1.2 — 2016-07-03
- 0.1.1 — 2016-07-03
- 0.1.0 — 2016-07-03

## README

# ob

[![npm version](https://badge.fury.io/js/%40ktsn%2Fob.svg)](https://badge.fury.io/js/%40ktsn%2Fob)
[![Build Status](https://travis-ci.org/ktsn/ob.svg?branch=master)](https://travis-ci.org/ktsn/ob)

A tiny and extensible FRP library.

## Example

```js
import { Observable } from '@ktsn/ob'

function source() {
  const o = new Observable()

  window.addEventListener('keypress', event => {
    o.value(event.key)
  })

  return o
}

source()
  .filter(val => val.length === 1)
  .flatMap(val => {
    const num = parseInt(val, 10)
    if (isNan(num)) {
      return Observable.error('parse error')
    } else {
      return Observable.value(num)
    }
  })
  .map(val => val * 10)
  .subscribe(
    result => console.log(result),
    error => console.log(error)
  )
```

### Custom operators

You can extends `Observable` class for custom operators.
If you does not want any build in operators, you should extends `BaseObservable` class.

```js
import { Observable } from '@ktsn/ob'

class MyObservable extends Observable {

  tap(f) {
    return this.chain((observer, val) => {
      f(val)
      observer.value(val)
    })
  }

  buffer(ms) {
    let timer = null
    let buffer = []

    return this.chain((observer, val) => {
      clearTimeout(timer)
      buffer.push(val)

      timer = setTimeout(() => {
        observer.value(buffer)
        buffer = []
      }, ms)
    })
  }
}

const my = new MyObservable()

my.tap(val => console.log(val))
  .buffer(100)
  .subscribe(val => console.log(val))

my.value(1)
my.value(2)

setTimeout(() => {
  my.value(3)
  my.value(4)
  my.value(5)
}, 200)

// Output should be...
// 1
// 2
// [1, 2]
// 3
// 4
// 5
// [3, 4, 5]
```

## License

MIT

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