zoukankan      html  css  js  c++  java
  • [RxJS] AsyncSubject: representing a computation that yields a final value

    This lesson will teach you about AsyncSubject, another type of Subject with some replaying logic inside. We will also look at some use cases for this peculiar RxJS subject variant.

    AsyncSubject:

    Emit last value only when sequence completed. 

    BehaviorSubject:

    Replay onces, only before compleation.

    ReplaySubject:

    Replay many, before of after compleation.

    var subject = new Rx.AsyncSubject();
    
    // Subject
    // ReplaySubject: replays many, before or after completion
    // BehaviorSubject: replays one, only before completion
    // AsyncSubject: replays one, only if completed
    
    var observerA = {
      next: function (x) { console.log('A next ' + x); },
      error: function (err) { console.log('A error ' + err); },
      complete: function () { console.log('A done'); },
    };
    
    subject.subscribe(observerA);
    console.log('observerA subscribed');
    
    var observerB = {
      next: function (x) { console.log('B next ' + x); },
      error: function (err) { console.log('B error ' + err); },
      complete: function () { console.log('B done'); },
    };
    
    setTimeout(() => subject.next(1), 100);
    setTimeout(() => subject.next(2), 200);
    setTimeout(() => subject.next(3), 300);
    setTimeout(() => subject.complete(), 350);
    
    /*
    ----1---2---3--|       
      .............3|
                       3|
    */
    
    setTimeout(function () {
      subject.subscribe(observerB);
      console.log('observerB subscribed');
    }, 400);
    /*
    "observerA subscribed"
    "A next 3"
    "A done"
    "B next 3"
    "B done"
    "observerB subscribed"
    */
  • 相关阅读:
    bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】
    【20】AngularJS 参考手册
    【19】AngularJS 应用
    【18】AngularJS 包含
    【17】AngularJS Bootstrap
    【16】AngularJS API
    【15】AngularJS 输入验证
    【14】AngularJS 表单
    【13】AngularJS 模块
    【12】AngularJS 事件
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5977464.html
Copyright © 2011-2022 走看看