Skip to content

LPegasus/async-iterator-from-rx

Repository files navigation

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 sizeMINIFIED

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

About

A tool to convert RxJS style observable stream asyncGenerator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published