确保脚本可用性的模式或技术?

发布于 2024-10-22 21:32:44 字数 1858 浏览 1 评论 0原文

随着我一直从事的项目不断发展,当其他代码尝试访问页面上的所有脚本时,页面上的所有脚本都不可用的情况也越来越频繁。尽管这种情况最常发生在代码更新后(例如未缓存),但我在测试中越来越多地出现这种情况,而以前从未发生过这种情况。

我已经通过使用函数等待模块变得可用来部分解决了这个问题(请参阅这个问题),这主要解决了人们的担忧,但我对实施并不完全感到兴奋,并且正在寻找一种更具工业实力的模式来解决这个问题。这些是我提出的可能的解决方案:

1) 按需加载脚本,例如 确保 - 不理想。为此,需要在每个脚本中包含实际的脚本名称依赖信息,而不仅仅是模块/对象名称。在使用资源之前仍然需要采取一些操作以确保其可用。

2) 管理脚本加载顺序。如果这甚至可以工作(例如,我认为简单地将脚本 A 放在脚本 B 之前并不能保证它可用,因为它们可以同时加载),这将是一个痛苦,因为你不知道依赖关系,直到你'已经加载了依赖它的东西。在一个拥有许多使用不同资源的页面的网站上需要进行大量的工作(并且我无意在每个页面上加载该网站上到处使用的所有内容)。

3) 在允许用户交互之前,等待所有内容加载到给定页面上。由于显而易见的原因,远非理想。仍然没有解决初始化代码中发生的依赖关系。

4)扩展我当前的解决方案。目前的工作原理如下(这是伪代码,但基本逻辑过程):

// Depends on Module2
Module1 = (function () {
   self = {};
   // function requires dependency
   // waitFor waits until global named 'dependency' is available then calls callback
   self.initialized=false; 
   self.init = function() {
       waitFor('Module2', function() {
           self.intialized=true;
       });
   }
  // waitForInitialization sets a callback when self.initialized=true
   self.func = self.waitForInitialization(func() {
           Module2.doStuff();
       });
   }
   //UI-initiated function requires dependency
   self.uiFunc = function() {
       if (!self.initialized) {
          showPleaseWaitDialog();
          self.waitForInitialization(function() {
              dismissPleaseWaitDialog();
              self.uiFuncImpl);
       } else {
          self.uiFuncImpl();
       }
   }
   self.uiFuncImpl= function() { 
       Module2.doStuff();
   }
} ());

我可以想出方法来创建一个原型,该原型将比我的解决方案更透明地处理依赖问题上面的代码,并且完全打算在必要时这样做,但这真的是最好的解决方案吗?其他人做什么?什么被认为是最佳实践?

As a project I have been working on has grown, so has the frequency of situations where all scripts on a page are not available when other code tries to access them. Though this happens most often after code is updated (e.g. not cached) I've had it come up more and more in testing, when it never used to happen.

I've addressed this, partially, by using a function to wait for a module to become available (see this question) and this addresses the concern, mostly, but I'm not totally thrilled with the implementation, and am looking for a more industrial strength pattern to deal with this. These are possible solutions I've come up with:

1) Load scripts on demand with something like ensure - not ideal. Requires actual script name dependency information to be included in each script, not just module/object name, to do this. Still have to take some action before using a resource to ensure it's available.

2) Manage script loading order. If this would even work (e.g. I don't think that simply putting script A before script B guarantees it will be available since they can be loaded concurrently), it would be a pain, since you don't know a dependency until you've loaded the thing that depends on it. Would require a lot of work to set up on a site that has lots of pages that use different resources (and I have no intention of loading everything used everywhere on the site on every page).

3) Wait for everything to be loaded on a given page before allowing user interaction. Far from ideal for obvious reasons. Still doesn't address dependencies that happen in initialization code.

4) Expand upon my current solution. Currently works like (this is pseudocode, but the basic logic process):

// Depends on Module2
Module1 = (function () {
   self = {};
   // function requires dependency
   // waitFor waits until global named 'dependency' is available then calls callback
   self.initialized=false; 
   self.init = function() {
       waitFor('Module2', function() {
           self.intialized=true;
       });
   }
  // waitForInitialization sets a callback when self.initialized=true
   self.func = self.waitForInitialization(func() {
           Module2.doStuff();
       });
   }
   //UI-initiated function requires dependency
   self.uiFunc = function() {
       if (!self.initialized) {
          showPleaseWaitDialog();
          self.waitForInitialization(function() {
              dismissPleaseWaitDialog();
              self.uiFuncImpl);
       } else {
          self.uiFuncImpl();
       }
   }
   self.uiFuncImpl= function() { 
       Module2.doStuff();
   }
} ());

I can think of ways to create a prototype that would deal with the dependency issue more transparently than my code above, and fully intend to do that if I have to, but is this truly the best solution? What do others do? What are considered best practices?

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

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

发布评论

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

评论(1

静待花开 2024-10-29 21:32:44

2) 脚本加载顺序 - 脚本将始终按照它们在 DOM 中放置的顺序执行,因此虽然它们可能同时加载,但它们将以有序的方式执行(在我从事的一个大型项目中也遇到了同样的问题)。

?)如果脚本加载顺序不是您的理想解决方案,您可以查看 Promise 模型。

??) 如果 Promises 和 Load Order 对您不起作用,您可以侦听每个模块在初始化时可以触发的命名空间事件,这样,如果对象存在,则可以使用它,如果不存在,则可以侦听其初始化。

2) Script Load Order - scripts will always be executed in the order they are placed in the DOM, so while they might load concurrently they will execute in an orderly fashion (faced this same problem on a large project I worked on).

?) If script load order is not an ideal solution for you, you could look into the Promise model.

??) If Promises and Load Order won't work for you, you could listen for a namespaced event that each module could fire when it's initialized, that way if the object exists it can be used and if not its initialization could be listened for.

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