immutability - RxJS with immutable datastructures? -


i'm working through rxjskoans , i'm seeing recurring pattern of feeding in array of results, subscriber pushes new values onto:

var rx = require('rx'),     subject = rx.subject  var result = []; var s1 = new subject(); s1.subscribe(result.push.bind(result)); s1.onnext('foo'); result;  // ['foo'] 

this impure function; result array mutated subscribe.

i've seen small-scale projects on github make stab @ using immutable.js, none actively maintained.

im wondering if there's widely-adopted immutable implementation pattern, , if not, why?

i wouldn't call pattern, since can pass through stream can pass in immutable data structure:

const stream$ = rx.subject.create();    stream$    .map(data => data.set('a', data.get('a') + 1))    .subscribe(data => console.log(data));    stream$.next(immutable.map({ a:1 }));  stream$.next(immutable.map({ a:2 }));
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.7/dist/global/rx.umd.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>

also, there no reason have result external state. methods scan for. would suggest manage (internal) state:

const stream$ = rx.subject.create();    stream$    .scan((list, val) => list.push(val), immutable.list())    .subscribe(data => console.log(data));    stream$.next('foo');  stream$.next('bar');
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.7/dist/global/rx.umd.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -