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 mapfilter, 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [RxJS] Reactive Programming - Rendering on the DOM with RxJS

    <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Vickers Vane Pump - How To Choose Vane Pump Parameter Specifications?

    1 rated pressure selection. The rated pressure of the vane pump products is 7MPa, 1OMPa, 16MPa, 2lMP ...

  2. PHP16 PHP访问MySQL

    学习要点 PHP访问MySQL配置 PHP访问MySQL函数介绍 足球赛程信息管理 PHP访问MySQL配置 PHP.ini配置文件确认以下配置已经打开 extension=php_mysql.dll ...

  3. c:if标签--判断不为空和其他的值判断

      用<c:if test=""></c:if>标签时 <c:if test="${sl.chc_status==1 }"> ...

  4. 配置maven报错 the java_home environment variable is not defined correctly ......

    the java_home environment variable is not defined correctly This environment variable is needed to r ...

  5. 哈理工(HUST)第八届程序设计竞赛--小乐乐的组合数

    这道题目是一道数学题,我们可以假设n为7,m为14. 这样的话我们就可以很清晰地看到7和7可以拼接在一起,这是一对,然后是7和14拼接在一起,第二对. 我们可以直接让n/7,m/7,这样就是1*2,就 ...

  6. centos6 下FastDFS 在storage节点上nginx的fastdfs-nginx-module 模块编译出现的问题

    centos6.6  下FastDFS  在storage节点上   make(编译)nginx的fastdfs-nginx-module 出现如下报错: /root/fastdfs-nginx-mo ...

  7. JavaIO基础学习笔记

    JavaIO JavaIO即Java的输入输出系统.比如我们的程序要读取一个文本文件.一张图片或者要获取控制台输入的内容,就要用到输入流:又或者程序要将生成的一段字符窜以文件的形式保存到系统中就要用到 ...

  8. mysql 常用命令(二)

    1.创建数据库,并制定默认的字符集是utf8. CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_g ...

  9. ul标签中,li标签的移除、属性值获取

  10. Pychorm提示Unresolved reference 导入模块报错

    最近使用Pychorm编写Python时,每次要引入自定义模块,就会报错,提示“Unresolved reference” Unresolved reference 'LoginClass' more ...