gpt4 book ai didi

javascript - RXJS 缺少throttleWhile 运算符?

转载 作者:行者123 更新时间:2023-12-01 00:36:53 25 4
gpt4 key购买 nike

令人惊讶的是,Rxjs 中似乎没有 throttleWhile 运算符。

我的用例很简单:

HTTP 事件,在文件上传过程中发出。

如果事件类型为 HttpEventType.UploadProgress,我想限制它们;如果事件类型为 HttpEventType.Response,则不限制它们(以捕获最终值,即响应)

我的服务电话:

this.httpService
.uploadDocument(file)
.pipe(
throttleTime(200) // <-- would luv throttleWhile here
)
.subscribe((ev: HttpEvent<any>) => {
if (ev.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * ev.loaded / ev.total);
console.log(percentDone);
this.progress = percentDone;
} else if (ev.type === HttpEventType.Response) {
console.log(ev);
}
})

有什么想法吗?

最佳答案

我建议您采用下一个解决方案:

import {throttleTime, partition, take}  from 'rxjs/operators';
import {timer} from 'rxjs';

let a$ = timer(0, 120).pipe(take(10));
let hasRequiredType = v => v === 9;
let [done$, load$] = partition(hasRequiredType)(a$);
load$.pipe(throttleTime(300)).subscribe(v => console.log("loading", v));
done$.subscribe(v => console.log("done", v));

https://stackblitz.com/edit/typescript-hzu3sp

关于javascript - RXJS 缺少throttleWhile 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com