[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 anything from the external world into our app. This lesson shows how we can change the DOM Driver to return a "DOM Source" representing read effects, such as click events. We will leverage that to create an interactive application.
// 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 effects = {
DOM: DOMEffect,
Log: consoleLogEffect
} function run(mainFn, effects){
const sinks = mainFn();
Object.keys(effects)
.forEach( (effectKey)=>{
effects[effectKey](sinks[effectKey]);
})
} run(main, effects);
Source: stands for input, read effect
sink: stands for output, write effect
So main() function need to take a param 'DOMSource' and effects function need return value:
function main(DOMSource) {
...
} function DOMDriver() {
...
const DOMSource = Rx.Observable.fromEvent(document, 'clicik');
return DOMSource;
} function run(mainFn, drivers) {
const sinks = mainFn(DOMSource);
const DOMSource = drivers['DOM'](sinks['DOM'])
....
}
The problem in the code above is that:
the main function need param 'DOMSource' which is returned by the driver DOMDriver. But for create DOMSource in run() function, we need pass DOMSource to the main() function. So 'DOMSource' is actually used before it created.
I can simply the problem as:
a = f(b); // we need b to create a
b = g(a) // we need a to create b
So there is a cycle going on between main() function and driver() function.
The solution to sovle this problem is :
A is an observable and also B is an observable. If we actually instead of using B, we could use something like B proxy here. Because B proxy is now available for f() as an argument.
Then that helps us to make A, and then given A we can make B. Then now that we have B, we can feed back all of the events that happen on B into B proxy. So that's what we're going to try to achieve.
bProxy = ...
a = f(bProxy)
b = g(a)
bProxy.imitat(b)
So the code looks like:
// Logic (functional)
function main(DOMSource) {
const click$ = DOMSource;
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),
};
} // 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));
} // bProxy = ...
// a = f(bProxy)
// b = g(a)
// bProxy.imitate(b) 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]);
// });
} const drivers = {
DOM: DOMDriver,
Log: consoleLogDriver,
} run(main, drivers);
[Cycle.js] Read effects from the DOM: click events的更多相关文章
- [Cycle.js] Fine-grained control over the DOM Source
Currently in our main() function, we get click$ event. function main(sources) { const click$ = sour ...
- [Cycle.js] Customizing effects from the main function
How can we show one string on the DOM, and a completely different string on Console log? This lesson ...
- [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] Making our toy DOM Driver more flexible
Our previous toy DOM Driver is still primitive. We are only able to sends strings as the textContent ...
- [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 ...
- 学习RxJS:Cycle.js
原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...
- jquery.cycle.js简单用法实例
样式: a{text-decoration: none;} *{;;} /*容器设置*/ .player { width:216px; height:248px; background:url(htt ...
- [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] 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 ...
随机推荐
- magento产品eav笔记【持续跟新...】
//magento把产品信息分在子表中,最顶上的表是catalog_product_entity,仅仅包含产品的信息(SKU) //表eav_attribute,这张表在magento里为全部不 同的 ...
- Mysql查看连接端口及版本
C:\Users\Administrator>mysql -uroot -pEnter password: *****Welcome to the MySQL monitor. Commands ...
- ORA-02095: specified initialization parameter cannot be modified
输入命令:alter system set utl_file_dir='/home/oracle/logmnr' scope=spfile; 报错: 出错原因:没有用spfile文件启动数据库 解决办 ...
- linux 进程备忘
进程间同步互斥方式: 1.管道pipe(亲属进程适用),命名管道fifopipe 2.信号量 3.共享内存配合信号量
- [Mugeda HTML5技术教程之8]添加行为
上一节我们已经在新建的作品中添加了元素和动画,如果我们想要作品能够和用户互动,就需要给元素添加动作行为.在舞台上选中一个要添加动作的元素,在属性栏的动作下拉列表中选择一个动作.可选类别有链接.表单.行 ...
- Zepto源码笔记(一)
最近在研究Zepto的源码,这是第一篇分析,欢迎大家继续关注,第一次写源码笔记,希望大家多指点指点,第一篇文章由于首次分析原因不会有太多干货,希望后面的文章能成为各位大大心目中的干货. Zepto是一 ...
- 讨论一下PHP相关技能的层次
1.PHP编程能力 由于PHP的入门较为简单,所以暂时只有熟悉和精通两个级别. 1.熟悉PHP:精通PHP语法,掌握常用的函数,熟悉PHP5下的OOP应用,这个是基础,也没什么好说的. 2.精通PHP ...
- DNS预获取(dns-prefetch)
今天翻看twitter的源码的时候看到了一下内容: <link rel=”dns-prefetch” href=”http://a0.twimg.com”/> <link rel=” ...
- Android用gif做启动页
公司的一个app的启动页想改为gif图,之前没有在android中加入过gif,所以赶紧饿补! 前言 我们都知道ImageView是不能完美加载Gif格式的图片,如果我们在ImageView中src指 ...
- Regular expression cheat sheet
\s white-space characters \S Non-white-space characters \d digital numbers \D non-digital numbers \w ...