1.0.0 • Published 2 years ago

async-iterator-from-rx v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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