[RxJS] Create a Reusable Operator from Scratch in RxJS
With knowledge of extending Subscriber and using source.lift to connect a source to a subscriber, you can now create your own operators by writing functions that return a source.lift call. This lesson creates a simple "multiply" operator in RxJS.
index.js:
import { from, Subscriber } from "rxjs";
import { multiply } from "./multiply";
const observable$ = from([1, 2, 3, 4, 5]);
const subscriber = {
next: value => {
console.log(value);
},
complete: () => {
console.log("done");
},
error: value => {
console.log(value);
}
};
observable$.pipe(multiply(3)).subscribe(subscriber);
multiply.js:
import { Subscriber } from "rxjs";
class MultiplySubscriber extends Subscriber {
constructor(subscriber, number) {
super(subscriber);
this.number = number;
}
_next(value) {
this.destination.next(value * this.number);
}
}
export const multiply = number => source => {
return source.lift({
call(sub, source) {
source.subscribe(new MultiplySubscriber(sub, number));
}
});
};
The most common scenario for creating custom operators is to reuse the built-in operators shipped with RxJS. You'll find yourself re-using map, filter, and others will solve most of the problems you come across.
import { map } from "rxjs/operators";
export const mul = number => map(v => v * number);
[RxJS] Create a Reusable Operator from Scratch in RxJS的更多相关文章
- [RxJS] Implement the `map` Operator from Scratch in RxJS
While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...
- [RxJS] Aggregating Streams With Reduce And Scan using RxJS
What is the RxJS equivalent of Array reduce? What if I want to emit my reduced or aggregated value a ...
- [RxJS] Toggle A Stream On And Off With RxJS
This lesson covers how to toggle an observable on and off from another observable by showing how to ...
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...
- [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()
In currently implemention, there is one problem, when the page load and click refresh button, the us ...
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- [RxJS] Implement pause and resume feature correctly through RxJS
Eventually you will feel the need for pausing the observation of an Observable and resuming it later ...
- [Functional Programming] Create Reusable Functions with Partial Application in JavaScript
This lesson teaches you how arguments passed to a curried function allow us to store data in closure ...
- advanced dom scripting dynamic web design techniques Chapter 2 CREATING YOUR OWN REUSABLE OBJECTS
JavaScript is all about objects. Objects are the foundation of everything, so if you’re unfamiliar w ...
随机推荐
- drawer 抽屉 弹框 在 modal的后面的解决方案
drawer 抽屉 弹框 在 modal的后面的解决方案 方案1 在框内 弹出 <Drawer title="拍照" :transfer="false" ...
- 我的app自动化实战练习一
''' -*- coding: utf-8 -*- @Time : 2019/6/10 0010 10:39 @Author : 无邪 @File : test_data.py @Software: ...
- error C2143: 语法错误 : 缺少“;”(在“&”的前面)
报错: error C2143: 语法错误 : 缺少“;”(在“&”的前面) 代码: #include <iostream> ostream & << (ost ...
- Gear Pump: Why Install A Pressure Reducing Valve?
When the Gear Pump Manufacturers prompts to install a gear pump, the following points should ...
- 小程序02 wxml和wxss
微信小程序的排版就跟wxml和wxss有关,它们两者相当于HTML和CSS,其中wxml指定了界面的框架结构,而wxss指定了界面的框架及元素的显示样式. 一.wxml 界面结构wxmL比较容易理解, ...
- linux查看内存和释放内存
linux: 查看内存:free -m 释放内存:echo 1 > /proc/sys/vm/drop_caches
- navicate备份与还原数据库
参考:https://jingyan.baidu.com/article/574c521977dea06c8d9dc1b1.html 1.备份数据库 首先新建数据库 选中数据库下方的“备份”,右击“新 ...
- css 浮动规则
1. 如果浮动元素的高度大于父级元素,那么浮动元素块会超过父级元素的底部.解决办法:将父级元素也设置为浮动定位. 2. 不改变box尺寸的情况下增加box内部的内边距:box-sizing: bord ...
- hdu3094 A tree game
题目描述 题解: 树上删边. $SG[u]$^=$SG[son[u]]+1$ 代码: #include<cstdio> #include<cstring> ; template ...
- MYSQL数据库攻防与加固
这是“官方”原本的模样搬过来的..写的很粗略啊.还有篇详细的请查看:MySQL安全加固题目及答案参考解析 启动xserver-mysql,进入xserver-mysql,开始实验,实验步骤如下: 1. ...