Created
October 2, 2017 03:41
-
-
Save arthyn/0b8f9968f3650bf86d19637207340ecd to your computer and use it in GitHub Desktop.
LibrarySystem with Dependencies
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
(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