Created
July 7, 2016 06:11
-
-
Save privatenumber/2d77707fa43a3944d37bc3e877a5a9f3 to your computer and use it in GitHub Desktop.
Require.js "Watch" Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var reload = (function () { | |
'use strict'; | |
var cache = {}; | |
var refresh = {}; | |
var execCb = window.requirejs.s.contexts._.execCb; | |
window.requirejs.s.contexts._.execCb = function (name, callback) { | |
if (cache[name]) { | |
var _callback = callback + ''; | |
refresh[name] = (cache[name] !== _callback); | |
cache[name] = _callback; | |
} | |
return execCb.apply(this, arguments); | |
}; | |
return function reload (paths, callback, interval) { | |
// Start watching | |
paths.forEach(function (path) { | |
cache[path] = true; | |
}); | |
setInterval(function () { | |
require(paths, function () { | |
var reload; | |
paths.forEach(function (path) { | |
requirejs.undef(path); | |
reload = refresh[path]; | |
refresh[path] = false; | |
}); | |
if (reload) { | |
console.info('Reloading', paths); | |
callback.apply(this, arguments); | |
} | |
}); | |
}, interval || 2000); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment