/ts-streams

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

Primary LanguageTypeScriptMIT LicenseMIT

streams

license:MIT jsr Test codecov

TypeScript modules that provides utilities for Streams API.

Example

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";

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();
  });

await fromMessage(port2)
  .pipeThrough(take(6))
  .pipeTo(forEach((chunk) => {
    console.log(chunk);
  }))
  .finally(() => {
    port2.close();
  });
// output: 1
// output: number
// output: foo
// output: string
// output: true
// output: boolean

License

This library is licensed under the MIT License. See the LICENSE file for details.