# rx-queue-backpressure

> An RX library to manage the number the items in a queue using backpressure

Latest version **0.0.4** (published 2017-01-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install rx-queue-backpressure
pnpm add rx-queue-backpressure
yarn add rx-queue-backpressure
bun add rx-queue-backpressure
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2017-01-23 |
| First published | 2016-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Giles Roadnight |
| Maintainers | roaders |

## Links

- npm: https://www.npmjs.com/package/rx-queue-backpressure
- Repository: https://github.com/Roaders/rx-queue-backpressure
- Homepage: https://github.com/Roaders/rx-queue-backpressure#readme
- Issues: https://github.com/Roaders/rx-queue-backpressure/issues
- npm.io page: https://npm.io/package/rx-queue-backpressure

## Dependencies (2)

- [rx](https://npm.io/package/rx.md) ^4.1.0
- [stream-item-timer](https://npm.io/package/stream-item-timer.md) ^3.0.0

## Recent versions

- 0.0.4 (latest) — 2017-01-23
- 0.0.3 — 2017-01-23
- 0.0.1 — 2016-12-18

## README

[![Build Status](https://travis-ci.org/Roaders/rx-queue-backpressure.svg?branch=master)](https://travis-ci.org/Roaders/rx-queue-backpressure)
[![Known Vulnerabilities](https://snyk.io/test/github/roaders/rx-queue-backpressure/badge.svg)](https://snyk.io/test/github/roaders/rx-queue-backpressure)

# rx-queue-backpressure
An RX library to manage the number the items in a queue using backpressure

## Installation

`npm install --save rx-queue-backpressure`

## Problem

Given a stream like this:

```
Rx.Observable.from(listOf1000Images)
  .map(imagePath => loadImage(imagePath))
  .merge(1)
  .map(image => resizeImage(image))
  .merge(1)
  .map(image => uploadImage(image))
  .merge(1)
  .subscribe();
```

if the `uploadImage` call is very slow there is nothing to slow down the accumulation of images in memory. If loading and resizing the images is very fast but the upload is very slow you would get into the situation where 1000 images had loaded and been resized but only 5 images or so had been uploaded meaning that you have nearly 1000 potentially large images residing in memory.

## Solution

This library allows you to set the number of items you want waiting in a queue so that there are always images waiting for upload but not too many:

```
import {QueueManager} from "rx-queue-backpressure"

var imageSource = Rx.Observable.from(listOf1000Images);

var queueManager = new QueueManager(imageSource,10); // Keep 10 items in queue at all times

queuedImageSource.map(imagePath => loadImage(imagePath))
  .merge(2)
  .map(image => resizeImage(image))
  .merge(2)
  .map(image => uploadImage(image))
  .merge(1)
  .do(() => queueManager.itemRemovedfromQueue())
  .subscribe();
```

In this example the `queuedImageSource` will immediately release 10 images to be loaded and resized. These 10 images will sit in the queue. When the first of these images has been uploaded the `queuedImageSource.itemComplete()` function will fire reducing the number of images in the queue by 1. At this point another images will be released and loaded.

## Tests

Tests can be run as follows:

```
git clone https://github.com/Roaders/rx-queue-backpressure.git
cd rx-queue-backpressure
npm install
npm test
```

## Example
An example stream that demonstrates the use of the queue manager can be ran as follows:

```
git clone https://github.com/Roaders/rx-queue-backpressure.git
cd rx-queue-backpressure
npm install
npm start
```

---
_Source: https://npm.io/package/rx-queue-backpressure · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
