[RxJS] Using Observable.create for fine-grained control
Sometimes, the helper methods that RxJS ships with such as fromEvent
, fromPromise
etc don't always provide the exact values you want & you end up having to do extra work to force them into the shape you require. For more fine-grained control you can use Observable.create
which allows you to project only the values which matter to you.
For example, the following code need add a filter method to filter the null value from the event.
const fromEvent = Rx.Observable.fromEvent; function delegate (wrapperSelector, elementSelector, eventName) { return fromEvent(
document.querySelector(wrapperSelector),
eventName,
(ev) => {
return ev.target.closest(elementSelector);
}
).filter(x => x !== null)
} delegate('.wrapper', 'button', 'click')
.subscribe(x => {
document.querySelector('#output').textContent = `Button ${x.textContent} clicked`;
});
We can use create method to do:
const create = Rx.Observable.create; function delegate (wrapperSelector, elementSelector, eventName){
return create( (observe)=> {
const wrapper = document.querySelector(wrapperSelector);
const handler = (ev) => {
const match = ev.target.closest(elementSelector);
if(match) {
return observe.onNext(match);
}
} wrapper.addEventListener(eventName, handler, false); // cancel the listener
return ()=>{
wrapper.removeEventListener(eventName, handler);
}
});
}
So to recap, sometimes the values that are projected from the helpers that RXJS ships with, are not exactly what you want, and you end up having to do a little bit of extra work to force them into the shape that want. If you find yourself doing this, you can always use create instead.
Create is the factory function that creates an anonymous observable for you, gives you the opportunity to only project the values that you want. You end up having to do a bit more manual work, such as adding event listeners and removing them finished with, but the result will be greater performance and greater control.
[RxJS] Using Observable.create for fine-grained control的更多相关文章
- [RxJS] Creation operator: create()
We have been using Observable.create() a lot in previous lessons, so let's take a closer look how do ...
- Angular学习笔记—RxJS与Observable(转载)
1. Observable与观察者模式的关系 其实这里讲的Observable就是一种观察者模式,只不过RxJS把Observable结合了迭代模式以及附件了很多的operator,让他变得很强大,也 ...
- [RxJS] Creating Observable From Scratch
Get a better understanding of the RxJS Observable by implementing one that's similar from the ground ...
- ng-packagr 打包报错 Public property X of exported class has or is using name 'Observable' from external module “/rxjs/internal/Observable” but cannot be named
old import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; @Injectable( ...
- Create Hierarchical Tree To Control Records In Oracle Forms
Download Source Code Providing an example form for creating hierarchical trees in Oracle Forms to co ...
- Rxjs 修改Observable 里的值
有这么一个对象c$: Observable<any> 修改里边的值: 声明一个subject subject: Subject<any>; 在ngOnInit()中进行初始化 ...
- [RxJS] Hot Observable, by .share()
.share() is an alias for .publish().refCount(). So if the source is not yet completed, no matter how ...
- Springfox Reference Documentation
1. Introduction The Springfox suite of java libraries are all about automating the generation of mac ...
- [转载] 跟着实例学习zookeeper 的用法
原文: http://ifeve.com/zookeeper-curato-framework/ zookeeper 的原生客户端库过于底层, 用户为了使用 zookeeper需要编写大量的代码, 为 ...
随机推荐
- ORA-24324、ORA-12560、ORA-12514
SQL> startup ERROR: ORA-24324: 未初始化服务句柄 ORA-01041: 内部错误, hostdef 扩展名不存在. SQL> conn sys /nolog; ...
- 利用iptables实现基于端口的网络流量统计
如何统计某个应用的网络流量(包括网络流入量和网络流出量)问题,可以转换成如何基于端口号进行网络流量统计的问题.大部分网络应用程序都是传输层及以上的协议,因此基于端口号(tcp, udp)统计网络流量基 ...
- eclipse项目转android studio详解
第一步:项目导入 向AS中导入项目的方法有两种(其实是一种). 方法一:是在eclipse中先导出为gradle(如图1),然后打开AS,找到项目中的gradle文件,直接导入. 方法二:直接在AS中 ...
- ewebeditor下利用ckplayer增加html5 (mp4)全平台的支持
学校数字化平台富文本编辑器一直用的ewebeditor,应该说非常的好,支持常用office文档的直接导入,极大的方便了老师们资料的上传,最近在规划整个数字化校园向全平台改版,框架采用bootstra ...
- DTO学习系列之AutoMapper(二)
本篇目录: Flattening-复杂到简单 Projection-简单到复杂 Configuration Validation-配置验证 Lists and Array-集合和数组 Nested m ...
- 18 java 代理模式 (转)
静态代理 1.新建一个接口,这个接口所提供的方法是关于数据库操作的 public interface EmployeeDao { public void updateSalary(); } 2.建一个 ...
- linux上安装apache以及httpd.conf基本配置
1.yum安装apache #yum install httpd -y 2.随系统自启动 #chkconfig httpd on 3.开启apache #service httpd start PS: ...
- android 点击水波纹效果
这里是重点,<ripple>是API21才有的新Tag,正是实现水波纹效果的; 其中<ripple android:color="#FF21272B" .... ...
- python的and与or剖析
1.只含有and的表达式 In []: and True and ' Out[]: ' In []: and and True and 'long' Out[]: 从左向右,遇到False,则返回改值 ...
- 用委托、匿名函数、Lambda的方式输出符合要求的数
最近看了一些博客,对委托和匿名函数和Lambda的方式有了一些更深的理解,在前人的基础上.我也写3个例子 using System; using System.Collections.Generic; ...