简体   繁体   English

如何在后台预加载数据(Angular Web App)

[英]How to pre-load data in the background (Angular Web App)

Our (Angular) app uses ~15 APIs to get data from the backend.我们的(Angular)应用程序使用约 15 个 API 从后端获取数据。 Each API is used to get data for a specific page.每个 API 用于获取特定页面的数据。 The APIs are not very fast unfortunately, some of them take 5-10 seconds to return.不幸的是,API 不是很快,其中一些需要 5-10 秒才能返回。 The amount of data that we get is about 1 MB for all pages.对于所有页面,我们获得的数据量约为 1 MB。 Some pages might not be used every day but several of them are guaranteed to be used all the time.有些页面可能不会每天都使用,但其中一些页面保证会一直使用。

Considering all of these, I am thinking to preload (almost) all the data in the background when the app starts, so when a page is loaded we won't wait for 5-10 seconds for the data.考虑到所有这些,我正在考虑在应用程序启动时在后台预加载(几乎)所有数据,这样当加载页面时我们不会等待 5-10 秒的数据。

The way I am thinking to do that is: when the app starts, wait for 10 seconds or so and then call all the APIs and save the data in the store (NGRX).我想这样做的方式是:当应用程序启动时,等待 10 秒左右,然后调用所有 API 并将数据保存在商店 (NGRX) 中。 However I have a few questions:但是我有几个问题:

  1. When the app starts, it displays Page-1 which starts getting Data-1 using API-1.当应用程序启动时,它会显示 Page-1,它开始使用 API-1 获取 Data-1。 Sometimes this request takes more than 10 seconds, and the Get-Data-In-Background kicks in and it calls API-1.有时此请求需要超过 10 秒,Get-Data-In-Background 启动并调用 API-1。 The previous call for API-1 is canceled (by the browser) and now we wait 10 more seconds for Data-1 to be retrieved.先前对 API-1 的调用被取消(由浏览器),现在我们再等待 10 秒以检索 Data-1。 Is there a way to specify "do not cancel a previous request if there is one"?有没有办法指定“如果有一个请求,不要取消以前的请求”?
  2. Currently we get approximative 1MB of data.目前我们得到大约 1MB 的数据。 If, I don't know for what reason, in the future the app will get a lot more data (1GB) from the backend, is the browser smart enough to handle big data by saving/loading it from the disk?如果,我不知道出于什么原因,将来应用程序将从后端获取更多数据(1GB),浏览器是否足够智能以通过从磁盘保存/加载来处理大数据? Or we will get into an out-of-memory situation?或者我们会陷入内存不足的情况?
  3. Is that the right way to pre-load data?这是预加载数据的正确方法吗?

I ended up implementing a 'can-load-in-the-background-trigger' (Subject) that is triggered by each page after it received the data.我最终实现了一个“can-load-in-the-background-trigger”(主题),它在每个页面收到数据后触发。 Only when this trigger is true I start loading the rest of the data in the background.只有当这个触发器为真时,我才开始在后台加载数据的 rest。

This way I mitigated issue-#1, but I would still like to get some answers if possible.这样我就缓解了问题#1,但如果可能的话,我仍然想得到一些答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM