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. Navicat连不上MySQL的解决办法

    USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{your password}'; ...

  2. ORA-03113: end-of-file on & ORA-07445

    --------------ORA-03113: end-of-file on-------------- SQL> show parameter background_dump; NAME T ...

  3. jquery 拖动(Draggable) 约束运动,输出数组排序Array

    <!doctype html><html lang="en"><head> <meta charset="utf-8" ...

  4. 模拟、字符串--P1042 乒乓球 题解

    P1042 乒乓球 字符串string的基本使用 #include <iostream> #include <algorithm> #include <map> # ...

  5. set指令详解

    set指令详解 功能说明:设置shell 语 法:set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...] 说 明:设置或取消设置shell选项 ...

  6. springMVC model传对象数组 jq 获取

    这个问题网上没有什么解答,有两种可能性: 一.我使用的这种方法实在太蠢了正常人都不会去这个搞: 二.我太蠢了.... 以下解决方案 //后台代码如下 public String plant(Model ...

  7. xtu summer individual 1 D - Round Numbers

    D - Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u D ...

  8. zoj 2886 Look and Say

    Look and Say Time Limit: 2 Seconds      Memory Limit: 65536 KB The look and say sequence is defined ...

  9. PTA 03-树1 树的同构 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

  10. POJ 1236 学校网络间的强连通

    题目大意: N个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输.问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.问题2:至少需要添加几条 ...