A tool to convert RxJS style observable stream asyncGenerator
npm install async-iterator-from-rxnpm i -S rx-from-async-iterator tslib rxjs
asyncIteratorFromRx to convert stream to asyncIterator so that you can use for await ... of syntax to process the stream signal.
typescript
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);
``