Rxjs 使用链早期阶段的先前可观察值

发布于 2025-01-12 21:49:09 字数 3989 浏览 5 评论 0原文

如何使用链早期阶段(不是最后阶段)的可观察值。

我有这样的代码:

deleteGroceryStoreAndItsOwner() {

    this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
        .flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries.filter(groceryStoreOwnerSummary=> groceryStoreOwnerSummary.user.gender == "Male"))
        .flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid))
        .flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
        .flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
        .flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)) // here is where i have the problem: how can i use the observable from early stage of the chain?
        .flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
        .subscribe(() => {
            console.log("deleted grocery store")
            console.log("deleted grocery store owner")
        })
};

在删除第四个平面图中的杂货店后,我需要删除杂货店老板,这是来自第二个平面图的。

注意:

  1. 每个删除方法(deleteGroceryStore和deleteGroceryStoreOnwer)都会返回一个Observable。
  2. 我的应用程序的角度版本不支持管道,因此请不要使用 .pipe 给出答案。

更新:2022 年 3 月 10 日 我像这样修改了我的代码,但由于某种原因,地图中的代码没有被执行,知道为什么吗?

deleteGroceryStoreAndItsOwner() {
    this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
        .flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
            .filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male"))
        .map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
            this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
                .flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
                .flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
            this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
                .flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
        })
        .subscribe(() => {
            console.log("deleted grocery store")
            console.log("deleted grocery store owner")
        })
};

我也尝试过这个,但仍然不起作用:

deleteGroceryStoreAndItsOwner() {
    this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
        .flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
            .filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male")
            .map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
                this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
                    .flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
                    .flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
                this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
                    .flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
            }))
        .subscribe(() => {
            console.log("deleted grocery store")
            console.log("deleted grocery store owner")
        })
};

How do I use an observable from early stage of the chain (not the last one).

I have code like this:

deleteGroceryStoreAndItsOwner() {

    this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
        .flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries.filter(groceryStoreOwnerSummary=> groceryStoreOwnerSummary.user.gender == "Male"))
        .flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid))
        .flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
        .flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
        .flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)) // here is where i have the problem: how can i use the observable from early stage of the chain?
        .flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
        .subscribe(() => {
            console.log("deleted grocery store")
            console.log("deleted grocery store owner")
        })
};

after I deleted the grocery store in the 4th flat map, I need to delete the grocery store owner, and this is from the second flatmap.

notes:

  1. each delete method (deleteGroceryStore and deleteGroceryStoreOnwer) returns an Observable.
  2. My app's angular version does not support pipe, so please don't give answers with .pipe.

Update: March 10 2022
I modified my code like this but for some reason the code in the map is not getting executed, any idea why?

deleteGroceryStoreAndItsOwner() {
    this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
        .flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
            .filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male"))
        .map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
            this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
                .flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
                .flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
            this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
                .flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
        })
        .subscribe(() => {
            console.log("deleted grocery store")
            console.log("deleted grocery store owner")
        })
};

I also tried this but that still did not work:

deleteGroceryStoreAndItsOwner() {
    this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
        .flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
            .filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male")
            .map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
                this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
                    .flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
                    .flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
                this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
                    .flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
            }))
        .subscribe(() => {
            console.log("deleted grocery store")
            console.log("deleted grocery store owner")
        })
};

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

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

发布评论

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

评论(1

最后的乘客 2025-01-19 21:49:09

您可以通过嵌套运算符而不是链接它们来实现此目的。

这是一个不起作用的简化示例:

const teamSummary = userId
  .flatMap(userId  => getUser(userId))
  .flatMap(user    => getManager(user.managerId))
  .flatMap(manager => getDirectReports(manager.id))
  .map(reports     => buildSummary(user, manager, reports)) 
;                  // cannot find name 'user' / 'manager'

当我们嵌套运算符时,所有参数都可以在 map 内访问:

const teamSummary = userId
  .flatMap(userId => getUser(userId)
    .flatMap(user => getManager(user.managerId)
      .flatMap(manager => getDirectReports(manager.id)
        .map(reports => buildSummary(user, manager, reports))
      )
    )
  );

FWIW,这里有一些 StackBlitz

You can accomplish this by nesting the operators instead of chaining them.

Here's a simplified example that WON'T work:

const teamSummary = userId
  .flatMap(userId  => getUser(userId))
  .flatMap(user    => getManager(user.managerId))
  .flatMap(manager => getDirectReports(manager.id))
  .map(reports     => buildSummary(user, manager, reports)) 
;                  // cannot find name 'user' / 'manager'

When we nest the operators, all the parameters are accessible inside the map:

const teamSummary = userId
  .flatMap(userId => getUser(userId)
    .flatMap(user => getManager(user.managerId)
      .flatMap(manager => getDirectReports(manager.id)
        .map(reports => buildSummary(user, manager, reports))
      )
    )
  );

FWIW, here's a little StackBlitz

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