zoukankan      html  css  js  c++  java
  • [RxJS] Creation operator: create()

    We have been using Observable.create() a lot in previous lessons, so let's take a closer look how does it work.

    The create function:

    var foo = Rx.Observable.create( function(observer){
      observer.next(42);
      observer.next(100);
      observer.next(200);
      observer.complete();
    })  ;
    
    foo.subscribe( 
      (x)=>{console.log('next ' + x);},
      (err)=>{console.log('err ' + err);},
      ()=>{console.log('done');},
    )

    In deep, create() function equal to new Rx.Observable():

    var foo = new Rx.Observable( function(observer){
      observer.next(42);
      observer.next(100);
      observer.next(200);
      observer.complete();
    })  ;

    And this also equal to:

    function subscribe(observer){
      observer.next(42);
      observer.next(100);
      observer.next(200);
      observer.complete();
    }
      
    var foo = new Rx.Observable( subscribe );

    So, if we get rid of RxJS, then we can create the create() function like:

    function subscribe(observer){
      observer.next(42);
      observer.next(100);
      observer.next(200);
      observer.complete();
    }
    
    var observer = {
      next: (x)=>{console.log('next ' + x);},
      error: (err)=>{console.log('err ' + err);},
      complete: ()=>{console.log('done');}
    }
    
    
    subscribe(observer);

    Of course, it's useful to have the observable type because then it has all those nice operators that we saw and that we are also seeing new operators coming next. If you paid attention, then you're going to remember that in the subscribe, we had previously three functions here as argument. Instead of an object, as we have now, we had just these three functions.

    Also, the observable type, it converts these three functions into an observer object. Before it calls this, it will actually take these three functions and put labels in front of them like that, to create the observer object. It's normalizing it.

  • 相关阅读:
    1分钟解决VS每次运行都显示“正在还原nuget程序包”问题
    C#多线程和异步(一)——基本概念和使用方法
    owin使用
    使用DotNetOpenAuth搭建OAuth2.0授权框架
    DotNetOpenAuth实践之搭建验证服务器
    DotNetOpenAuth实践系列
    Android使用zxing生成二维码
    漂亮的Android表格框架
    Android控件七:视图-图片-文字切换器ViewAnimator
    Android学习随笔--ListView的分页功能
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5401422.html
Copyright © 2011-2022 走看看