如何以优雅的方式组合辅助图和排气板?

发布于 2025-02-07 04:42:38 字数 2023 浏览 2 评论 0原文

我有一个带有recaptcha的CreateReviewform,我有3个流:

  • NGRX Action(addReview(第一个可观察到)。每次触发此操作时,我都必须调用this.recaptchav3service.execute()方法,该方法返回使用Token(string)(2nd observable)返回可观察到的方法。然后使用此令牌我必须调用API方法来创建评论this.reviewapiservice.create({...评论,recaptchavalue:token})),它可以用创建的评论返回可观察的评论(3trd observable可观察到)

首先,我使用combineLatest尝试过它,但它并不像我预期的那样工作。仅当我第一次单击“添加评论” BTN时,才能获得令牌发射值的方法。下次,它都没有发出任何值(使用使用相同的方式。我也尝试

createReview$ = combineLatest([
    this.actions$.pipe(
        ofType(ReviewsDialogActions.addReview)
    ),
    this.recaptchaV3Service.execute('AddREview')
]).pipe(exhaustMap(([{ review }, token]) => 
    this.reviewApiService.create({ ...review, recaptchaValue: token })))
        .pipe(
            map(createdReview => ReviewsDialogApiActions.createReviewSuccess({ createdReview, companyId: createdReview.company.id })),
            catchError(error => {
                debugger;
                return observableOf(ReviewsDialogApiActions.createReviewError({ error: error.error.message.join(', ') }))
            })

过 /code>它可以正常工作,但我不喜欢它的外观,我认为它看起来像这样

createReview$ = createEffect((): any => {
let reviewFormData = {};
return this.actions$.pipe(
    ofType(ReviewsDialogActions.addReview),
    concatMap(({ review }) => {
        reviewFormData = review;
        return this.recaptchaV3Service.execute('AddREview');
    }),
    exhaustMap((token) => this.reviewApiService.create({ ...<Review>reviewFormData, recaptchaValue: token })
        .pipe(map(createdReview => ReviewsDialogApiActions.createReviewSuccess({ createdReview, companyId: createdReview.company.id })),
        catchError(error => {
            return observableOf(ReviewsDialogApiActions.createReviewError({ error: error.error.message.join(', ') }))
        })))
)

}

请帮助这是RXJS运营商的最佳组合吗?

I have a CreateReviewForm with reCaptcha and I have 3 streams for it:

  • ngRx action (addReview) (1st observable). Everytime when this action is triggered I have to call this.recaptchaV3Service.execute() method which returns observable with token (string) (2nd observable). And then using this token I have to call API method for creating review this.reviewApiService.create({ ...review, recaptchaValue: token }) which returns observable with created review (3rd observable)

First I tried it with combineLatest and it didn't work like I expected. Method for getting token emited value only when I clicked "Add Review" btn first time. All next times it didn't emit any value (withLatestFrom worked in the same way. I tried it also)

createReview$ = combineLatest([
    this.actions$.pipe(
        ofType(ReviewsDialogActions.addReview)
    ),
    this.recaptchaV3Service.execute('AddREview')
]).pipe(exhaustMap(([{ review }, token]) => 
    this.reviewApiService.create({ ...review, recaptchaValue: token })))
        .pipe(
            map(createdReview => ReviewsDialogApiActions.createReviewSuccess({ createdReview, companyId: createdReview.company.id })),
            catchError(error => {
                debugger;
                return observableOf(ReviewsDialogApiActions.createReviewError({ error: error.error.message.join(', ') }))
            })

)

I combined it all with concatMap and exhaustMap and it works but I don't like how it looks and I don't think it should look like this

createReview$ = createEffect((): any => {
let reviewFormData = {};
return this.actions$.pipe(
    ofType(ReviewsDialogActions.addReview),
    concatMap(({ review }) => {
        reviewFormData = review;
        return this.recaptchaV3Service.execute('AddREview');
    }),
    exhaustMap((token) => this.reviewApiService.create({ ...<Review>reviewFormData, recaptchaValue: token })
        .pipe(map(createdReview => ReviewsDialogApiActions.createReviewSuccess({ createdReview, companyId: createdReview.company.id })),
        catchError(error => {
            return observableOf(ReviewsDialogApiActions.createReviewError({ error: error.error.message.join(', ') }))
        })))
)

}

Please help which is the best combination here for rxJs operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2025-02-14 04:42:38

combineLatestwith 这样:

createReview$ = createEffect(() => this.actions$.pipe(
    ofType(ReviewsDialogActions.addReview),
    combineLatestWith(this.recaptchaV3Service.execute('AddREview')),
    exhaustMap(([reviewFormData, token]) => this.reviewApiService.create(..)
        ...
    )
)

您可以使用可管道的操作员

You could use the pipeable operator combineLatestWith like this:

createReview$ = createEffect(() => this.actions$.pipe(
    ofType(ReviewsDialogActions.addReview),
    combineLatestWith(this.recaptchaV3Service.execute('AddREview')),
    exhaustMap(([reviewFormData, token]) => this.reviewApiService.create(..)
        ...
    )
)

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文