[Cycle.js] Introducing run() and driver functions
Currently the code looks like :
// Logic (functional)
function main() {
return {
DOM: Rx.Observable.timer(0, 1000)
.map(i => `Seconds elapsed ${i}`),
Log: Rx.Observable.timer(0, 2000).map(i => 2*i),
};
} // Effects (imperative)
function DOMEffect(text$) {
text$.subscribe(text => {
const container = document.querySelector('#app');
container.textContent = text;
});
} function consoleLogEffect(msg$) {
msg$.subscribe(msg => console.log(msg));
} const sinks = main();
DOMEffect(sinks.DOM);
consoleLogEffect(sinks.Log);
We still have this part of code which didn't wrap into a function, we can wrap into a run() function, this can provide a main enterence for the code:
function run(mainFn){
const sinks = mainFn();
DOMEffect(sinks.DOM);
consoleLogEffect(sinks.Log);
}
run(main);
Let's say we want to remove consoleLogEffect, current way we need to comment out from the main() function.
The problems are that in the first place we are hard coding these effects inside run. Instead, we should be able to specify that these are the effects that I want to run my main function, so we need to give our effects to run as well.
That will be an object. Effects functions will be an object, and the keys will match those keys that we saw from the sync here. The DOM function is DOM Effect, and the log function is consoleLogEffect.
const effects = {
DOM: DOMEffect,
Log: consoleLogEffect
}
function run(mainFn, effects){
const sinks = mainFn();
Object.keys(effects)
.forEach( (effectKey)=>{
effects[effectKey](sinks[effectKey]);
})
}
run(main, effects);
The last thing author introduce the 'driver' as the name instead of 'effect' ...
// Logic (functional)
function main() {
return {
DOM: Rx.Observable.timer(0, 1000)
.map(i => `Seconds elapsed ${i}`),
Log: Rx.Observable.timer(0, 2000).map(i => 2*i),
};
} // Effects (imperative)
function DOMDriver(text$) {
text$.subscribe(text => {
const container = document.querySelector('#app');
container.textContent = text;
});
} function consoleLogDriver(msg$) {
msg$.subscribe(msg => console.log(msg));
} function run(mainFn, drivers) {
const sinks = mainFn();
Object.keys(drivers).forEach(key => {
drivers[key](sinks[key]);
});
} const drivers = {
DOM: DOMDriver,
Log: consoleLogDriver,
} run(main, drivers);
[Cycle.js] Introducing run() and driver functions的更多相关文章
- [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 ...
- [Cycle.js] Generalizing run() function for more types of sources
Our application was able to produce write effects, through sinks, and was able to receive read effec ...
- [Cycle.js] Main function and effects functions
We need to give structure to our application with logic and effects. This lessons shows how we can o ...
- 学习RxJS:Cycle.js
原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...
- [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 ...
- RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景?
RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? - 知乎 https://www ...
- 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 ...
- [antd-design-pro] mock 数据(post,request不一致)Sorry, we need js to run correctly!
Sorry, we need js to run correctly! 可能问题: mock数据 api 和 request api 不一致 'POST /api/banners/left' ...
随机推荐
- IOS成长之路-Nsstring中搜索方法rangeOfString
NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1这个字符 ...
- java 多态,和方法覆盖分析(转)
多态 (Polymorphism) 大家应该都不陌生,它是我们开发面向对象系统的“老朋友”了 .但是老朋友也会有“烦心”的时候啊,呵呵.有时候 不注意,还真会被它难到.譬如下面这个例子(thank H ...
- LSI SAS 3008配置操作
配置 LSI SAS 3008 介绍LSISAS3008的配置操作. 4.1 登录CU界面 介绍登录LSISAS3008的CU配置界面的方法. 4.2 创建RAID 介绍在LSISAS3008扣卡上创 ...
- VirtualBox虚拟机网络设置
VirtualBox虚拟机网络设置 测试环境:物理机win10企业版本,VirtaulBox版本5.0.14,虚拟机安装Windows XP及linux系统 想实现虚拟机上网的最简单方式,修改虚拟机网 ...
- jquery之杂记
//选中事件,放在初始化方法里面,toolbar下面 onSelect : function(rowIndex, rowData) { queryChannelFloor(rowIndex, rowD ...
- sql中视图视图的作用
视图是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态 ...
- TableView基本使用
TableView基本使用 基本步奏 1设置数据源 self.tableview.dataSource = self; 2遵守协议 @interface ViewController () <U ...
- 监听键盘 防止输入时覆盖掉textfiled
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwasChange:) name:U ...
- JQ 遍历节点
.children() : 取得匹配元素的子元素集合 .next() :取得匹配元素后面紧邻的同辈元素 .prev() :取得匹配元素前面紧邻的同辈元素 .siblings() :取得匹配元素前.后的 ...
- Struts2 的国际化实现
以前一直看见 i18N ,现在才知道原来 i18N 就是 Internationalization,因为以 i 开头,以 N 结尾,共18个字母,也就是国际化的意思.在百度搜索主页上没有看见中英文的切 ...