美文网首页
TS代码中关于闭包

TS代码中关于闭包

作者: 三月木头 | 来源:发表于2021-01-19 16:24 被阅读0次

    问题:
    在看代码时候,发现一个对象中的某个订阅在重连的时候就会发起请求,所以找公司元老问了问,现在就这个方面进行解释一下。

    constructor(eventManager: EventManager, public marketService: MarketService) {
        super(eventManager, ExCommonService.name);
        marketService.readySubject.subscribe({
          next: (ready) => {
            if (ready) {
              this.symbolKey = store.getters.currentSymbol;
              if (this.symbolKey) {
                this.subscribeDefaultUpdateEvent();
              }
              this.requestOtcStateRequest().then((res) => {
                if (res.enable != null) {
                  store.commit("setOtcEnable", res.enable);
                }
              });
              this.subscribeOtcStateUpdate();
              this.readySubject.next(true);
            }
          },
        });
      }
    

    在 ExCommonService 这个对象的方法中存在这样一段代码,其中有对otc状态的请求,但是我不明白的是这个readySubject 方法什么时候调用的。
    后来看了一下MarketService 这个类里面找到重连时候触发的this.readySubject.next(true);这里当next传入true的时候,我们会调用上面那个方法。也就是一个闭包函数被调用。

    什么是闭包函数
    https://baike.baidu.com/item/%E9%97%AD%E5%8C%85%E5%87%BD%E6%95%B0/110261?fr=aladdin

    相关文章

      网友评论

          本文标题:TS代码中关于闭包

          本文链接:https://www.haomeiwen.com/subject/wstczktx.html