zig*_*ack 4 javascript amd node.js requirejs socket.io
所以我正在编写一个使用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);
});
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个老问题,但这对我来说很有趣,所以我调查了一下......
有一个require.undef方法,你需要为了告诉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();
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
751 次 |
最近记录: |