reactive-programmingxstream-js

Emit a stream only if value changed


Using xstream, how can I create a stream that only emits when it's input stream emits a new value

Here is a diagram

input  -----1--1-1--2-3--3--3---5-|
output -----1-------2-3---------5-|

Solution

  • While the core xstream library is comprised of a few well chosen operators, additional operators are included as extras and can accessed by their path.

    import xs from 'xstream';
    import dropRepeats from 'xstream/extra/dropRepeats'
    
    const stream = xs.of(1, 1, 1, 2, 3, 3, 3, 5)
      .compose(dropRepeats())
    
    stream.addListener({
       next: i => console.log(i),
       error: err => console.error(err),
       complete: () => console.log('completed')
    });
    

    The .compose operator is used to drop the extra methods into the stream.

    source: https://github.com/staltz/xstream/blob/master/EXTRA_DOCS.md#dropRepeats