Skip to content

Instantly share code, notes, and snippets.

@stepheneb
Last active January 7, 2020 21:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stepheneb/9a0c67bb925d439b294052b6acee6bee to your computer and use it in GitHub Desktop.
Save stepheneb/9a0c67bb925d439b294052b6acee6bee to your computer and use it in GitHub Desktop.
very simple javascript module dependency manager
// This simple module makes available a UUID generator created by Jeff Ward
//
// This module will be available under the global App.UUID
//
// After this module has completely loaded it registers itself so other modules that
// depend on it being present can wait until it is available to complete their startup.
**
* Fast UUID generator, RFC4122 version 4 compliant.
* @author Jeff Ward (jcward.com).
* @license MIT license
* @link http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
*
*/
(function() {
jQuery(document).ready(function() {
App.UUID = (function() {
var i, lut, self;
self = {};
lut = [];
i = 0;
while (i < 256) {
lut[i] = (i < 16 ? '0' : '') + i.toString(16);
i++;
}
self.generate = function() {
var d0, d1, d2, d3;
d0 = Math.random() * 0xffffffff | 0;
d1 = Math.random() * 0xffffffff | 0;
d2 = Math.random() * 0xffffffff | 0;
d3 = Math.random() * 0xffffffff | 0;
return lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + '-' + lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + '-' + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + '-' + lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + '-' + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] + lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
};
return self;
})();
return App.component.register('uuid');
});
}).call(this);
// In a second module that requires the presence of App.UUID to function
// this function is called after the second module is loaded.
// The function finishSecondModuleSetup() will be called after App.UUID
// has loaded and registered itself.
App.component.callbackWhenAvailable('uuid', finishSecondModuleSetup)
// A very simple App component module system I wrote.
//
// When a JavaScript module has completed loading the module should resister itself:
//
// example: App.component.register('uuid');
//
// If a module depends on the presense of another module before it
// can complete it's startup it can request a callbackWhenAvailable when the
// other module is registered and available.
//
// Example after loading second-module.js: App.component.callbackWhenAvailable('uuid', finishSecondModuleSetup)
(function() {
(function() {
this.App || (this.App = {});
return App.component = (function() {
var callbackWhenAvailable, components, register, self;
components = {};
register = function(component) {
if (components[component]) {
if (components[component].registered) {
return console("*** Component: " + component + " already registered");
} else {
components[component].registered = true;
components[component].callbackWhenAvailables.forEach(function(callbackWhenAvailable) {
callbackWhenAvailable();
});
return components[component].callbackWhenAvailables = [];
}
} else {
components[component] = {};
components[component].registered = true;
return components[component].callbackWhenAvailables = [];
}
};
callbackWhenAvailable = function(component, callbackWhenAvailable) {
if (!components[component]) {
components[component] = {};
components[component].callbackWhenAvailables = [];
}
if (components[component].registered) {
return callbackWhenAvailable();
} else {
return components[component].callbackWhenAvailables.push(callbackWhenAvailable);
}
};
return self = {
register: register,
callbackWhenAvailable: callbackWhenAvailable,
components: components
};
})();
}).call(this);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment