[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();
observer.next();
observer.next();
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();
observer.next();
observer.next();
observer.complete();
}) ;
And this also equal to:
function subscribe(observer){
observer.next();
observer.next();
observer.next();
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();
observer.next();
observer.next();
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.
[RxJS] Creation operator: create()的更多相关文章
- [RxJS] Creation operator: of()
RxJS is a lot about the so-called "operators". We will learn most of the important operato ...
- rxjs自定义operator
rxjs自定义operator
- [RxJS] Creation operators: from, fromArray, fromPromise
The of() operator essentially converted a list of arguments to an Observable. Since arrays are often ...
- [RxJS] Using Observable.create for fine-grained control
Sometimes, the helper methods that RxJS ships with such as fromEvent, fromPromise etc don't always p ...
- [RxJS] Connection operator: multicast and connect
We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple obse ...
- [RxJS] Transformation operator: repeat
Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson ...
- [RxJS] Transformation operator: buffer, bufferCount, bufferTime
This lesson will teach you about another horizontal combination operator: buffer and its variants. B ...
- [RxJS] Transformation operator: scan
All of the combination operators take two or more observables as input. These operators may also be ...
- [RxJS] Combination operator: withLatestFrom
Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...
随机推荐
- xp snapshot.
snap current active window(alt + Print Screen SysRq). snap the whole window (Print Screen SysRq).
- NPOI对excel文件的导入导出
现理解:将一个Excel文件(工作簿-IWorkBook)看做是一个你要操作的对象,每个工作簿包含多个工作表(ISheet)对象,每个工作表中又包含多个行对象(IRow),每行又包含多个单元格(ICe ...
- 一起学makefile
Unix.Linux必学知识哈哈,网上看到一哥们写得挺好挺详细的,直接复制地址就分享哈哈哈. 跟我一起写 Makefile(一) 概述 跟我一起写 Makefile(二) make是如何工作的 跟我一 ...
- Protostuff自定义序列化(Delegate)解析
背景 在使用Protostuff进行序列化的时候,不幸地遇到了一个问题,就是Timestamp作为字段的时候,转换出现问题,通过Protostuff转换后的结果都是1970-01-01 08:00:0 ...
- 两个示例介绍JavaScript的闭包
JavaScript的闭包有两个用途:一个是访问函数内部的变量:另一个是让变量的值在作用域内保持不变.函数是JavaScript 中唯一有作用域的对象,因此JavaScript的闭包依赖于函数实现,下 ...
- 初涉JavaScript模式 (2) : 基本技巧
尽量少用全局变量 大量使用全局变量会导致的后果 全局变量创建以后会在整个JavaScript应用和Web页面中共享.所有的全局变量都存在于一个全局命名空间内,很容易发生冲突 不知不觉创建了全局变量 其 ...
- nodejs 初学笔记
首先到nodejs的官网安装nodejs,地址nodejs.org,网站第一页会根据你的电脑系统推荐你适合的版本,下载,不断next,在cmd中输入 node -v 可以看到版本的话,即安装成功. 说 ...
- jquery.cookie()方法
jquery.cookie.js是一个轻量级的cookie 插件,可以读取.写入.删除 cookie. 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. < ...
- display 属性
这里向大家描述一下HTML DOM display属性的定义和用法,HTML DOM display属性主要用来设置元素如何显示,比如当此属性值为none时,表示此元素不会被显示,而block则表示此 ...
- json数组对象和对象数组(转)
转自:http://www.cnblogs.com/zhangji/archive/2011/09/02/2163811.html 一.Json的简单介绍 从结构上看,所有的数据最终都可以分成三种类型 ...