zoukankan      html  css  js  c++  java
  • [Cycle.js] Introducing run() and driver functions

    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);
    
      
  • 相关阅读:
    父母的房产继承买卖赠予以及网络红包代金券优惠券的国家最新税法规定
    家里这7个地方要装柜子,少装一个都是灾难!
    centos里的压缩解压命令tar总结
    2019年9月2日开学!寒假时间也定了……
    理赔时很容易出差错的3个“隐蔽”点
    Linux命令:用“dirs”、“pushd”、“popd”来操作目录栈
    python金融反欺诈-项目实战
    模型分数分布
    PSi-Population Stability Index (PSI)模型分稳定性评估指标
    Kolmogorov–Smirnov test(KS)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5178061.html
Copyright © 2011-2022 走看看