学习RxJS:Cycle.js
原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/
是什么
Cycle.js 是一个极简的JavaScript框架(核心部分加上注释125行),提供了一种函数式,响应式的人机交互接口(以下简称HCI):
函数式
Cycle.js 把应用程序抽象成一个纯函数 main()
,从外部世界读取副作用(sources
),然后产生输出(sinks
) 传递到外部世界,在那形成副作用。这些外部世界的副作用,做为Cycle.js的插件存在(drivers),它们负责:处理DOM、提供HTTP访问等。
响应式
Cycle.js 使用 rx.js 来实现关注分离,这意味着应用程序是基于事件流的,数据流是 Observable 的:
HCI
HCI 是双向的对话,人机互为观察者:
在这个交互模型中,人机之间的信息流互为输出输出,构成一个循环,也即 Cycle这一命名所指,框架的Logo更是以莫比乌斯环贴切的描述了这个循环。
唯一的疑惑会是:循环无头无尾,信息流从何处发起?好问题,答案是:
However, we need a .startWith() to give a default value. Without this, nothing would be shown! Why? Because our
sinks
is reacting tosources
, butsources
is reacting tosinks
. If no one triggers the first event, nothing will happen. —— via examples
有了.startWith()
提供的这个初始值,整个流程得以启动,自此形成一个闭环,一个事件驱动的永动机 :)
Drivers
driver 是 Cycle.js 主函数 main()
和外部世界打交道的接口,比如HTTP请求,比如DOM操作,这些是由具体的driver 负责的,它的存在确保了 main()
的纯函数特性,所有副作用和繁琐的细节皆由 driver来实施——所以 @cycle/core 才125 行,而 @cycle/dom 却有 4052 行之巨。
driver也是一个函数,从流程上来说,driver 监听sinks
(main()
的输出)做为输入,执行一些命令式的副作用,并产生出sources
做为main()
的输入。
DOM Driver
即 @cycle/dom,是使用最为频繁的driver。实际应用中,我们的main()
会与DOM进行交互:
- 需要传递内容给用户时,
main()
会返新的DOM sinks,以触发domDriver()
生成virtual-dom
,并渲染 main()
订阅domDriver()
的输出值(做为输入),并据此进行响应
组件化
每个Cycle.js应用程序不管多复杂,都遵循一套输入输出的基本法,因此,组件化是很容易实现,无非就是函数对函数的组合调用
实战
准备工作
安装全局模块
npm install -g http-server
依赖模块一览
"devDependencies": {
"babel-plugin-transform-react-jsx": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babelify": "^7.3.0",
"browserify": "^13.0.1",
"uglifyify": "^3.0.1",
"watchify": "^3.7.0"
},
"dependencies": {
"@cycle/core": "^6.0.3",
"@cycle/dom": "^9.4.0",
"@cycle/http": "^8.2.2"
}
.babelrc (插件支持JSX语法)
{
"plugins": [
["transform-react-jsx", { "pragma": "hJSX" }]
],
"presets": ["es2015"]
}
Scripts(热生成和运行服务器)
"scripts": {
"start": "http-server",
"build": "../node_modules/.bin/watchify index.js -v -g uglifyify -t babelify -o bundle.js"
}
以下实例需要运行时,可以开两个shell,一个跑热编译,一个起http-server(爱用currently亦可
$ npm run build
$ npm start
交互实例1
- 功能:两个button,一加一减, 从0起步,回显计数
- demo地址: http://output.jsbin.com/lamexacaku
HTML代码 (实例2同,略
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>components</title>
</head>
<body>
<div id="container"></div>
<script src="bundle.js"></script>
</body>
</html>
index.js
import Cycle from '@cycle/core'
import { makeDOMDriver, hJSX } from '@cycle/dom' function main({ DOM }) {
const decrement$ = DOM.select('.decrement').events('click').map(_ => -1)
const increment$ = DOM.select('.increment').events('click').map(_ => +1)
const count$ = increment$.merge(decrement$)
.scan((x, y) => x + y)
.startWith(0)
return {
DOM: count$.map(count =>
<div>
<input type="button" className="decrement" value=" - "/>
<input type="button" className="increment" value=" + "/>
<div>
Clicked {count} times~
</div>
</div>
)
}
} Cycle.run(main, {
DOM: makeDOMDriver('#container'),
})
不难看出:
main()
是个纯函数,从始至终不依赖外部状态,它的所有动力来自于DOM事件源click,这个状态机依靠Observable.prototype.scan()
得以计算和传递,最后生成sinks
传递给DOM driver以渲染;- 启动了这个循环是
.startWith()
; - Cycle.run是应用程序的入口,加载
main()
和DOM driver,后者对一个HTML容器进行渲染输出
交互实例2
- 功能: 一个button一个框,输入并点button后,通过Github api搜索相关的Repo,回显总数并展示第一页Repo列表
index.js
import Cycle from '@cycle/core'
import { makeDOMDriver, hJSX } from '@cycle/dom'
import { makeHTTPDriver } from '@cycle/http' const GITHUB_SEARCH_URL = 'https://api.github.com/search/repositories?q=' function main(responses$) {
const search$ = responses$.DOM.select('input[type="button"]')
.events('click')
.map(_ => { return { url: GITHUB_SEARCH_URL } }) const text$ = responses$.DOM.select('input[type="text"]')
.events('input')
.map(e => { return { keyword: e.target.value } }) const http$ = search$.withLatestFrom(text$, (search, text)=> search.url + text.keyword)
.map(state => { return { url: state, method: 'GET' } }) const dom$ = responses$.HTTP
.filter(res$ => res$.request.url && res$.request.url.startsWith(GITHUB_SEARCH_URL))
.mergeAll()
.map(res => JSON.parse(res.text))
.startWith({ loading: true })
.map(JSON => {
return <div>
<input type="text"/>
<input type="button" value="search"/>
<br/>
<span>
{JSON.loading ? 'Loading...' : `total: ${JSON.total_count}`}
</span>
<ol>
{
JSON.items && JSON.items.map(repo =>
<div>
<span>repo.full_name</span>
<a href={ repo.html_url }>{ repo.html_url }</a>
</div>
)
}
</ol>
</div>
}
) return {
DOM: dom$,
HTTP: http$,
}
} const driver = {
DOM: makeDOMDriver('#container'),
HTTP: makeHTTPDriver(),
} Cycle.run(main, driver)
有了实例1做铺垫,这段代码也就通俗易懂了,需要提示的是:
- Rx的Observable对象,命名上约定以$符为结束,以示区分
Observable.prototype.withLatestFrom()
的作用是:在当前Observable对象的事件触发时(不同于combineLatest
),去合并参数的目标Observable对象的最新状态,并传递给下一级Observer- 以上项目完整实例,可在 /rockdragon/rx_practise/tree/master/src/web 找到
小结
寥寥数语,并不足以概括Cycle.js,比如 MVI设计模式,Driver的编写,awesome-cycle 这些进阶项,还是留给看官们自行探索吧。
更多文章请移步我的blog新地址: http://www.moye.me/
学习RxJS:Cycle.js的更多相关文章
- RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景?
RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? - 知乎 https://www ...
- 学习RxJS: 导入
原文地址:http://www.moye.me/2016/05/31/learning_rxjs_part_one_preliminary/ 引子 新手们在异步编程里跌倒时,永远会有这么一个经典问题: ...
- [Cycle.js] From toy DOM Driver to real DOM Driver
This lessons shows how we are able to easily swap our toy DOM Driver with the actual Cycle.js DOM Dr ...
- Javascript学习记录——原生JS实现旋转木马特效
昨天学习到了JS特效部分,然后老师讲了旋转木马特效的实现,如上图.不过只是讲了通过点击箭头实现图片的切换,对于点击图片本身以及二者联动却是没有讲解. 本着一颗追求完美的心,今天花费了一个中午终于将整个 ...
- jquery.cycle.js简单用法实例
样式: a{text-decoration: none;} *{;;} /*容器设置*/ .player { width:216px; height:248px; background:url(htt ...
- [Cycle.js] The Cycle.js principle: separating logic from effects
The guiding principle in Cycle.js is we want to separate logic from effects. This first part here wa ...
- [Cycle.js] Hello World in Cycle.js
Now you should have a good idea what Cycle.run does, and what the DOM Driver is. In this lesson, we ...
- bootstrap插件学习-bootstrap.dropdown.js
bootstrap插件学习-bootstrap.dropdown.js 先看bootstrap.dropdown.js的结构 var toggle = '[data-toggle="drop ...
- bootstrap插件学习-bootstrap.modal.js
bootstrap插件学习-bootstrap.modal.js 先从bootstrap.modal.js的结构看起. function($){ var Modal = function(){} // ...
随机推荐
- [转]Python数据挖掘
- WordPress页面Page和文章Post的相互转换
1. 进入phpMyAdmin: 2. 进入WordPress对应的数据库: 3. 浏览wp_posts数据表: 4. 找到相应的 页面Page 并编辑(找到相应的 文章Post 并编辑): 5. 修 ...
- GNU Makefile编写
[Introduction] make 是程序员很好用的工具,如果存在makefile存在,每次更新代码,执行shell命令 shell 就可以执行所有需要编译的文件,make是根据你编写的Makef ...
- windows 配置 apache + php
需要工具 apache 下载地址 php for windows (与apache同版本,使用线程安全)下载地址 vc (Visual Studio C++) 根据 apache 与 php 的版 ...
- [.NET领域驱动设计实战系列]专题十:DDD扩展内容:全面剖析CQRS模式实现
一.引言 前面介绍的所有专题都是基于经典的领域驱动实现的,然而,领域驱动除了经典的实现外,还可以基于CQRS模式来进行实现.本专题将全面剖析如何基于CQRS模式(Command Query Respo ...
- js for 循环中的 变量问题。
今日处理项目中的一个循环,本来就是一个小小的for循环,后来发现该段程序出现了问题,仔细检查代码没有发现其中的错误.无奈只好叫来了老大帮忙.通过在模版中断点调试(该方式只能自己写debugger断点) ...
- 我所了解的CSS
我真的了解css吗? 我这样问自己. 我的思考和这几天的学习来自于看了寒冬winter大神的这篇blog:谈谈面试与面试题 .说实话, 我边看,脑袋里面边翻篇一样的过着我的那点css知识,看完了,整个 ...
- 实战使用Axure设计App,使用WebStorm开发(3) – 构建页面架构
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- 从零开始用gulp
gulp是基于流的前端构件化工具.目前比较火的前端构建化工具还是挺多的,grunt gulp fis3等等. 这个鬼东西有什么用?请参考https://www.zhihu.com/question/3 ...
- Nginx+Keepalived(带Nginx监控脚本)
转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0220/151.html?1456381460 Keepalived+ nginx的安装部署 主机: ...