gpt4 book ai didi

node.js - RxJS if-else 类似管道过滤

转载 作者:搜寻专家 更新时间:2023-11-01 00:35:11 24 4
gpt4 key购买 nike

我不擅长 JS 和 RxJS,所以如果我的问题很愚蠢,抱歉。

我有这个代码;不是我写的:

  prepare(): Observable<any> {
const i = of({}).pipe(
// Check if file type is image
filter(() => isImage(this.file)),
switchMap(() => this.generateThumb()),
switchMap(() => this.resizeImage()),
take(1)
);

return i
}

但是我还需要第二件事;如果文件类型不是图像:

  1. 如果文件是图像,调整图像大小并生成缩略图。
  2. 如果文件不是图像,则只生成缩略图。

我怎样才能做到这一点?谢谢。

编辑:AJT_82 的评论

我真的不知道这里发生了什么。为什么要先创建一个可观察对象,对其进行管道传输、过滤等等。

所以我试着让它更简单:

if (isImage(this.file)) {
this.resizeImage();
}
this.generateThumb();

return of(this);

失败了。

最佳答案

我认为你可以使用 partition 运算符:

它应该看起来像:

const source = of({});
const [images, notImages] = source.pipe(partition(() => isImage(this.file));

merge(
images.pipe(
switchMap(() => this.resizeImage()),
)
notImages
)
.pipe(switchMap(() => this.generateThumb())),
.subscribe(...);

分区会。给你 observable of images,第二个 observable of noImages

在图像上应用 resizeImages,将图像与 noImages 合并并在合并的 observable 上应用 generateThumb

link to docs

关于node.js - RxJS if-else 类似管道过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54580624/

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