While it's great to use the RxJS built-in operators, it's also important to realize you now have the knowledge to write them by yourself if needed. The mapoperator turns out to be a simple MapSubscriber which takes a function and applies it to the value passed to next.

map.js:

import { Subscriber } from "rxjs";

class MapSubscriber extends Subscriber {
constructor(sub, fn) {
super(sub);
this.fn = fn;
} _next(value) {
this.destination.next(this.fn(value));
}
} export const map = fn => source =>
source.lift({
call(sub, source) {
source.subscribe(new MapSubscriber(sub, fn));
}
});

multiply.js:

import { map } from "./map";

export const mul = number => map(v => v * number);

index.js:

import { from, Subscriber } from "rxjs";
import { multiply, mul } from "./multiply"; const observable$ = from([, , , , ]); const subscriber = {
next: value => {
console.log(value);
},
complete: () => {
console.log("done");
},
error: value => {
console.log(value);
}
}; observable$.pipe(mul()).subscribe(subscriber);
observable$.pipe(mul()).subscribe(subscriber);

最新文章

  1. VS2013中, 无法嵌入互操作类型“……”,请改用适用的接口的解决方法
  2. Quartz作业调度框架及时间表达式的含义和语法
  3. (Tree)94.Binary Tree Inorder Traversal
  4. 配置与使用 Git与Github
  5. codevs 1002 搭桥
  6. IOS中tableView每组的头部控件、通过tableView的代理方法控制某一行的cell能否达到高亮选中状态
  7. HTML5 面试中最常问到的 10 个问题
  8. hdu 1754 线段树模板题
  9. JSP EL表达式的运用
  10. JS中onload的各种使用
  11. Node.js新手教程——怎样实现文件上传功能
  12. Python3常用学习网站总结(随时更新)
  13. vuex Loding加载..
  14. android 与 服务器通信
  15. SQLite Where 子句(http://www.w3cschool.cc/sqlite/sqlite-where-clause.html)
  16. Python并发编程之线程中的信息隔离(五)
  17. Springboot项目配置druid数据库连接池,并监控统计功能
  18. vue的router-link传参问题
  19. week07 13.1 NewsPipeline之 一 NewsMonitor
  20. 《.NET之美》之程序集

热门文章

  1. excel数据比对,查找差异
  2. 前端打包--source-map=false作用
  3. NFS和DHCP服务
  4. 小而美的Promise库——promiz源码浅析
  5. python基础知识01-数据类型和序列类型
  6. Java 多线程执行
  7. 【HIHOCODER 1049】 后序遍历
  8. python之tkinter变量设置 2014-4-9
  9. 大数据学习——linux常用命令(二)四
  10. IDEA将Maven项目中src源代码下的xml配置文件编译进classes