An open API service indexing awesome lists of open source software.

https://github.com/milly/ts-streams

Typescript modules that provides utilities for Streams API. Like RXJS.
https://github.com/milly/ts-streams

Last synced: 9 months ago
JSON representation

Typescript modules that provides utilities for Streams API. Like RXJS.

Awesome Lists containing this project

README

          

# streams

[![license:MIT](https://img.shields.io/github/license/Milly/ts-streams)](LICENSE)
[![jsr](https://jsr.io/badges/@milly/streams)](https://jsr.io/@milly/streams)
[![Test](https://github.com/Milly/ts-streams/actions/workflows/test.yml/badge.svg)](https://github.com/Milly/ts-streams/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/Milly/ts-streams/branch/master/graph/badge.svg)](https://codecov.io/gh/Milly/ts-streams)

TypeScript modules that provides utilities for
[Streams API](https://developer.mozilla.org/docs/Web/API/Streams_API).

## Example

```typescript
import { from } from "@milly/streams/readable/from";
import { fromMessage } from "@milly/streams/readable/from-message";
import { switchMap } from "@milly/streams/transform/switch-map";
import { take } from "@milly/streams/transform/take";
import { forEach } from "@milly/streams/writable/for-each";
import { postMessage } from "@milly/streams/writable/post-message";
import { assertEquals } from "@std/assert";

async function* gen() {
yield 1;
await Promise.resolve();
yield "foo";
await Promise.resolve();
yield true;
}

const { port1, port2 } = new MessageChannel();

from(gen())
.pipeThrough(switchMap((chunk) => [chunk, typeof chunk]))
.pipeTo(postMessage(port1))
.finally(() => port1.close());

const result: unknown[] = [];

await fromMessage(port2)
.pipeThrough(take(6))
.pipeTo(forEach((chunk) => {
result.push(chunk);
}))
.finally(() => port2.close());

assertEquals(result, [
1,
"number",
"foo",
"string",
true,
"boolean",
]);
```

## License

This library is licensed under the MIT License. See the [LICENSE](./LICENSE)
file for details.