zoukankan      html  css  js  c++  java
  • [Javascript] Broadcaster + Operator + Listener pattern -- 4. Concat

    import { compose } from "ramda";
    let input = document.getElementById("input");
    
    let inputBroadcaster = (listener) => {
      input.addEventListener("input", listener);
    
      return () => {
        input.removeEventListener("input", listener);
      };
    };
    
    let log = (value) => {
      console.log(value);
    };
    
    let getTargetValue = (broadcaster) => (listener) => {
      let stop = broadcaster((event) => {
        listener(event.target.value);
      });
      return stop;
    };
    
    let done = Symbol("done'");
    
    let timeout = (listener) => {
      setTimeout(() => {
        listener(done);
      }, 3000);
    };
    /*
    let concat = (firstBroadcaster, secondBroadcaster) => (listener) => {
      firstBroadcaster((value) => {
        if (value === done) {
          secondBroadcaster(listener);
        }
      });
    };*/
    
    let concat_forloop = (...broadcastera) => (listener) => {
      broadcastera.forEach((broadcaster, i) => {
        broadcaster((value) => {
          if (value === done && i + 1 <= broadcastera.length - 1) {
            broadcastera[i + 1](listener);
          }
        });
      });
    };
    
    let concat_recusive = (...broadcastera) => (listener) => {
      let i = 0;
    
      let setup = (broadcaster) => {
        broadcaster((value) => {
          if (value === done) {
            ++i <= broadcastera.length - 1
              ? setup(broadcastera[i])
              : listener(done);
          } else {
            listener(value);
          }
        });
      };
    
      setup(broadcastera[i]);
    };
    
    concat_recusive(timeout, inputBroadcaster)(log);
  • 相关阅读:
    struts2_maven_learning
    test_maven_实现表单验证
    Struts2_learning
    计算机信息安全技术_学习
    SQL_sql语言的学习
    ios视图加载时所涉及到的事件
    ios 判断版本更新
    ios 裁剪图片(1裁多)
    iOS 设置与配置
    ios Auto Layout中Stack View的使用
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13755927.html
Copyright © 2011-2022 走看看