缓存发射值可观察到的依赖性但不可观察的源

发布于 2025-01-31 15:49:09 字数 371 浏览 3 评论 0原文

我目前正在实施一项新功能。该应用程序希望给卖方一个管理员可以看到各种东西。他们在仪表板上可以看到的一件事的一个例子是他们的最后10个订单。

该订单的API仅返回各种ID的数组(客户ID,产品ID,卖方ID等),以填充订单页面,我必须对每个订单进行3个不同的API调用,以使数据获取数据以呈现订单 现在,我必须创建一个与订单列表页面不同的仪表

板,我不想再次进行如此乏味的API调用。我想创建一个仪表板$可观察的,该仪表列表具有订购列表$的最后一个发射值,但我不希望订阅订单列表$的任何内容。 因此,当调用订单列表$时,它从服务器获得了最后的订单列表数据(我感觉不像订单列表一样敏感),当调用仪表板$时,获得订购列表$的最后发射值,如果订购列表$尚未发出任何值,然后仪表板$可以提出检索订单列表的请求。

I am currently implementing a new feature at work. The app wants to give sellers an admin where they can see various things. An example of one of the things they can see on their dashboard is their last 10 orders.

The order's API only returns an array of various ids (customer id, product id, seller id, etc) In order to populate the orders page, I have to make 3 different API call on each order to get the data to render on the order list page

Now that I have to create a dashboard that is different from the order list page, I do not want to make such a tedious API call again. I want to create a dashboard$ observable that has the last emitted value of orderList$ but I do not want anything subscribed to orderList$ to be cached.
So when orderList$ is called, it gets the lastest orderList data from the server (I do not feel something as sensitive as order list should be cached), when dashboard$ is called, get the last emitted value of orderList$ and if orderList$ has not emitted any values then dashboard$ can make a request to retrieve order list.

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

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

发布评论

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

评论(1

他不在意 2025-02-07 15:49:09

在具有反应性状态时,我喜欢根据事件定义应该发生的事情。我的意思是,我将拥有一个主题,其中可以将值推入通知事件,从那里我们可以对这些事件做出反应。

就您而言,这是您想要的想法:

const navigationToOrdersPage$ = new Subject<void>();
const refreshOrdersButtonClicked$ = new Subject<void>();

const orders$: Order[] = merge(
  navigationToOrdersPage$,
  refreshOrdersButtonClicked$
).pipe(
  switchMap(() => yourOrderService.getOrders()),
  shareReplay({
    bufferSize: 1,
    // make sure that even if we don't have anyone subscribed to that observable
    // we keep the result in the cache and if we ever go to the orders page or click
    // on the refresh button it'll be updated anyway but with this it's safe to navigate
    // to another page than orders and the dashboard and if you go back to the dashboard
    // you'd still get an instant result
    refCount: false,
  })
);

因此,在管理员和仪表板页面上都可以重复使用此观察到的安全性。当然,在适当的情况下,您需要在两个主题上调用下一个,以便在需要时可以刷新订单。

When working with a reactive state of mind, I like to define what should happen based on events. By this I mean that I'll have a Subject in which in can push values into to notify an event and from there we can react to these events.

In your case, here's an idea for what you want:

const navigationToOrdersPage$ = new Subject<void>();
const refreshOrdersButtonClicked$ = new Subject<void>();

const orders$: Order[] = merge(
  navigationToOrdersPage$,
  refreshOrdersButtonClicked$
).pipe(
  switchMap(() => yourOrderService.getOrders()),
  shareReplay({
    bufferSize: 1,
    // make sure that even if we don't have anyone subscribed to that observable
    // we keep the result in the cache and if we ever go to the orders page or click
    // on the refresh button it'll be updated anyway but with this it's safe to navigate
    // to another page than orders and the dashboard and if you go back to the dashboard
    // you'd still get an instant result
    refCount: false,
  })
);

So here it'd be safe to reuse this observable on both the admin and the dashboard page. Of course, you'll need to call next on the 2 subjects when appropriate so that the orders can be refreshed when they need to.

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