博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Cycle.js] Generalizing run() function for more types of sources
阅读量:5964 次
发布时间:2019-06-19

本文共 2327 字,大约阅读时间需要 7 分钟。

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

 

转载地址:http://hmvax.baihongyu.com/

你可能感兴趣的文章
划分数系列问题
查看>>
springboot整合jersey
查看>>
Hibernate实体对象的生命周期(三种状态)
查看>>
23. Merge k Sorted Lists
查看>>
Python:pygame游戏编程之旅七(pygame基础知识讲解1)
查看>>
java B转换KB MB GB TB PB EB ZB
查看>>
通过SharpZipLib实现文件夹压缩以及解压
查看>>
20145209预备作业02
查看>>
精通CSS滤镜(filter)
查看>>
弄清楚高层到底是什么情况!
查看>>
HDU 4374 One hundred layer(单调队列DP)
查看>>
OPP Services Log
查看>>
JQuery增删改查
查看>>
android webview 全屏播放H5 (Playing HTML5 video on fullscreen in android webview)
查看>>
python的一些常用函数
查看>>
微信公众号教程(19)微信音乐播放器开发 中
查看>>
浏览器跨域问题
查看>>
部署WEB项目到服务器(二)安装tomcat到linux服务器(Ubuntu)详解
查看>>
SpringBoot之SpringBoot+Mybatis+Mysql+Maven整合
查看>>
SQLServer BI 学习笔记
查看>>