我们在项目中使用 require.js,我们需要重写设置超时时间在第 705 行,这是我们需要的代码以某种方式忽略/省略这个 setTimeout 根本(我的意思是运行它),问题是,如果我在更改版本时显式地在开源代码中更改它,代码将丢失,我应该如何仅针对 require.js 文件从外部重写此 setTimout只要我使用这个库就保留它,是否可以在elegantJS 全局的方式?
https://github.com/jrburke/requirejs/blob/master/require.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:后台选项卡中暂停了超时/间隔? https://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs
您已经说过您的目标是解决 Chrome 执行的限制setTimeout
对于后台的选项卡。我认为这样做不是一个好主意,但如果你必须这样做,那么你绝对应该修补 RequireJS 而不是搞乱setTimeout
全球。你说:
如果我在更改版本时显式地在开源代码中更改它,则代码将丢失
仅当您不使用合理的方法来执行更改时才会出现这种情况。明智地去做是可能的。例如,您可以使用 Gulp 来获取require.js
文件安装在node_modules
(在安装 RequireJS 之后npm
)并生成一个修补文件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。如果您安装新版本的 RequireJSnpm
,当您重建软件时,只要 RequireJS 代码的更改不会妨碍应用补丁,补丁就会自动应用。请参阅下一点,了解如果更改阻止应用补丁会发生什么情况。
-
该方法比重写更稳健setTimeout
在运行时。假设 James Burke 决定在较新版本的 RequireJS 中重命名checkLoaded
to checkDone
并重命名关联的变量(以便checkLoadedTimeoutId
变成checkDoneTimeoutId
)。当您再次运行上面的 gulpfile 时,它会引发异常,因为它找不到要替换的文本。您必须更新要替换的文本和替换内容,以便补丁可以与新版本的 RequireJS 配合使用。这样做的好处是您可以提前收到警告,告知情况已发生变化,您需要查看补丁。在游戏后期你不会有惊喜,也许在您已经向客户交付了新版本的软件之后。
重写的方法setTimeout
在运行时只会默默地无法完成他们的工作。他们将寻找一个包含以下内容的函数checkLoadedTimeoutId
,在新版本中将不再存在。所以他们只会让 RequireJS 按照默认的方式运行。失败将是一种微妙的失败。 (我已经使用建议的自定义版本运行了 RequireJSsetTimeout
一个项目在未优化时加载超过 50 个模块。我发现 RequireJS 使用 stock 之间没有明显的区别setTimeout
和 RequireJS 使用自定义setTimeout
.)
-
此方法不会减慢每次使用的速度setTimeout
. setTimeout
被 RequireJS 以外的其他代码使用。不管你如何削减它,在自定义替换中添加代码setTimeout
开始在传递给它的每个函数中查找字符串将使all的用途setTimeout
slower.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)