所以我正在编写一个使用RequireJS和Socket.io的应用程序来检查socket.io资源是否可用,然后在连接时引导应用程序。如果socket.io暂时出现故障,我希望对资源进行几次requireJS轮询,直到它可用,然后继续初始化应用程序。
不幸的是(或者幸运的是?)似乎在require中存在某种缓存机制,它会为不加载的脚本注册脚本错误,这样如果你在错误回调中执行setTimeout来重新启动socketio require函数,即使资源可用,require也会继续抛出错误。
这是疏忽还是有理由保持缓存此错误?更重要的是,是否有允许重试的解决方法?
这是我一直在尝试的一个例子:
function initialize() {
require(['socketio', function(io) {
io.connect('http://localhost');
app._bootstrap();
}, function(err) {
console.log(err);
setTimeout(initialize, 10000);
});
}
答案 0 :(得分:6)
我知道这是一个老问题,但这对我来说很有趣,所以我调查了它......
您需要调用require.undef method以告知RequireJS不要缓存负载的先前故障状态。另请参阅errbacks示例。
然后您可以使用null回调再次调用require。原始回调仍然会被调用 - 不需要递归。像这样:
function requireWithRetry(libname, cb, retryInterval, retryLimit) {
// defaults
retryInterval = retryInterval || 10000;
retryLimit = retryLimit || 10;
var retryCount = 0;
var retryOnError = function(err) {
var failedId = err.requireModules && err.requireModules[0];
if (retryCount < retryLimit && failedId === libname) {
// this is what tells RequireJS not to cache the previous failure status
require.undef(failedId);
retryCount++;
console.log('retry ' + retryCount + ' of ' + retryLimit)
setTimeout(function(){
// No actual callback here. The original callback will get invoked.
require([libname], null, retryOnError);
}, retryInterval);
} else {
console.log('gave up', err)
}
}
// initial require of the lib, using the supplied callback plus our custom
// error callback defined above
require([libname], cb, retryOnError);
}
requireWithRetry('socketio', function(io) {
io.connect('http://localhost');
app._bootstrap();
});