- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我们在我们的项目中使用 require.js,我们需要覆盖第 705 行的 setTimeout,这是我们需要以某种方式忽略/省略的代码完全设置超时(我的意思是运行它),问题是如果我在更改版本时在开源代码中显式更改它,代码将会丢失,我应该如何从外部仅针对 require.js 文件覆盖此 setTimout 和只要我使用这个库就保留它,是否可以在全局 JS 中以优雅的方式做到这一点?
https://github.com/jrburke/requirejs/blob/master/require.js
这是第 705 行
//If still waiting on loads, and the waiting load is something
//other than a plugin resource, or there are still outstanding
//scripts, then just try back later.
if ((!expired || usingPathFallback) && stillLoading) {
//Something is still waiting to load. Wait for it, but only
//if a timeout is not already in effect.
if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) {
checkLoadedTimeoutId = setTimeout(function () {
checkLoadedTimeoutId = 0;
checkLoaded();
}, 50);
}
}
仅供引用,我们这样做的原因是 Chrome: timeouts/interval suspended in background tabs?
最佳答案
您已声明您的目标是解决 Chrome 在 setTimeout
上对后台标签页执行的限制。我不认为这样做是个好主意,但如果你必须这样做,那么你绝对应该给 RequireJS 打补丁,而不是在全局范围内乱用 setTimeout
。你说:
if I change it in the open source code explicit when I change version the code will be lost
只有当您没有使用明智的方法来执行更改时,才会出现这种情况。明智地做到这一点是可能的。例如,您可以使用 Gulp 获取安装在 node_modules
中的 require.js
文件(在使用 npm
安装 RequireJS 之后)并生成一个补丁build
中的文件。然后在您的应用程序中使用此修补文件。这是 gulpfile.js
:
var gulp = require("gulp");
// Bluebird is a good implementation of promises.
var Promise = require("bluebird");
// fs-extra produces a `fs` module with additional functions like
// `ensureDirAsync` which is used below.
var fs = require("fs-extra");
// Make it so that for each the function in fs that is asynchronous
// and takes a callback (e.g. `fs.readFile`), a new function that
// returns promise is created (e.g. `fs.readFileAsync`).
Promise.promisifyAll(fs);
var to_replace =
"if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) {\n\
checkLoadedTimeoutId = setTimeout(function () {\n\
checkLoadedTimeoutId = 0;\n\
checkLoaded();\n\
}, 50);";
var replace_with =
"if (isBrowser || isWebWorker) {\n\
checkLoaded();";
gulp.task("default", function () {
// Use `fs.ensureDirAsync` to make sure the build directory
// exists.
return fs.ensureDirAsync("build").then(function () {
return fs.readFileAsync("node_modules/requirejs/require.js")
.then(function (data) {
data = data.toString();
// We use the split/join idiom to a) check that we get
// the string to be replaced exactly once and b)
// replace it. First split...
var chunks = data.split(to_replace);
// Here we check that the results of splitting the
// chunk is what we expect.
if (chunks.length < 2) {
throw new Error("did not find the pattern");
}
else if (chunks.length > 2) {
throw new Error("found the pattern more than once");
}
// We found exactly one instance of the text to
// replace, go ahead. So join...
return fs.writeFileAsync("build/require.js",
chunks.join(replace_with));
});
});
});
在运行它之前,您需要先运行 npm install gulp fs-extra bluebird requirejs
。无论如何,您可以使用 Gulp,您可以使用 Grunt,或者您可以使用您想要执行构建的任何其他系统。重点是:
您有一个可重现的自动化方法来为 RequireJS 打补丁。如果您使用 npm
安装新版本的 RequireJS,当您重建软件时,补丁是自动应用,只要 RequireJS 的代码没有以阻止应用补丁的方式更改。如果更改阻止应用补丁,请参阅下一点,了解会发生什么。
此方法比在运行时覆盖 setTimeout
更健壮。假设 James Burke 决定在较新版本的 RequireJS 中重命名 checkLoaded
到 checkDone
并重命名关联的变量(以便 checkLoadedTimeoutId
变为 checkDoneTimeoutId
)。上面的 gulpfile 将在您再次运行时引发异常,因为它找不到要替换的文本。您必须更新要替换的文本和替换,以便补丁与新版本的 RequireJS 一起使用。这样做的好处是您会提前收到警告,告知您情况已发生变化,您需要查看补丁。 游戏后期您不会有惊喜,也许是在您已经向客户交付了新版本的软件之后。
在运行时覆盖 setTimeout
的方法将默默地无法完成它们的工作。他们将寻找包含 checkLoadedTimeoutId
的函数,该函数在新版本中将不再存在。所以他们只会让 RequireJS 按默认方式运行。失败将是一个微妙的失败。 (我已经使用建议的自定义版本的 setTimeout
运行 RequireJS,该项目在未优化时加载了 50 个以上的模块。我没有看到 RequireJS 使用普通 setTimeout
和 RequireJS 使用自定义 setTimeout
。)
此方法不会减慢每次使用 setTimeout
的速度。 setTimeout
被 RequireJS 以外的其他代码使用。无论您如何削减它,在 setTimeout
的自定义替换中添加代码,开始在传递给它的每个函数中寻找字符串,都将 所有 使用 setTimeout
较慢。
关于javascript - 在 require.js 中覆盖 setTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34587078/
使用 ABC 加载模块 ( require )在分发的一个模块中工作,而在分发的另一个模块中失败。 加载 ABC 的原因可能是什么?与 require在一个地方失败? require Name::AB
我定义了以下方法: void Write(string fileContent, string fileName, string container = StorageBlobContainers.P
现在,Require.js是我最喜欢的Javascript编程方式。它可以使代码化整为零,并易于管理。而Require.js Optimizer能帮助我们将一个较大的应用分散成多个较小的应用,并通过
尝试开始使用 apioto http://apiato.io/A.getting-started/installation/ 如果我尝试测试 http://api.apiato.dev/registe
浏览 MDN 文档以查看提议的伪类的状态时,我遇到了 :required(并且扩展为 :optional)。这两个都已经存在很长时间了,但我现在才刚刚了解到。 此选择器与使用属性选择器 [requir
我正在尝试实现此条件:如果存在特定属性,则需要另一个属性;但如果它不存在,则不需要另一个。 另外,在 JSON 模式中,我们可以在依赖项中使用 not 吗? 这是一个示例架构 var schema =
我正在使用react-hot-loader我对其示例代码感到非常困惑: import React from 'react' import ReactDOM from 'react-dom' impor
过去几天我一直在玩 requirejs。我试图理解定义和要求之间的区别。 Define 似乎允许模块分离并允许遵守依赖关系顺序。但它会下载开始时所需的所有文件。而 require 仅在您需要时加载您需
我的项目是使用 angular cli [版本 - 6.1.3] 创建的。 我安装了 npm 模块 - is-reachable并在我的代码中使用它作为 - const isReachable = r
(有人可能会相应地更改标题)当像这样调用 javascript 的 require 方法时到底发生了什么: var xyz = require('xy')(require('z')); 谢谢 最佳答案
我一直在使用编译为 Node 代码的 Typescript 开发应用程序。因此,我更喜欢使用 import 语句来 require。 我一直在尝试将 Lodash 与 Lodash-Deep 一起使用
我在 require 中有一个奇怪的行为,我不知道如何避免(或者也许我的基础知识错误?)。 考虑以下代码: define (require) -> potoo = require "potoo"
这两种加杏仁和不加杏仁有什么区别? require('模块');require(['模块']); 编辑 嵌套: define(function() { require('module'); } def
我愿意使用 require.js 优化器优化我的 javascript 应用程序,但我现在想知道是否也可以在一个文件中包含 require.config 路径和 javascript 模块。事实上,在
我想我需要在一个页面中支持多个 require 实例,但在实现它时遇到了两个问题。我正在开发一项服务,该服务向外部客户端页面提供可嵌入的交互式对象。最重要的设计标准是易于嵌入,尽可能少地假设客户端环境
required 和 ng-required(表单验证)之间有什么区别? 最佳答案 AngularJS 表单元素查找 required属性来执行验证功能。 ng-required允许您设置requir
我有以下多选框: 0" /> 在我的 Controller 中,我在初始化时执行此操作: $scope.form.Slides = []; 如果幻灯片数组中有幻灯片,我希望表单的此元素有效。这些是动
我在 ubuntu 上运行 VPS: Distributor ID: Ubuntu Description: Ubuntu 14.04.5 LTS Release: 14.04 C
我正在使用 ArcGIS API for Javascript 3.21。我在 require() 中有一个函数。我希望在单击按钮时调用该函数,但该按钮位于 require() 之外。
我的浏览器应用程序使用 require.js。该应用程序在屏幕上显示许多不同的小部件之一。 URL 片段包含小部件的路径(require.js 路径),然后调用 require 来动态加载它: var
我是一名优秀的程序员,十分优秀!