Skip to content

Instantly share code, notes, and snippets.

@privatenumber
Created July 7, 2016 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save privatenumber/2d77707fa43a3944d37bc3e877a5a9f3 to your computer and use it in GitHub Desktop.
Save privatenumber/2d77707fa43a3944d37bc3e877a5a9f3 to your computer and use it in GitHub Desktop.
Require.js "Watch" Implementation
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