Currently the code looks like :

// 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 sinks = main();
DOMEffect(sinks.DOM);
consoleLogEffect(sinks.Log);

We still have this part of code which didn't wrap into a function, we can wrap into a run() function, this can provide a main enterence for the code:

function run(mainFn){
const sinks = mainFn();
DOMEffect(sinks.DOM);
consoleLogEffect(sinks.Log);
} run(main);

Let's say we want to remove consoleLogEffect, current way we need to comment out from the main() function.

The problems are that in the first place we are hard coding these effects inside run. Instead, we should be able to specify that these are the effects that I want to run my main function, so we need to give our effects to run as well.

That will be an object. Effects functions will be an object, and the keys will match those keys that we saw from the sync here. The DOM function is DOM Effect, and the log function is consoleLogEffect.

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);

The last thing author introduce the 'driver' as the name instead of 'effect' ...

// 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 DOMDriver(text$) {
text$.subscribe(text => {
const container = document.querySelector('#app');
container.textContent = text;
});
} function consoleLogDriver(msg$) {
msg$.subscribe(msg => console.log(msg));
} function run(mainFn, drivers) {
const sinks = mainFn();
Object.keys(drivers).forEach(key => {
drivers[key](sinks[key]);
});
} const drivers = {
DOM: DOMDriver,
Log: consoleLogDriver,
} run(main, drivers);

[Cycle.js] Introducing run() and driver functions的更多相关文章

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

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

  3. [Cycle.js] Main function and effects functions

    We need to give structure to our application with logic and effects. This lessons shows how we can o ...

  4. 学习RxJS:Cycle.js

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

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

  6. RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景?

    RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? - 知乎 https://www ...

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

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

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

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

随机推荐

  1. hdu 2101

    #include <stdio.h> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { i ...

  2. pd的django To do list教程-----(2)models模型的建立

    1:在models.py中建表 from django.db import models class Tcontent(models.Model): content = models.CharFiel ...

  3. (转)使用Microsoft Web Application Stress Tool对web进行压力测试

    http://www.blogjava.net/crespochen/archive/2009/06/02/279538.html Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试 ...

  4. C# DateTime的ToString()方法的使用

    Console.WriteLine("ToShortDateString:" + DateTime.Now.ToShortDateString()); Console.WriteL ...

  5. 多维背包 hrbudt 1335 算法与追MM

    hrbust #include<string.h> //多进制储存数,第i位进制维back[i]+1,可以避免重复 #include<stdio.h> using namesp ...

  6. VC++深入详解读书笔记-第六章菜单

    1.MFC都是采用大写字母来标识资源ID号. 2.程序中会用到多种资源,在为资源确定其ID号时,为了明确区分资源类型,一般都遵循这样一个原则:在“ID”字符串后加上一个标识资源类型的字母,例如,我们给 ...

  7. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  8. php 之 类,对象

    --恢复内容结束--- 一.类和对象: 1.定义: 对象:我们所见到的东西都可以称之为对象,是类实例化出来的东西 类:是对所有的同类对象抽象出来的东西 eg: 在一张表中记录了全班同学的学号,姓名,性 ...

  9. JQ点击列表显示隐藏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Gradle Android客户端程序打包(基于gradle 2.10版本验证通过)

    一.前言 目前正在准备从eclipse开发环境向AndroidStudio迁移,提前过去探探路,不出所料,原来gradle脚本果然报错,无法运行,想想索性把本地的gradle一起升级到最新版本,毕竟1 ...