在 RxJS 中,有没有办法将一个数组的 Observable 拆分为一个 Observable 数组,每个元素一个?
syahiruddin
Asked:
2024-03-22 22:13:06 +0800 CST
我有一个可观察的丰富_sns$,我通过使用 concatMap 对每个发出的项目执行操作来丰富它,然后使用 toArray() 将结果组合到数组中。但是,我很好奇是否有其他方法可以在不使用 toArray() 的情况下实现相同的结果。
这是我当前的代码:
private enriched_sns$ = toObservable(this.sn_results).pipe(
concatMap((results) => results as StreamNotificationResult[]),
concatMap((notification) => {
const user_id = notification.activities[0].actor.id;
return zip(of(notification), this.read_user_data_service.readUserData(user_id));
}),
map(([notification, user]) => {
return {
notification,
user: user,
};
}),
toArray(),
);
有没有一种方法可以在可观察对象的末尾获取发出的项目数组,而无需诉诸 toArray() ?任何帮助或替代方法将不胜感激。谢谢你!
AbbasFaisal
Asked:
2024-01-10 00:43:10 +0800 CST
我有一个在浏览器中运行的纯 JavaScript 应用程序。它有一些主题,有一些订阅者。我是否需要确保在浏览器关闭之前完成所有订阅?或者关闭或移动到另一个页面会进行清理并确保没有内存泄漏吗?我似乎找不到“是”或“否”的答案,也找不到与 Angular 无关的答案。
LeO
Asked:
2023-09-19 21:21:48 +0800 CST
根据文档,它exhaustMap
使用 ResultSelector,这将在 V8 中删除。我尝试遵循该示例,但无法找到如何重构我的代码的线索。
interval(5000)
.pipe(
takeWhile(() => !this.isDialogDestroyed),
exhaustMap(() => this.versService.checkStatus(myParameters))
)
.subscribe({
next: (status) => {
if (status === 'DONE') {
this.isDialogDestroyed = true;
// ... further processing - removed for simplicity
}
},
error: () => {
// Handle errors here
},
});
如何调整代码以适应 V8?
parliament
Asked:
2023-08-24 20:18:20 +0800 CST
我想在我的应用程序中发出请求时显示加载程序。但有时我的服务器响应太快,加载器只是闪烁一瞬间,导致不愉快的体验。
过去我一直做的是使用combineLatest
atimer
来创建最小的请求延迟。
const request = getRequestPromise();
await lastValueFrom(combineLatest([request, timer(1000)]).pipe(mapRx(([x]) => x)))
其作用是,如果服务器在不到一秒内响应,则只需在解析之前等待 1 秒即可。然而,如果服务器在 3 秒内响应,则此代码不会增加额外的一秒。
这一直很有效。但我刚刚意识到,如果请求失败(导致异常),它不会遵守最小延迟。它会立即解决并导致令人不快的加载器闪烁。
我如何才能同时处理失败和成功的请求?
我仍然需要能够处理请求本身的成功/错误:
await lastValueFrom(combineLatest([request, timer(1000)])
.pipe(mapRx(([x]) => x)))
.then(() => console.log('handle successful request'))
.catch(() => console.log('handle error'))