ReactiveX

http://reactivex.io/

An API for asynchronous programming
with observable streams

The Observer pattern done right

ReactiveX is a combination of the best ideas from
the Observer pattern, the Iterator pattern, and functional programming

Easily create event streams or data streams.
Compose and transform streams with query-like operators.
Subscribe to any observable stream to perform side effects.
 
 
http://reactivex.io/intro.html

ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.

It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O.

The ReactiveX Observable model allows you to treat streams of asynchronous events with the same sort of simple, composable operations that you use for collections of data items like arrays. It frees you from tangled webs of callbacks, and thereby makes your code more readable and less prone to bugs.

http://reactivex.io/documentation/observable.html

In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits. This pattern facilitates concurrent operations because it does not need to block while waiting for the Observable to emit objects, but instead it creates a sentry in the form of an observer that stands ready to react appropriately at whatever future time the Observable does so.

ReactiveX中文

https://segmentfault.com/a/1190000003632186

响应式编程是一种面向数据流和变化传播的编程范式,数据更新是相关联的。比如很多时候,在写界面的时候,我们需要对事件做处理,伴随着前端事件的增多,对于事件的处理愈发需要更加方便的处理。

设想一下,平时在处理事件的时候,一单上了复杂度,比如输入的时候,需要停止输入的时候才进行,这个时候又只能输入长度大于2才进行事件,当还是之前的数据的话不进行事件,可以考虑一下这个情况下如何去写。

例子

var keyup = Rx.Observable.fromEvent($input, 'keyup')
.map(function (e) {
return e.target.value;
})
.filter(function (text) {
return text.length > 2;
})
.debounce(750)
.distinctUntilChanged();

三秒后解除

var btn = document.getElementById('button');
var logRun = Rx.Observable.fromEvent(btn, 'click')
.merge(Rx.Observable.timer(3000))
.subscribe(e => {
console.log('run!');
logRun.dispose(); // 如果是一次性的就移除observable
});

ReactJS语言实现

https://github.com/Reactive-Extensions/RxJS

例子如上。

教程!!

http://xgrommx.github.io/rx-book/why_rx.html

为什么要使用 ReactJS, 因为 其将 Promise 和 DOM 作为一个整体对待。

One question you may ask yourself, is why RxJS? What about Promises? Promises are good for solving asynchronous operations such as querying a service with an XMLHttpRequest, where the expected behavior is one value and then completion. The Reactive Extensions for JavaScript unifies both the world of Promises, callbacks as well as evented data such as DOM Input, Web Workers, Web Sockets. Once we have unified these concepts, this enables rich composition.

ReactLua语言实现

https://github.com/bjornbytes/RxLua

Reactive Extensions for Lua.

RxLua gives Lua the power of Observables, which are data structures that represent a stream of values that arrive over time. They're very handy when dealing with events, streams of data, asynchronous requests, and concurrency.

local Rx = require 'rx'

Rx.Observable.fromRange(, )
:filter(function(x) return x % == end)
:concat(Rx.Observable.of('who do we appreciate'))
:map(function(value) return value .. '!' end)
:subscribe(print) -- => 2! 4! 6! 8! who do we appreciate!

协程异步。

local Rx = require 'rx'
local scheduler = Rx.CooperativeScheduler.create() -- Cheer someone on using functional reactive programming local observable = Rx.Observable.fromCoroutine(function()
for i = , , do
coroutine.yield(i)
end return 'who do we appreciate'
end, scheduler) observable
:map(function(value) return value .. '!' end)
:subscribe(print) repeat
scheduler:update()
until scheduler:isEmpty()

最简洁

local Rx = require 'rx'

-- Create an observable that produces a single value and print it.
Rx.Observable.of():subscribe(print)

concat例子

local Rx = require 'rx'

local first = Rx.Observable.fromRange()
local second = Rx.Observable.fromRange(, )
local third = Rx.Observable.fromRange(, , ) first:concat(second, third):dump('concat') print('Equivalent to:') Rx.Observable.concat(first, second, third):dump('concat')

观察者模式

local Rx = require 'rx'

local subject = Rx.Subject.create()

subject:subscribe(function(x)
print('observer a ' .. x)
end) subject:subscribe(function(x)
print('observer b ' .. x)
end) subject:onNext()
subject()
subject:onNext()

多道并发

local Rx = require 'rx'
local scheduler = Rx.CooperativeScheduler.create()
local timerResolution = .
local function log(message)
print('[' .. string.format('%.2f', scheduler.currentTime) .. '] ' .. message)
end -- Demonstrate Rx.Scheduler.Cooperative by running some simultaneous cooperative threads.
scheduler:schedule(function()
log('this is like a setTimeout')
end, ) scheduler:schedule(function()
local i =
while true do
log('this prints i twice per second: ' .. i)
i = i +
coroutine.yield(.)
end
end) scheduler:schedule(function()
for i = , do
log('this will print for 3 updates after 1 second')
coroutine.yield()
end
end, ) -- Simulate 3 virtual seconds.
repeat
scheduler:update(timerResolution)
until scheduler.currentTime >=

ReactiveX编程范式的更多相关文章

  1. 编程范式感想(一)——在C中进行对模板功能的实现

    最近一直在看网易公开课上的编程范式的公开课,斯坦福的教授讲的真的非常到位,感觉还是要好好学习下C还有汇编,熟悉下计算机的内存机制什么的. 大家都知道关于模板或者说范式的问题,基本在很多高级语言上都有实 ...

  2. 【编程范式】C语言1

    最近在网易公开课上看斯坦福大学的<编程范式>,外国人讲课思路就是清晰,上了几节课,感觉难度确实比我们普通大学大很多,但是却很有趣,让人能边学边想. 范式编程,交换两个数,利用 void * ...

  3. 编程范式:命令式编程(Imperative)、声明式编程(Declarative)和函数式编程(Functional)

    主要的编程范式有三种:命令式编程,声明式编程和函数式编程. 命令式编程: 命令式编程的主要思想是关注计算机执行的步骤,即一步一步告诉计算机先做什么再做什么. 比如:如果你想在一个数字集合 collec ...

  4. Linux Kernel C语言编程范式

    介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...

  5. 冒号课堂 编程范式与OOP思想

    上篇:编程范式与编程语言 第1课 开班导言 第2课 重要范式 第3课 常用范式 第4课 重温范式 第5课 语言小谈 第6课 语言简评 下篇:抽象机制与对象范式 第7课 抽象封装 第8课 抽象接口 第9 ...

  6. Python3学习之路~6.1 编程范式:面向过程 VS 面向对象

    编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...

  7. Edit Distance问题在两种编程范式下的求解

    本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...

  8. jQuery中的编程范式

    浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...

  9. jQuery 中的编程范式

    浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...

随机推荐

  1. scala tail recursive优化,复用函数栈

    在scala中如果一个函数在最后一步调用自己(必须完全调用自己,不能加其他额外运算子),那么在scala中会复用函数栈,这样递归调用就转化成了线性的调用,效率大大的提高.If a function c ...

  2. Javascript并发模型和事件循环

    Javascript并发模型和事件循环 JavaScript的"并发模型"是基于事件循环的,这个并发模型有别于Java的多线程, javascript的并发是单线程的. Javas ...

  3. Android 图标尺寸与设计

    样例和图解 外框:整体大小 ↑ 边框:图标留白大小 ↓ 图标:外图标的大小 ↑ 阴影:阴影特效大小 ↓ 图形:内图标的大小 ↑ 可选视图权重:使用两种类型的图形尺寸可以达到统一的视觉权重(可选),   ...

  4. python字符串

    字符串格式化 字符串格式化使用字符串格式化操作符%来实现:格式化字符串 % 值(字符串或者数字或者多个值的元组,字典) >>> format = "hello, %s. % ...

  5. 利用JS实现自定义滚动条

    一般默认的滚动条会比较丑,我们可以用简单的js实现自定义滚动条的功能: 代码如下: <!doctype html> <html> <head> <meta c ...

  6. 微信支付(20140923更新)商户支付密钥key的生成与设置

    微信支付(0923更新)商户支付密钥key的生成与设置 说明:新版微信支付,用户必须授权登录才能支付.需要商家自己设置商户号支付密钥. 设置商户号支付密钥方法如下: 1. 申请通过审核后,打开微信发来 ...

  7. osip2 代码分析

    主要类型定义: 1.osip_t /** * Structure for osip handling. * In order to use osip, you have to manage at le ...

  8. JavaScript 入门教程四 语言基础【2】

    一.数据类型介绍: undefined null NaN 1.判断当前变量是否为 undefined: if (i === undefined) 或者 if (typeof (i) === " ...

  9. js 小知识

    在iframe 页面获取父级页面的 html var obj = window.parent.document.getElementById('modaliframe'); 解决Jquery 的在一个 ...

  10. linux添加环境变量(centos)

    1.查看当前环境变量 #echo $PATH 2.增加环境变量 #vi /etc/profile export PATH=/usr/path/bin:$PATH 3.生效 #source /etc/p ...