Skip to content

Instantly share code, notes, and snippets.

@arthyn
Created October 2, 2017 03:41
Show Gist options
  • Save arthyn/0b8f9968f3650bf86d19637207340ecd to your computer and use it in GitHub Desktop.
Save arthyn/0b8f9968f3650bf86d19637207340ecd to your computer and use it in GitHub Desktop.
LibrarySystem with Dependencies
(function() {
var libraryStorage = {};
var Library = function(dependencies, callback) {
this.dependencies = dependencies;
this.generator = callback;
};
function retreiveLibrary(libraryName) {
var library = libraryStorage[libraryName];
var dependencies;
if(library.dependencies.length === 0) {
return library.generator();
} else {
dependencies = library.dependencies.reduce(function(loadedDependencies, dependency) {
loadedDependencies.push(retreiveLibrary(dependency));
return loadedDependencies;
}, []);
return library.generator.apply(null, dependencies);
}
}
function librarySystem(libraryName, dependencies, callback) {
if(arguments.length > 2) {
libraryStorage[libraryName] = new Library(dependencies, callback);
} else {
return retreiveLibrary(libraryName);
}
}
window.librarySystem = librarySystem;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment