szurubooru/public_html/js/DI.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-08-31 23:22:56 +02:00
var App = App || {};
App.DI = (function() {
2015-06-28 10:07:11 +02:00
var factories = {};
var instances = {};
function get(key) {
var instance = instances[key];
if (!instance) {
var factory = factories[key];
if (!factory) {
throw new Error('Unregistered key: ' + key);
}
var objectInitializer = factory.initializer;
var singleton = factory.singleton;
var deps = resolveDependencies(objectInitializer, factory.dependencies);
instance = {};
instance = objectInitializer.apply(instance, deps);
if (singleton) {
instances[key] = instance;
}
}
return instance;
}
function resolveDependencies(objectIntializer, depKeys) {
var deps = [];
for (var i = 0; i < depKeys.length; i ++) {
deps[i] = get(depKeys[i]);
}
return deps;
}
function register(key, dependencies, objectInitializer) {
factories[key] = {initializer: objectInitializer, singleton: false, dependencies: dependencies};
}
function registerSingleton(key, dependencies, objectInitializer) {
factories[key] = {initializer: objectInitializer, singleton: true, dependencies: dependencies};
}
function registerManual(key, objectInitializer) {
instances[key] = objectInitializer();
}
return {
get: get,
register: register,
registerManual: registerManual,
registerSingleton: registerSingleton,
};
2014-08-31 23:22:56 +02:00
})();