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的更多相关文章

  1. [Cycle.js] Fine-grained control over the DOM Source

    Currently in our main() function,  we get click$ event. function main(sources) { const click$ = sour ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 学习RxJS:Cycle.js

    原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...

  7. jquery.cycle.js简单用法实例

    样式: a{text-decoration: none;} *{;;} /*容器设置*/ .player { width:216px; height:248px; background:url(htt ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. REST深入浅出

    不知你是否意识到,围绕着什么才是实现异构的应用到应用通信的“正确”方式,一场争论正进行的如火如荼:虽然当前主流的方式明显地集中在基于SOAP.WSDL和WS-*规范的Web Services领域,但也 ...

  2. python:字符串取值

    某个字符串为stmp="abcdef54321" 取前面5个stmp[:5] #abcde 取后面5个stmp[-5:] #54321 从前面开始取,不包括最后两个stmp[:-2 ...

  3. Resharper

    http://baike.baidu.com/link?url=H8DVtrvKV1Cg-Hrz82C6ZiJOUXbi_3BfoROe-RlHhctPna4-BFfglPh2OsR-KmCqRZ7_ ...

  4. .Net HttpClient 模拟登录微信公众平台发送消息

    1.模拟登录 public WeiXinRetInfo ExecLogin(string name, string pass) { CookieContainer cc = new CookieCon ...

  5. iPhone手机的屏幕尺寸、分辨率及适配

    1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...

  6. static在类中的定义,和enum的用法

    class A { // static int a = 1;//错误,静态变量在类外定义 static int a; static const int b = 1;//如果是静态成员常量,则可以在类内 ...

  7. eclipse安装ADT插件重启后不显示Android SDK Manager和Android Virtual Device Manager图标的一种解决办法

    通常安装,搭建安卓环境后,不显示Android SDK Manager和Android Virtual Device Manager ize解决方法:Eclipse ->window->c ...

  8. sql 查找数据库中某字符串所在的表及字段

    declare   @str   varchar(100)     set   @str='是否严格控制'     --要搜索的字符串         declare   @s   varchar(8 ...

  9. amchart

    amchart能够根据提供的数据便捷的生成好看的图标,曾在项目中遇到使用falsh版以支持对js支持不好的低版本浏览器,但是现在官网上都是js版本的,flash版的文档都没有,搜索结果一般都是链接到博 ...

  10. POJ1850 组合数学

    POJ1850 问题重述: 用26个小写字母进行编码,编码规则如下: 1)每个编码中前一个字母必须小于后一个字母 2)编码按照长度从小到大排列,相同长度按字典序进行排列 输入一个字母串,求解该编码对应 ...