Rxjs
事件流
js
import { range } from "rxjs";
import { map, filter } from "rxjs/operators";
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
js
import { Subject, from } from "rxjs";
import { debounceTime, distinctUntilChanged, switchMap } from "rxjs/operators";
const searchItems = new Subject();
searchItems
.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap((val) => from(getSuggestList(val)))
)
.subscribe((x) => console.log(x));
const input = document.getElementById("input");
input.oninput = search;
function search(val) {
searchItems.next(val.target.value);
}
function getSuggestList(val) {
return new Promise((resolve) => {
console.log(val);
setTimeout(() => {
resolve([
{ id: 1, name: "zhangsan" },
{ id: 2, name: "lisi" },
]);
});
});
}