# lite-observable

> Lightweight cold observable js library

Latest version **3.0.0** (published 2020-07-17) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2020-07-17 |
| First published | 2019-02-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 48.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Alexis Munsayac |
| Maintainers | lxsmnsyc |

## Links

- npm: https://www.npmjs.com/package/lite-observable
- Repository: https://github.com/LXSMNSYC/LiteObservable
- npm.io page: https://npm.io/package/lite-observable

## Recent versions

- 3.0.0 (latest) — 2020-07-17
- 2.0.1 — 2019-04-16
- 2.0.0 — 2019-04-03
- 1.0.2 — 2019-02-05
- 1.0.1 — 2019-02-05
- 1.0.0 — 2019-02-05

## README

# LiteObservable

Cold Observables for JS in just 617 bytes.

## Usage

### Installing

As a node module

```bash
npm i lite-observable
```

```yarn
yarn add lite-observable
```

### Creating an Observable

```js
const observable = Observable.create(emitter => {
  emitter.next('Hello');
  emitter.next('World');
  emitter.complete();
});
```

Emitters have the following properties:

| Field | Type | Description |
| --- | --- | --- |
| next | ```function``` | Emits a value. |
| error | ```function``` | Emits an error signal. |
| complete | ```function``` | Emits a completion signal. |
| subscription | ```object``` | The subscription context. |

### Subscribing

```js
observable.subscribe(
  x => console.log(`Next: ${x}`)
  e => console.log(`Error: ${e}`),
  () => console.log('Completed'),
);
```

Subscriptions have the following properties:

| Field | Type | Description |
| --- | --- | --- |
| active | ```function``` | Returns the current state of the subscription. Returns false if the subscription is disposed or finished. |
| dispose | ```function``` | Disposes the subscription. |

### Creating your own operators

With the ```pipe``` method, it is easy to create and use your own custom operators.

When constructing operators, it is recommended to use the constructor instead of ```create``` method to prevent subscription overhead, and to directly access the unabstracted Observer object. Use ```sub``` instead of ```subscribe``` to access the operator of the source Observable.

Example below is a map and filter operators:

```js
const map = mapper => source => new Observable((o) => {
  const {
    subscribe, complete, error, next,
  } = o;
  source.sub({
    subscribe,
    next: x => next(mapper(x)),
    error,
    complete,
  });
});

const filter = predicate => source => new Observable((o) => {
  const {
    subscribe, complete, error, next,
  } = o;
  source.sub({
    subscribe,
    next: x => predicate(x) && next(x),
    error,
    complete,
  });
});
```

Example usage:

```js
const observable = Observable.create((e) => {
  for (let i = 0; i < 10; i += 1) {
    e.next(i);
  }
  e.complete();
});

observable.pipe(
  filter(x => x % 2 === 0),
  map(x => x * 2),
  map(x => `Next: ${x}`),
).subscribe(
  console.log,
);
```

which outputs:

```
Next: 0
Next: 4
Next: 8
Next: 12
Next: 16
```

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