Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

javascript - Poll for resource available with RequireJS

So I'm writing an app using RequireJS and Socket.io that checks to see if the socket.io resource is available and then, on connection, bootstraps the app. In case socket.io ever temporarily goes down I'd like to have requireJS poll for the resource a few times until it is available and then continue on initializing the application.

Unfortunately (or maybe fortunately?) it seems like there is some sort of caching mechanism in require that registers scripterrors for scripts that don't load so that if you do a setTimeout in the error callback that retrys the socketio require function, require will continue to throw errors even when the resource becomes available.

Is this an oversight or is there a reason for keeping this error cached? More importantly, is there a workaround to allow require retrys?

Here's an example of what I've been trying:

function initialize() {
  require(['socketio', function(io) {
    io.connect('http://localhost');
    app._bootstrap();
  }, function(err) {
    console.log(err);
    setTimeout(initialize, 10000);
  });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know this is an old question, but it was intriguing to me so I looked into it...

There is a require.undef method you need to call in order to tell RequireJS not to cache the prior failure status of the load. See also errbacks example.

Then you can simply call require again with a null callback. The original callback will still be called -- no need for the recursion. Something like this:

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();
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...