RxJS 是管理复杂异步应用的非常优秀的方案。

    It doesn’t really. Ramda is about manipulating transforming data and composing functions. And mostly,
    https://github.com/ramda/ramda/issues/1041
    函数式是转换与组合数据,而非操作数据。

    RxJS
    Reactive Extensions Library for JS

    JS 的响应式扩展库?

    使用 Observables

    RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. This project is a rewrite of Reactive-Extensions/RxJS with better performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface

    处理异步与事件驱动

    一个核心类型 Observable
    辅助类型:Observer, Scheduler, Subject
    算子 - operators 将异步事件处理,看成集合(数组)

    Observer Pattern + Iterator Pattern + FP with collection
    用于管理事件序列

    RxJS: 概念,用于解决异步事件管理:

    • Observable: represents the idea of an invokable collection of future values or events.
    • Observer: is a collection of callbacks that knows how to listen to values delivered by the Observable.
    • Subscription: represents the execution of an Observable, is primarily useful for cancelling the execution.
    • Operators: are pure functions that enable a functional programming style of dealing with collections with operations like map, filter, concat, reduce, etc.
    • Subject: is the equivalent to an EventEmitter, and the only way of multicasting a value or event to multiple Observers.
    • Schedulers: are centralized dispatchers to control concurrency, allowing us to coordinate when computation happens on e.g. setTimeout or requestAnimationFrame or others.

    从一个原生事件,构建一个点击事件序列,然后订阅该事件

    1. import { fromEvent } from 'rxjs';
    2. fromEvent(document, 'click').subscribe(() => console.log('Clicked!'));

    通过纯函数产生值(隔离的状态),使程序更不容易出错:

    1. let count = 0; // 非隔离的状态,(全局的,或非私有的变量)
    2. document.addEventListener('click', () => console.log(`Clicked ${++count} times`));
    3. // -----vs-----
    4. import { fromEvent } from 'rxjs';
    5. import { scan } from 'rxjs/operators';
    6. fromEvent(document, 'click')
    7. .pipe(scan(count => count + 1, 0)) // 隔离的状态(局部变量), 纯函数— 将状态隐含在函数参数中。
    8. .subscribe(count => console.log(`Clicked ${count} times`));

    一系列的算子,方便控制事件流
    RxJS has a whole range of operators that helps you control how the events flow through your observables.

    1. import { fromEvent } from 'rxjs';
    2. import { throttleTime, scan } from 'rxjs/operators';
    3. fromEvent(document, 'click')
    4. .pipe(
    5. throttleTime(1000),
    6. scan(count => count + 1, 0)
    7. )
    8. .subscribe(count => console.log(`Clicked ${count} times`));

    Values
    You can transform the values passed through your observables.

    1. import { fromEvent } from 'rxjs';
    2. import { throttleTime, map, scan } from 'rxjs/operators';
    3. fromEvent(document, 'click')
    4. .pipe(
    5. throttleTime(1000),
    6. map(event => event.clientX),
    7. scan((count, clientX) => count + clientX, 0)
    8. )
    9. .subscribe(count => console.log(count));

    Observables are lazy Push collections of multiple values.
    They fill the missing spot in the following table:


    SINGLE MULTIPLE
    Pull Function Iterator 拿过来,我处理。
    Push Promise Observable 发通知,你处理。— 回调

    Pull and Push are two different protocols that describe how a data Producer (数据生产者) can communicate with a data Consumer(数据消费者).

    pull: 消费者决定何时从生产者获取数据,生产者本身不知道数据何时被消费。
    push: 生产者决定何时向消费者发送数据,消费者不知道何时会获取到数据。

    Pull 与 Push

    Pull与Push是数据’生产者’与数据’消费者’之间通信的两种不同的协议。

    什么是Pull? 在Pull系统中,由数据消费者决定什么时候从数据生产者那里接收数据,数据生产者本身不知道数据是什么时候传递到数据消费者那里的。

    每个JavaScript函数都是一个Pull系统。函数是一个数据生产者,and the code that calls the function is consuming it by “pulling” out a single return value from its call.

    ES2015引入了 generator functions and iterators (function*), 另一种类型的Pull系统。调用 iterator.next() 的代码即是数据消费者,从迭代器iterator(数据生产者)中拉(pulling)出多个值。


    生产者 消费者
    Pull 被动的: produces data when requested. 主动的: decides when data is requested.
    Push 主动的: produces data at its own pace. 被动的: reacts to received data.

    什么是Push? 在Push系统中,由数据生产者决定什么时候向数据消费者发送数据。数据消费者不知道自己什么时候会接收到数据。

    Promise 是最常见的一种 Push 系统。一个 Promise(生产者)向注册的回调函数(消费者)传送 resolved 值,与JavaScript函数不同,它是由Promise负责什么时候向它的回调传递(Push)值.

    RxJS 引入了 Observable 对象,一个新的Push系统。一个 Observable 对象是能够生产多个值的数据生产者 — — 把它的多个值推(Push)向观察者(消费者)。

    • 函数(Function): is a lazily evaluated computation that synchronously returns a single value on invocation.
    • 生成器(Generator):is a lazily evaluated computation that synchronously returns zero to (potentially) infinite values on iteration.
    • Promise:is a computation that may (or may not) eventually return a single value.
    • Observable: is a lazily evaluated computation that can synchronously or asynchronously return zero to (potentially) infinite values from the time it’s invoked onwards.

    图解rxjs:https://reactive.how/
    官网:https://rxjs-dev.firebaseapp.com/