Last active
August 29, 2015 14:07
-
-
Save i09158knct/57deb4512841ae10dd3c to your computer and use it in GitHub Desktop.
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
window.DI = function(key) { | |
return [DI, key]; | |
}; | |
DI.toString = function() { | |
return '@Inject'; | |
}; | |
DI._parseArgs = function(args) { | |
args = [].slice.call(args); | |
var info = {}; | |
info.key = args[0]; | |
info.deps = args.slice(1, -1); | |
info.target = args[args.length - 1]; | |
return info; | |
}; | |
DI._construct = function(constructor, prototype, args) { | |
function Constructor() { | |
return constructor.apply(this, args); | |
} | |
Constructor.prototype = prototype; | |
return new Constructor(); | |
}; | |
DI.Container = function() { | |
var self = this; | |
self._instances = {}; | |
self._constructors = {}; | |
self._singletonConstructors = {}; | |
self._factories = {}; | |
self._singletonFactories = {}; | |
self.instance = function(key, val) { | |
self._instances[key] = val; | |
return self; | |
}; | |
self.constructor = function() { | |
var info = DI._parseArgs(arguments); | |
self._constructors[info.key] = info; | |
return self; | |
}; | |
self.singletonConstructor = function() { | |
var info = DI._parseArgs(arguments); | |
self._singletonConstructors[info.key] = info; | |
return self; | |
}; | |
self.factory = function() { | |
var info = DI._parseArgs(arguments); | |
self._factories[info.key] = info; | |
return self; | |
}; | |
self.singletonFactory = function() { | |
var info = DI._parseArgs(arguments); | |
self._singletonFactories[info.key] = info; | |
return self; | |
}; | |
self.get = function(key) { | |
var info; | |
var target; | |
var proto; | |
var depInstances; | |
var instance; | |
if (key in self._instances) { | |
return self._instances[key]; | |
} | |
if (key in self._constructors) { | |
info = self._constructors[key]; | |
target = info.target; | |
proto = target.prototype; | |
depInstances = info.deps.map(self.get); | |
instance = DI._construct(target, proto, depInstances); | |
self.inject(instance); | |
return instance; | |
} | |
if (key in self._singletonConstructors) { | |
info = self._singletonConstructors[key]; | |
if ('instance' in info) { return info.instance; } | |
target = info.target; | |
proto = target.prototype; | |
depInstances = info.deps.map(self.get); | |
info.instance = DI._construct(target, proto, depInstances); | |
self.inject(info.instance); | |
return info.instance; | |
} | |
if (key in self._factories) { | |
info = self._factories[key]; | |
target = info.target; | |
depInstances = info.deps.map(self.get); | |
instance = (function() { | |
return target.apply(this, depInstances); | |
})(); | |
return instance; | |
} | |
if (key in self._singletonFactories) { | |
info = self._singletonFactories[key]; | |
if ('instance' in info) { return info.instance; } | |
target = info.target; | |
depInstances = info.deps.map(self.get); | |
info.instance = (function() { | |
return target.apply(this, depInstances); | |
})(); | |
self.inject(info.instance); | |
return info.instance; | |
} | |
throw new Error(key + ' is not registered'); | |
}; | |
self.inject = function(ins) { | |
for (var prop in ins) { | |
if (ins[prop] != null && ins[prop][0] === DI) { | |
ins[prop] = self.get(ins[prop][1]); | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment