[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 ...
随机推荐
- qt 实现钟表图标
#include "clock.h" CLOCK::CLOCK(QWidget *parent) : QWidget(parent) { QTimer *timer = new Q ...
- c++ 连接两个字符串实现代码 实现类似strcat功能(转)
想实现strcat功能,直接网上找一个. 第一种: #include "stdafx.h" #include<iostream> using namespace std ...
- [编辑中] 免费的Internet流量发生器 | Free Internet Traffic Generators
流量发生器 (Traffic Generator) 是用来检测网络性能,进行网络相关研究的一个很重要的工具.大家可能用过Iperf或者IxChariot,前者是类UNIX环境下的一个免费.开源的网络性 ...
- Word01-从正文处开始插入页码
一份正式的文档应该由以下几部分组成:封面.目录.摘要.正文…… 现在要求前三页不需要插入页码,从正文部分插入页码为第一页,原文档如下: 步骤: 第一步:将光标移动到摘要页的末尾,选择页面部局--> ...
- Linux的VI/VIM
参考自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html 作者:iTech 出处:http://itech.cnblogs.co ...
- 小学生之手(01)之 "for循环"
---恢复内容开始--- 咳咳咳!第一次要写这种东西,要是有不足的地方,请见谅!!!并且感觉在这班门弄斧是不是有点托大了.一向擅长低调的我,在’被逼无奈‘之下,要嚣张一下了......(此处省略500 ...
- Ajax (jquery)实现智能提示搜索框(in Django)
搜索框输入搜索名字,从数据库中筛选名字, 如果有包含输入的字母的名字则以json格式返回并且显示在搜索框下: html文件: <!DOCTYPE html> <ht ...
- mysql 分区表详解
项目中要一张库表实现 list分区.并且支持多种数据库. oracle 很顺利,只是mysql 听说5.1版本就已经支持了, 可是试了很多个版本,都不行,后来查到原因是要5.5 以上版本 分区才支持 ...
- 12 hdfs常用文件、目录拷贝操作、删除操作
package com.da.hbase.tool.utils; import com.da.hbase.tool.common.Const; import org.apache.hadoop.con ...
- C# HashSet类(复杂)对象的去重
public class Student { public string Id { get; set; } public string Name { get; set; } public overri ...