New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

cubekit-ioc

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cubekit-ioc - npm Package Compare versions

Comparing version

to
0.0.5

@@ -18,2 +18,4 @@ 'use strict';

function Container() {
var params = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, Container);

@@ -26,4 +28,39 @@

this.instance(Container, this);
if (params.parent) {
this._setParent(params.parent);
}
}
/**
* Make a "branch"-container from the current container.
*
* The new container inherits all singletons and instances
* that was registered before the forking.
*
* Singletons and instances that will be registered after
* the forking will not affect each other.
*
* ```js
* const ioc = new Container
*
* ioc.singleton(Foo)
*
* const fork = ioc.fork()
*
* ioc.singleton(Bar)
* fork.singleton(Baz)
*
* console.log(fork.make(Foo) == ioc.make(Foo))
* console.log(fork.make(Bar) != ioc.make(Bar))
* console.log(fork.make(Baz) != ioc.make(Baz))
* ```
*
* @returns {Container}
*/
Container.prototype.fork = function fork() {
return new Container({ parent: this });
};
Container.prototype.singleton = function singleton(Type) {

@@ -43,6 +80,10 @@ this._singletons = this._singletons.set(Type, true);

Container.prototype.make = function make(Type) {
this.registerAsSingletonIfFlagged(Type);
this._registerAsSingletonIfFlagged(Type);
if (this._isInheritedSingleton(Type)) {
return this._parent.make(Type);
}
if (this._isSingleton(Type)) {
return this._getSingleton(Type);
return this._getOrMakeSingletonInstance(Type);
}

@@ -57,3 +98,7 @@

Container.prototype.registerAsSingletonIfFlagged = function registerAsSingletonIfFlagged(Type) {
Container.prototype.getSingletons = function getSingletons() {
return this._singletons;
};
Container.prototype._registerAsSingletonIfFlagged = function _registerAsSingletonIfFlagged(Type) {
if (this._flaggedAsSingleton(Type)) {

@@ -64,2 +109,7 @@ this.singleton(Type);

Container.prototype._setParent = function _setParent(container) {
this._parent = container;
this._inheritedSingletons = this._parent.getSingletons();
};
Container.prototype._flaggedAsSingleton = function _flaggedAsSingleton(Type) {

@@ -69,2 +119,8 @@ return _lodash2['default'].get(Type.__cubekitMeta__, 'useAsSingleton');

/**
* @param {*} Type
* @returns {boolean}
* @private
*/
Container.prototype._isSingleton = function _isSingleton(Type) {

@@ -74,9 +130,39 @@ return !!this._singletons.get(Type);

Container.prototype._getSingleton = function _getSingleton(Type) {
/**
* Returns `true` if this container was forked from another,
* and at the moment of the fork this `Type` was registered
* as a singleton in the original container.
*
* @param {*} Type
* @returns {boolean}
* @private
*/
Container.prototype._isInheritedSingleton = function _isInheritedSingleton(Type) {
return !!(this._inheritedSingletons && this._inheritedSingletons.has(Type));
};
/**
* @param {*} Type
* @returns {*}
* @private
*/
Container.prototype._getOrMakeSingletonInstance = function _getOrMakeSingletonInstance(Type) {
if (!this._instances.has(Type)) {
this._setInstance(Type, this._instantiate(Type));
}
return this._instances.get(Type);
};
/**
* Recursively instantiates the given type and all his deps.
*
* @param {*} Type
* @param {Array<*>} args
* @returns {*}
* @private
*/
Container.prototype._instantiate = function _instantiate(Type) {

@@ -91,2 +177,9 @@ var args = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

/**
* @param {*} Type
* @param {Array<*>} args
* @returns {Array}
* @private
*/
Container.prototype._makeDeps = function _makeDeps(Type, args) {

@@ -103,2 +196,8 @@ var _this = this;

/**
* @param {*} Type
* @returns {Array<*>}
* @private
*/
Container.prototype._getConstructorTypes = function _getConstructorTypes(Type) {

@@ -108,2 +207,8 @@ return _lodash2['default'].get(Type.__cubekitMeta__, 'constructor.types') || [];

/**
* @param {*} Type
* @returns {*}
* @private
*/
Container.prototype._getBindingOrReturnType = function _getBindingOrReturnType(Type) {

@@ -116,2 +221,8 @@ if (this._bindings.has(Type)) {

/**
* @param {*} Type
* @param {*} instance
* @private
*/
Container.prototype._setInstance = function _setInstance(Type, instance) {

@@ -118,0 +229,0 @@ this._instances = this._instances.set(Type, instance);

{
"name": "cubekit-ioc",
"version": "0.0.4",
"version": "0.0.5",
"description": "Cubekit IoC Container",

@@ -5,0 +5,0 @@ "main": "lib/index.js",