question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

exhaustMap equivalent

See original GitHub issue

Hi there
I am new to xstream and I want to use something like exhaustMap operation.
I can not figure out what to do given the available operators (anything like flattenFirst maybe).
What do I do?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
xtianjohnscommented, Dec 13, 2016

Do you want to do something like exhaustMap, @MidnightWonderer? Which parts do you want to keep?

The following should be an acceptable operator implementing only exhaust.

import xs, { Stream } from 'xstream';
import flattenSequentially from 'xstream/extra/flattenSequentially';

function exhaust<T>( source ): Stream<T> {
  /* Initialize listening status to false. */
  let listening: boolean = false;
  
  /**
   * Project the source into a stream of streams.
   * Each will have a value or be empty.
   */
  const projected = source.map( element => xs.of( element ) ).map( project );
  
  function project( inner ) {
    /* We're not listening, project the element. */
    if ( !listening ) {
      /* Toggle listening status. */
      listening = true;
  
      /* When that stream ends, set listening status to false. */
      inner.addListener({
        complete: () => {
          listening = false;
        },
      });

      /* Return the inner stream. */
      return inner;
    }
    
    /* We're already listening, emit nothing. */
    return xs.empty();
  }

  /* Flatten the result streams into a single stream. */
  return projected.compose( flattenSequentially );
}

You would use this operator as @staltz says, using compose.

/* Create a source. */
const source = xs.periodic( 100 );

/* Use the operator. */
source.compose( exhaust );

If you also wanted the projection capabilities in exhaustMap, you’d need to modify the operator to support a selector.

function exhaustMap( selector ) {
  return function exhaust<T>( source ): Stream<T> {
    // ...
    /**
     * Project the source into a stream of streams.
     * Each will have a value or be empty.
     */
    const projected = source.map( selector ).map( project );
  
    function project( element ) {
      // ...
    }
    // ...
  }
}

And you can use this operator by providing a selector. Here is a bin.

import delay from 'xstream/extra/delay';
const source = xs.periodic( 1000 );

const selector = () => xs.from( [ 1, 2, 3] ).compose( delay( 1000 ) );

source.compose( exhaustMap( selector ) );

Does this help you implement your feature?

0reactions
midnight-wonderercommented, Dec 16, 2016

Thank you both for the clarification.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJs Mapping: switchMap vs mergeMap vs concatMap vs ...
The behavior of concatMap, mergeMap, switchMap and exhaustMap is similar in the sense they are all higher order mapping operators.
Read more >
Operator similar to exhaustMap but that remembers the last ...
rxjs - Operator similar to exhaustMap but that remembers the last skipped value from the source and executes it in the end -...
Read more >
exhaustMap - RxJS Reference - InDepth.Dev
exhaustMap operator is basically a combination of two operators - exhaust and map. The map part lets you map a value from a...
Read more >
SwitchMap, MergeMap, ConcatMap & ExhaustMap Explained
With this article I want to describe the differences between the rxjs operators switchMap, mergeMap, concatMap and exhaustMap.
Read more >
Using ExhaustMap in Angular - TekTutorialsHub
Angular ExhaustMap maps value from source to inner observable, subscribes & waits for it to ... It is similar to the index of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found