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

  1. [Cycle.js] Introducing run() and driver functions

    Currently the code looks like : // Logic (functional) function main() { return { DOM: Rx.Observable. ...

  2. 学习RxJS:Cycle.js

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

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

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

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

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

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

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

  8. jquery.cycle.js

    jquery.cycle.js简单用法实例 样式: a{text-decoration: none;} *{margin:0; padding:0;} /*容器设置*/ .player { width ...

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

随机推荐

  1. 设计模式(Java版)-创建型模式之简单工厂模式

    前言:这段时间在学习设计模式,本人也是小菜一枚(所以写的如果有错误的地方请大大们给予指出).这个东西也是我一直想学习的,从点点滴滴做起,记录下自己每天的领悟! 一.工厂模式的动机 在软件系统中,经常面 ...

  2. GPG error [...] NO_PUBKEY [...]

    今天在Linux下遇到这个问题,发现很多资料都是英文的,为了方便出现同样错误的有英语阅读困难的人,整理解决方案如下: sudo apt-key adv --keyserver keyserver.ub ...

  3. mybatis知识总结

    基于昨天的mybatis入门详解,今天我们再来看看mybatis稍微高深些的知识点. 1.解决Model属性和数据库字段不一致的问题 1),开启驼峰命名 2),使用resultMap进行映射, < ...

  4. 服务 远程服务 AIDL 进程间通讯 IPC

    Activity aidl接口文件 package com.bqt.aidlservice;  interface IBinderInterface {     /* 更改文件后缀为[.aidl]去掉 ...

  5. WPF 制作圆角按钮

    在程序对应坐置插入以下代码,或是先拖一个按钮控件到窗体中,再替换对应的代码. 修改 CornerRadius="18,3,18,3"  就可以改变圆角大小 按钮效果: <Bu ...

  6. WCF 客户端与服务端消息传输

    WCF很多需要认证信息,保证服务的安全,可以使用消息来实现 WCF 实现消息的方式: WCF中有两个接口: IClientMessageInspector [定义一个消息检查器对象,该对象可以添加到 ...

  7. C#摇奖程序

    private void Form1_Load(object sender, EventArgs e) { //取消跨线层访问控件的判断 Control.CheckForIllegalCrossThr ...

  8. (原)python中matplotlib的颜色及线条控制

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6117528.html 参考网址: http://stackoverflow.com/questions ...

  9. hibernate级联保存,更新个人遇到的问题

    在级联更新的时候,数据库中的数据是增加的,只是外键不存在,导致这样的问题产生的原因是,字表主键ID没有添加到集合中,导致Hibernate找不到子项而执行更新.

  10. Chrome调试nodejs

    1.安装node-inspector 命令: npm install -g node-inspector 2.nodemon --debug xxx.js启动项目(如果使用--debug-brk 就会 ...