switchMap相关文章
rxjs里switchMap operators的用法
通过rxjs的一个例子, 来学习SwitchMap的使用方法
rxjs switchMap的实现原理
rxjs的map和switchMap在SAP Spartacus中的应用 -将高阶Observable进行flatten操作
源代码:
import { Observable, of, OperatorFunction } from "rxjs";
import { map } from "rxjs/operators";
import { interval } from "rxjs";
import { switchMap } from "rxjs/operators";
/* Projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recently projected Observable.
switchMap将source value映射成一个新的Observable,这个Observable被output Observable自动merge.
只会从最新的被projected过后的Observable里emit数据
给consumer
*/
const add100 = (counter: number) => {
return of(counter + 100);
};
const addFunctionOperator: OperatorFunction = switchMap(add100);
const pollTasks = () => {
return interval(1000).pipe(addFunctionOperator);
// caller can do subscription and store it as a handle:
let tasksSubscription = pollTasks().subscribe(data =>
console.log("timestamp: " + new Date() + ": " + data)
);
// turn it off at a later time
setTimeout(() => tasksSubscription.unsubscribe(), 3000);
