• [Javascript] Broadcaster + Operator + Listener pattern -- 12. Repeat a broadcaster that is DONE


    After a broadcaster is "done", starting it up again requires calling the broadcaster with the same listener. This creates a kind of "async recursion" where you pass the listener back into the broadcaster each time you set it up. This lesson walks through creating a repeat operator that can start a broadcaster over.

    Idea for Repeat operator, is that when source broadcaster emit done, then in repeat operator call the broadcaster(repeatListener) again.

    let repeat = (broadcaster) => (listener) => {
      let cancel;
      let repeatListener = (value) => {
        if (value === done) {
          if (cancel) {
            cancel();
          }
          cancel = broadcaster(repeatListener);
          return;
        }
    
        listener(value);
      };
      cancel = broadcaster(repeatListener);
    
      return () => {
        cancel();
      };
    };

    import { createTimeout, done } from "./broadcasters";
    import { hardCode } from "./operators";
    
    let repeat = (broadcaster) => (listener) => {
      let cancel;
      let repeatListener = (value) => {
        if (value === done) {
          if (cancel) {
            cancel();
          }
          cancel = broadcaster(repeatListener);
          return;
        }
    
        listener(value);
      };
      cancel = broadcaster(repeatListener);
    
      return () => {
        cancel();
      };
    };
    
    let one = repeat(hardCode("hi")(createTimeout(1000)));
    
    let cancel = one(console.log);
    setTimeout(() => {
      cancel();
    }, 5000);
    
    
    // broadcasters.js
    import { curry } from "lodash"
    export let done = Symbol("done")
    
    export let createTimeout = curry((time, listener) => {
      let id = setTimeout(() => {
        listener(null)
        listener(done)
      }, time)
    
      return () => {
        clearTimeout(id)
      }
    })
    
    
    // operators.js
    import { curry } from "lodash";
    import { done } from "./broadcasters";
    
    let createOperator = curry((operator, broadcaster, listener) => {
      return operator((behaviorListener) => {
        return broadcaster((value) => {
          if (value === done) {
            listener(done);
            return;
          }
    
          behaviorListener(value);
        });
      }, listener);
    });
    
    export let hardCode = (newValue) =>
      createOperator((broadcaster, listener) => {
        let cancel = broadcaster((value) => {
          listener(newValue);
        });
    
        return () => cancel();
      });
    

      

  • 相关阅读:
    BizTalk开发系列(五) 属性字段
    BizTalk开发系列(二十一) Mapping 扩展开发
    BizTalk Server 2009 Beta初体验
    BizTalk开发系列(二十三) BizTalk性能指标参考
    BizTalk开发系列(三十三)BizTalk之Excel终极解决方案
    BizTalk开发系列(十九) BizTalk命名规范
    BizTalk开发系列(十五) Schema设计之Qualified 与Unqualified
    BizTalk开发系列(十三) Schema设计之值约束
    BizTalk开发系列(十) ESB Guidance安装笔记
    jQuery练手之简易聊天室
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13917430.html
Copyright © 2020-2023  润新知