[RxJS] Reactive Programming - What is RxJS?

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Reactive Programming - What is RxJS?相关的知识,希望对你有一定的参考价值。

First thing need to understand is, Reactive programming is dealing with the event stream.

 

Event streams happens overtime, which not stay in the memory.

 

For example, the array we have:

var source = [‘1‘, ‘1‘, ‘foo‘, ‘2‘, ‘3‘, ‘5‘, ‘bar‘, ‘8‘, ‘13‘];

Which is stay in the momery. 

 

But the stream we have:

let logic = Rx.Observable.interval(400).take(9)
  .map( (i) => {
    let val = [‘1‘, ‘1‘, ‘foo‘, ‘2‘, ‘3‘, ‘5‘, ‘bar‘, ‘8‘, ‘13‘][i]
    return parseInt(val, 10);
})

Which happens overtime, every 400ms it return an Interge if possible.

 

So the main difference between array stay in memory and the events streams is array already stay in memory and the streams happens overtime.

 

But the nice things about the stream is we can still use the methods we have for array:

let logic = Rx.Observable.interval(400).take(9)
  .map( (i) => {
    let val = [‘1‘, ‘1‘, ‘foo‘, ‘2‘, ‘3‘, ‘5‘, ‘bar‘, ‘8‘, ‘13‘][i]
    return parseInt(val, 10);
})
  .filter( (number) => {
    return !isNaN(number)
  })
  .reduce( (acc, y) => {
     return acc + y;
  } );

let effect = logic.subscribe( (number) => {
  console.log(number);
});

 

以上是关于[RxJS] Reactive Programming - What is RxJS?的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()

[RxJS] Reactive Programming - Sharing network requests with shareReplay()

[RxJS] Reactive Programming - New requests from refresh clicks -- merge()

Reactive-Extensions / RxJS和ReactiveX / rxjs之间有什么区别

全面拥抱 Reactivity: RxJS, RSocket & Svelte

RxJS的基础