# obgen

> Javascript Observables implemented with async generators

Latest version **0.5.2** (published 2025-01-25) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 55/100 (C)** — status: stable.

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

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.5.2 |
| Published | 2025-01-25 |
| First published | 2021-11-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 89.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Felipe Lima |
| Maintainers | felipecsl |
| Keywords | observable, reactive, stream, generator, async |

## Links

- npm: https://www.npmjs.com/package/obgen
- Repository: https://github.com/felipecsl/obgen
- Homepage: https://github.com/felipecsl/obgen#readme
- Issues: https://github.com/felipecsl/obgen/issues
- npm.io page: https://npm.io/package/obgen

## 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.5.2 (latest) — 2025-01-25
- 0.5.1 — 2023-06-11
- 0.5.0 — 2023-06-11
- 0.4.0 — 2023-06-10
- 0.3.0 — 2023-01-27
- 0.2.4 — 2022-11-02
- 0.2.3 — 2022-11-01
- 0.2.2 — 2022-10-30
- 0.2.1 — 2022-10-30
- 0.2.0 — 2022-10-29
- 0.1.5 — 2021-12-15
- 0.1.4 — 2021-12-15
- 0.1.3 — 2021-12-15
- 0.1.2 — 2021-12-15
- 0.1.1 — 2021-12-15
- … 6 more at https://npm.io/package/obgen/versions

## README

# Obgen

Observable (reactive streams) pattern implemented using es2015 [async generators](https://tc39.es/proposal-async-iteration/).

## Installation

Using yarn:

```bash
yarn add obgen
```

or using npm:

```bash
npm i --save obgen
```

## Usage

Observables are lazy streams of data that emit items asynchronously. They may be infinite or include an optional terminal event to signal the end of the stream. You can `map`, `filter`, etc.:

```typescript
import Observable from "obgen/observable";
import { asyncDefer, buffer, empty, from, just, promise, wrap } from "obgen";

const arr = [...Array(num).keys()].map((_, i) => i);
const observable = from(arr)
  .map((i) => i * 2)
  .filter((i) => i % 2 == 0)
  .take(10);
```

`Observable`s are lazily evaluated. Items are not collected until you subscribe to them:

```typescript
observable.subscribe(console.log);
// outputs:
// 0
// 2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
```

If you prefer, you can instead iterate it with for-await as you normally would:

```typescript
for await (const element of observable.iterable()) {
  console.log(element);
}
```

Or collect the items into an array:

```typescript
const array = await observable.toArray();
```

Observables can be created in multiple ways. For example, you can manually wrap an async generator
function (which is not particularly useful by itself):

```typescript
const observable = wrap(async function* () {
  yield "a";
  yield "b";
  yield "c";
});
```

You can also use `buffer()` to accumulate items until subscription time:

```typescript
const observable = buffer((stream) => {
  stream.emit(1);
  stream.emit(2);
  stream.emit(3);
  stream.emit(4);
  stream.end();
});
```

Or asynchronously emit items:

```typescript
const observable = buffer((stream) => {
  // delay emission for a few milliseconds so that it happens after we subscribe
  times(5, (i) => setTimeout(() => stream.emit(i), i * 100));
  setTimeout(() => stream.end(), 600);
});
expect(await observable.toArray()).toEqual([0, 1, 2, 3, 4]);
```

## Releasing

- Bump the version in `package.json`
- Run `npm publish`
- Create a git tag with the new version and changelog

## License

MIT

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