rxjs-1介绍
2021-04-26 11:28
标签:sch ams think rate 创建 current min gui guid RxJS is a library for composing asynchronous and event-based programs by using observable sequences. It provides one core type, the Observable, satellite types (Observer, Schedulers, Subjects) and operators inspired by Array#extras (map, filter, reduce, every, etc) to allow handling asynchronous events as collections. rxjs是一组异步和事件基础的库通过使用可观察的序列,它提供一个核心类型 Observable, 伴随类型(Observer观察者, Schedulers调度, Subjects主题)和操作符,其灵感来源于 Array#extras(map, filter, reduce, every, etc)。操作符准许操作异步事件的集合 Think of RxJS as Lodash for events. rxjs操作事件类似于Lodash操作js ReactiveX combines the Observer pattern with the Iterator pattern and functional programming with collectionsto fill the need for an ideal way of managing sequences of events. ReactiveX 合并 观察者模式、迭代器模式和函数式编程集合,满足管理事件序列的理想方式 The essential concepts in RxJS which solve async event management are: Rxjs解决异步事件管理的必要概念如下: Using RxJS you create an observable instead. 纯净 What makes RxJS powerful is its ability to produce values using pure functions. That means your code is less prone to errors. Rxjs强大的是它能力其生产的值使用纯函数,这意味着你的代码较少遇到错误 Normally you would create an impure function, where other pieces of your code can mess up your state. 正常时你会创建不纯的函数,其他地方你的代码可能会弄乱你的状态 Using RxJS you isolate the state. 流 RxJS has a whole range of operators that helps you control how the events flow through your observables. Rxjs有一系统操作符帮助你控制怎么让事件流穿过你的可能的对象 This is how you would allow at most one click per second, with plain JavaScript: 这个你怎么控制最多一次点击每秒,用普通JavaScript With RxJS: Other flow control operators are filter, delay, debounceTime, take, takeUntil, distinct, distinctUntilChanged etc. 其他流控制操作符 值 You can transform the values passed through your observables. 你会转换值当穿过你的可观察的对象 Here‘s how you can add the current mouse x position for every click, in plain JavaScript: 这是你怎么增加当前鼠标x坐标为每次点击,javascript With RxJS: Other value producing operators are pluck, pairwise, sample etc. 其他值生产操作符 rxjs-1介绍 标签:sch ams think rate 创建 current min gui guid 原文地址:https://www.cnblogs.com/tiandidao/p/12221866.html
map
, filter
, concat
, reduce
, etc.map
, filter
, concat
, reduce
setTimeout
or requestAnimationFrame
or others.setTimeout
或者 requestAnimationFrame
或其他
First examples
Normally you register event listeners.
document.addEventListener(‘click‘, () => console.log(‘Clicked!‘));
import { fromEvent } from ‘rxjs‘;
fromEvent(document, ‘click‘).subscribe(() => console.log(‘Clicked!‘));
Purity
let count = 0;
document.addEventListener(‘click‘, () => console.log(`Clicked ${++count} times`));
import { fromEvent } from ‘rxjs‘;
import { scan } from ‘rxjs/operators‘;
fromEvent(document, ‘click‘)
.pipe(scan(count => count + 1, 0))
.subscribe(count => console.log(`Clicked ${count} times`));
Flow
let count = 0;
let rate = 1000;
let lastClick = Date.now() - rate;
document.addEventListener(‘click‘, () => {
if (Date.now() - lastClick >= rate) {
console.log(`Clicked ${++count} times`);
lastClick = Date.now();
}
});
import { fromEvent } from ‘rxjs‘;
import { throttleTime, scan } from ‘rxjs/operators‘;
fromEvent(document, ‘click‘)
.pipe(
throttleTime(1000),
scan(count => count + 1, 0)
)
.subscribe(count => console.log(`Clicked ${count} times`));
Values
let count = 0;
const rate = 1000;
let lastClick = Date.now() - rate;
document.addEventListener(‘click‘, event => {
if (Date.now() - lastClick >= rate) {
count += event.clientX;
console.log(count);
lastClick = Date.now();
}
});
import { fromEvent } from ‘rxjs‘;
import { throttleTime, map, scan } from ‘rxjs/operators‘;
fromEvent(document, ‘click‘)
.pipe(
throttleTime(1000),
map(event => event.clientX),
scan((count, clientX) => count + clientX, 0)
)
.subscribe(count => console.log(count));
上一篇:使用 Hexo 创建项目文档网站