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内存模型(下)

    一.happens-before happens-before是JMM最核心的概念.对于Java程序员来说,理解happens-before是理解JMM的关键. 1.1 JMM的设计 从JMM设计者的 ...

  2. SVN服务器的本地搭建和使用

    用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用. 详细步骤如下 http://www.2cto.com/os/201412/361931.html

  3. css选择器基本属性

    选择器一,相邻选择器: 1,相邻选择器 1),定义:相邻选择器匹配指定元素的相邻兄弟元素 2),用法:如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使用相邻兄弟选择器 3),表示符 ...

  4. asp.net "callback" 和 "postback" 的区别.

    下图解释了基于 asp.net的 "postback" 和 "callback"的生命周期: Postback 是在将 整个页面的数据 从 client 提交到 ...

  5. explicit 只对构造函数起作用,用来抑制隐式转换。

    class A { private: int a; public: A(int x) :a(x){} void display(){ cout << a << endl; } ...

  6. The Suspects

    算法:并查集 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not-Spre ...

  7. Jade学习笔记

    初学nodejs,折腾过用handlebars做模板,后来隔了一段重新学习,用了jade,真心简洁……记录一些学习笔记,以备复习. jade是基于缩进的,所以tab与space不能混用: 属性的设置: ...

  8. 关于js中的事件

    这是第一篇技术性博客. 因为最近做的web版前端求职简历算是告一段落了(点此看简历).(稍微记录下吧:自从确定简历的简笔画风格后(因为刚开始设想的蓝天白云大树啥的不仅图片特难找而且做着做着就觉得有点俗 ...

  9. dede分页

    {dede:list pagesize ='} <div class="intro clearfix"> <ul class="pic"> ...

  10. Python新手学习基础之运算符——比较运算符

    比较运算符 比较运算符可以使用比较两个值,所有的内建类型都支持比较运算.当用运算符比较两个值时,结果是一个逻辑值,不是True,就是False. 有一点要注意的是,不同的类型的比较方式不一样,数字类型 ...