[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 effects, through the DOM sources. However, the main function only gets the DOMSource as input. This lessons shows how we can generalize main to receive an object of sources, containing all kinds of read effects that we can use.
Code to be chagned:
function run(mainFn, drivers) {
const proxyDOMSource = new Rx.Subject();
const sinks = mainFn(proxyDOMSource);
const DOMSource = drivers.DOM(sinks.DOM);
DOMSource.subscribe(click => proxyDOMSource.onNext(click));
// Object.keys(drivers).forEach(key => {
// drivers[key](sinks[key]);
// });
}
This is hard code now, make it more fixable:
function run(mainFn, drivers) {
const proxySource = {};
//For each driver, we need to proxySource
Object.keys(drivers).forEach( (key)=>{
proxySource[key] = new Rx.Subject();
} );
//Get sinks (output effect)
const sinks = mainFn(proxySource);
Object.keys(drivers).forEach( (key)=>{
//for each drive, create a source for that
const Source = drivers[key](sinks[key]);
Source.subscribe( x => proxySource[key].onNext(x) );
} );
}
---------------------
Code:
// Logic (functional)
function main(Sources) {
const click$ = Sources.DOM;
return {
DOM: click$
.startWith(null)
.flatMapLatest(() =>
Rx.Observable.timer(0, 1000)
.map(i => `Seconds elapsed ${i}`)
),
Log: Rx.Observable.timer(0, 2000).map(i => 2*i),
};
} const drivers = {
DOM: DOMDriver,
Log: consoleLogDriver,
} // bProxy = ...
// a = f(bProxy)
// b = g(a)
// bProxy.imitate(b) function run(mainFn, drivers) { const proxySource = {};
Object.keys(drivers).forEach( (key)=>{
proxySource[key] = new Rx.Subject();
} );
const sinks = mainFn(proxySource);
Object.keys(drivers).forEach( (key)=>{
const Source = drivers[key](sinks[key]);
Source.subscribe( x => proxySource[key].onNext(x) );
} );
} run(main, drivers); // source: input (read) effects
// sink: output (write) effects // Effects (imperative)
function DOMDriver(text$) {
text$.subscribe(text => {
const container = document.querySelector('#app');
container.textContent = text;
});
const DOMSource = Rx.Observable.fromEvent(document, 'click');
return DOMSource;
} function consoleLogDriver(msg$) {
msg$.subscribe(msg => console.log(msg));
}
[Cycle.js] Generalizing run() function for more types of sources的更多相关文章
- [Cycle.js] Introducing run() and driver functions
Currently the code looks like : // Logic (functional) function main() { return { DOM: Rx.Observable. ...
- 学习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 ...
- [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 ...
- jquery.cycle.js简单用法实例
样式: a{text-decoration: none;} *{;;} /*容器设置*/ .player { width:216px; height:248px; background:url(htt ...
- [Cycle.js] Read effects from the DOM: click events
So far we only had effects that write something to the external world, we are not yet reading anythi ...
- [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' ...
- jquery.cycle.js
jquery.cycle.js简单用法实例 样式: a{text-decoration: none;} *{margin:0; padding:0;} /*容器设置*/ .player { width ...
- [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 ...
随机推荐
- 在VM中安装Android4.4连接小米手环 之 在VM中安装Android4.4
今天刚买了个小米手环,系统须要4.4及以上,但自己手机系统版本号不匹配.故打算在VM中安装Android4.4连接小米手环. 这一节先介绍在VM中安装Android4.4(怎么安装VM就不介绍了) 1 ...
- 福昕阅读器drm加密解密总结
drm是数字版权保护的一种方式,前一段时间在做四川文轩数字图书馆项目的时候用到了相关的知识,感觉这东西对于一些在线阅读和视频播放还是有很大用处的. 对于其工作原理我也很好奇,先摘抄度娘的内容如下,当然 ...
- Android学习笔记之viewholder
在adapter中通过使用静态内部类(viewholder)缓存组件的引用来防止ListView刷新时重新LayoutInflater跟findViewById从而达到优化的目的.示例如下: @Ove ...
- zabbix监控应用连接数
zabbix使用用户自定义键值来监控应用系统连接数: 1.修改配置文件zabbix_agentd.conf 格式: UserParameter=<key>,<shell comman ...
- js获取页面名称
function pageName() { var strUrl = location.href; var arrUrl = strUrl.split("/"); ...
- Unable to find manifest signing certificate in the certificate(Visual studio)
今天打开之前做的项目,突然得到很奇怪的编译错误: Unable to find manifest signing certificate in the certificate 网上搜一下,有两个方法, ...
- SVN中trunk,branches,tags用法详解
原文地址:http://www.cnblogs.com/dafozhang/archive/2012/06/28/2567769.html Subversion有一个很标准的目录结构,是这样的.比如项 ...
- do/while(0) c4127
原文链接:http://cnicholson.net/2009/03/stupid-c-tricks-dowhile0-and-c4127/ // NOISY CODE #define MULTI_L ...
- C# 调用浏览器打开网址
private void button1_Click(object sender, EventArgs e) { //调用系统默认的浏览器 System.Diagnostics.Process.Sta ...
- npm和gulp学习笔记
原文链接:[gulpfile.js文件常见配置]