Giter VIP home page Giter VIP logo

dive-into-rxjs's Introduction

dive-into-rxjs

对rxjs 5的使用编写demo示例并对rxJS的设计**做简单分析

由于rxjs 5已由版本4迁移,部分api的名字有些变更,需要开发者注意

rxjs4 迁移至 rxjs5 文档

主程序示例代码在'''/example/*.js'''下面

参考文档

learn rxjs reactive io reactive programming

使用

  $ cnpm install
  $ npm start

rxjs核心理念分层

    Observable:
        proveider ==> 事件或者数据源invokable

    Observer:
        consumer ==> 监听observerable的信息分发

    Subscription:
        执行Observable的数据

    Operators:
        纯函数,以函数式风格处理数据,比如map, filter, concat, flatMap等操作符.

    Subject:
        和eventEmitter基本相同,唯一的方式分发数据或者事件到多个Observer.

    Schedulers:
        中心化处理并发,允许我们协调计算。比如e.g. setTimeout or requestAnimationFrame or others.

核心**

  1. Observerable核心

Observable=>Observer: Subscribing to an Observable is analogous to calling a Function.

    Creating Observables
    Subscribing to Observables
    Executing the Observable
    Disposing Observables

why rxjs

  1. purity: 采用纯函数加工数据
    //老的写法
    var count = 0;
    var button = document.querySelector('button');
    button.addEventListener('click', () => console.log(`Clicked ${++count} times`));

    //对比
    var button = document.querySelector('button');
    Rx.Observable.fromEvent(button, 'click')
        //https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/scan.md
      .scan(count => count + 1, 0)
      .subscribe(count => console.log(`Clicked ${count} times`));
  1. flow: 通知机制的流式处理
    // 以典型的button每秒点击为例。 采用rx提供的throttleTime操作符
    const button = document.querySelector('button');
    Rx.Observable.fromEvent(button, 'click')
      .throttleTime(1000)
      .scan(count => count + 1, 0)
      .subscribe(count => console.log(`Clicked ${count} times`));

类似的operator

    filter, delay, debounceTime, take, takeUntil, distinct, distinctUntilChanged etc.
  1. values: 数据处理
    const button = document.querySelector('button');
    Rx.Observable.fromEvent(button, 'click')
      .throttleTime(1000)
      .map(event => event.clientX)
      .scan((count, clientX) => count + clientX, 0)
      .subscribe(count => console.log(count))

类似的operator

    pluck, pairwise, sample etc.

核心概念分解

Observable

rxJS的各种api命名和lodash很像

rxJS的通知执行机制python的generator很类似,

在程序流上面能够很好的和PromiseA+库等保持兼容性

本项目持续更新中,欢迎提出PR

rxjs 5 github文档

Copyright

Copyright (C) 2016 ~ slashhuang [email protected]

dive-into-rxjs's People

Contributors

slashhuang avatar

Stargazers

zhoutk avatar  avatar  avatar mhyuan avatar Ethan avatar Freda avatar Ryan avatar LDQ-first avatar guwm avatar lxlzq11 avatar  avatar Steve avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.