# rxjs-multiscan-operator

> RxJS operator for sharing state between Observables

Latest version **1.1.0** (published 2017-04-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install rxjs-multiscan-operator
pnpm add rxjs-multiscan-operator
yarn add rxjs-multiscan-operator
bun add rxjs-multiscan-operator
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-04-08 |
| First published | 2017-03-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mateusz Podlasin |
| Maintainers | mpodlasin |
| Keywords | RxJS, rxjs, Observable, Observables, scan |

## Links

- npm: https://www.npmjs.com/package/rxjs-multiscan-operator
- npm.io page: https://npm.io/package/rxjs-multiscan-operator

## Recent versions

- 1.1.0 (latest) — 2017-04-08
- 1.0.0 — 2017-03-17
- 0.1.0 — 2017-03-17

## README

# Observable.multiScan

Custom [RxJS 5](https://github.com/ReactiveX/rxjs) operator for sharing state between Observables in safe and controlled manner. It is an extension of `scan` operator for multiple Observables.

## Motivation

Say you are displaying two buttons and a counter (starting with zero) on a web page. When user clicks first button, the counter should be increased by one. When second button is clicked, counter should be decreased.

Usual solution is to merge two Observables, somehow mark values from each and then use `scan` detecting with switch statement if we should increase or decrease counter. This results in [Redux](https://github.com/reactjs/redux/)-like code:

```javascript
const allClicks = Observable.merge(
    clicksFromFirst.mapTo({type: 'INCREASE'}), 
    clicksFromSecond.mapTo({type: 'DECREASE'})
);

const counterValues = allClicks.scan((counter, click) => {
    switch (click.type) {
        case 'INCREASE':
            return counter + 1;
        case 'DECRASE':
            return counter - 1;
        default:
            throw Error(`Unknown click type: ${click.type}`)
    }
}, 0);
```

Redux is cool architecture, but this is a bit much to simply share some stateful computation between Observables. There is quite a lot of boilerplate. Default case is handled awkwardly and we even know that it will never happen, but the linter will still complain. Also TypeScript handles such code poorly, since it is difficult for compiler (without additional help and thus more boilerplate) to guess what values will appear in reducer function.

With `multiScan` the same functionality can be implemented like so:

```javascript
const counterValues = Observable.multiScan(
    [clicksFromFirst, (counter, click) => counter + 1],
    [clicksFromSecond, (counter, click) => counter - 1],
    0
);
```

We are passing arrays, which have two elements each: Observable with some values and a reducer function. We also pass initial value, just as in regular `scan`.

Whenever any of passed Observables emits a value, corresponding reducer will be used to update state. When the other Observable emits, its reducer will be called with the previously updated state. 

Now Observables share state in safe and predictable manner.

## Usage

Since `multiScan` operator is not a Observable method, but static function, you can simply import and use it directly:

```javascript
import { multiScan } from 'rxjs-multiscan-operator';
```

If you want your `multiScan` always close, there is an option to patch Observable object as well. Just don't blame me for any name conflicts!

```javascript
import 'rxjs-multiscan-operator/add';
```

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