async-iterator-from-rx
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

async-iterator-from-rx

A tool to convert RxJS style observable stream asyncGenerator.

Motivation

RxJS is not so common compare with async/await and it's hard to learn. This tool can help user who is not so familiar with observable to use observables in async/await way.

npm versionbuildcoverageinstall sizeMINIFIEDMINIFIED + GZIPPED

Install

npm i -S rx-from-async-iterator tslib rxjs

Example

Simple usage

For example, we need to collect 10 user touch points when in some case. And we've already have a click stream. Use asyncIteratorFromRx to convert stream to asyncIterator so that you can use for await ... of syntax to process the stream signal.

import { asyncIteratorFromRx } from "async-iterator-from-rx";
import { fromEvent } from "rxjs";

const click$ = fromEvent("pointerdown", document.body); // A click stream.

async function batchCollectByCount(count: number) {
  const clickTrace: Array<[x: number, y: number]> = [];
  const clickIterator = asyncIteratorFromRx(click$); // Convert click stream to an asyncIterator
  for await (const evt of clickIterator) {
    // use `for await ... of` to scan the click stream
    clickTrace.push([evt.x, evt.y]);
    if (clickTrace.length === count) {
      break;
    }
  }
  return clickTrace;
}

batchCollectByCount(10).then(uploadClickTrace);

Package Sidebar

Install

npm i async-iterator-from-rx

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

13.1 kB

Total Files

10

Last publish

Collaborators

  • lpegasus