Comparing version 2.0.0 to 2.1.0-beta.1
@@ -16,9 +16,17 @@ "use strict"; | ||
var Brusc = /*#__PURE__*/function () { | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
let Brusc = /*#__PURE__*/function () { | ||
function Brusc(inject) { | ||
var _this = this; | ||
_classCallCheck(this, Brusc); | ||
this._errorContainerNotCreated = function (key) { | ||
throw new Error("Cannot provide [" + (0, _utils.stringKey)(key) + "], call the Brusc#create method first"); | ||
}; | ||
_defineProperty(this, "_errorContainerNotCreated", key => { | ||
throw new Error(`Cannot provide [${(0, _utils.stringKey)(key)}], call the Brusc#create method first`); | ||
}); | ||
@@ -32,77 +40,79 @@ this._guardInjectIsFunction(inject); | ||
inject.provide = function (key) { | ||
return _this._errorContainerNotCreated(key); | ||
}; | ||
inject.provide = key => this._errorContainerNotCreated(key); | ||
} | ||
Brusc.define = function define(inject) { | ||
return new Brusc(inject); | ||
}; | ||
_createClass(Brusc, [{ | ||
key: "singleton", | ||
value: function singleton(key, provider, isEager) { | ||
this._guardKeyNotDefinedYet(key); | ||
var _proto = Brusc.prototype; | ||
this._instanceProviders.set(key, new _InstanceProvider.InstanceProvider({ | ||
key, | ||
provider: this._defaults[key] || provider, | ||
isEager | ||
})); | ||
_proto.singleton = function singleton(key, provider, isEager) { | ||
this._guardKeyNotDefinedYet(key); | ||
return this; | ||
} | ||
}, { | ||
key: "prototype", | ||
value: function prototype(key, provider) { | ||
this._guardKeyNotDefinedYet(key); | ||
this._instanceProviders.set(key, new _InstanceProvider.InstanceProvider({ | ||
key: key, | ||
provider: this._defaults[key] || provider, | ||
isEager: isEager | ||
})); | ||
this._instanceProviders.set(key, new _InstanceProvider.InstanceProvider({ | ||
key, | ||
provider: this._defaults[key] || provider, | ||
isSingleton: false | ||
})); | ||
return this; | ||
}; | ||
return this; | ||
} | ||
}, { | ||
key: "adapter", | ||
value: function adapter({ | ||
name, | ||
match, | ||
adapt | ||
} = {}) { | ||
this._adapters.push(new _InstanceAdapter.InstanceAdapter({ | ||
name, | ||
match, | ||
adapt | ||
})); | ||
_proto.prototype = function prototype(key, provider) { | ||
this._guardKeyNotDefinedYet(key); | ||
return this; | ||
} | ||
}, { | ||
key: "create", | ||
value: function create() { | ||
const container = new _Container.Container({ | ||
instanceProviders: this._instanceProviders, | ||
adapters: this._adapters | ||
}); | ||
this._inject.defaults = undefined; | ||
this._instanceProviders.set(key, new _InstanceProvider.InstanceProvider({ | ||
key: key, | ||
provider: this._defaults[key] || provider, | ||
isSingleton: false | ||
})); | ||
this._inject.provide = key => container.provide(key); | ||
return this; | ||
}; | ||
_proto.adapter = function adapter(_temp) { | ||
var _ref = _temp === void 0 ? {} : _temp, | ||
name = _ref.name, | ||
match = _ref.match, | ||
adapt = _ref.adapt; | ||
this._adapters.push(new _InstanceAdapter.InstanceAdapter({ | ||
name: name, | ||
match: match, | ||
adapt: adapt | ||
})); | ||
return this; | ||
}; | ||
_proto.create = function create() { | ||
var container = new _Container.Container({ | ||
instanceProviders: this._instanceProviders, | ||
adapters: this._adapters | ||
}); | ||
this._inject.defaults = undefined; | ||
this._inject.provide = function (key) { | ||
return container.provide(key); | ||
}; | ||
container.loadEagerInstances(); | ||
}; | ||
_proto._guardInjectIsFunction = function _guardInjectIsFunction(inject) { | ||
if (typeof inject !== 'function') { | ||
throw new Error('To enable injection, inject to define must be a function'); | ||
container.loadEagerInstances(); | ||
} | ||
}; | ||
_proto._guardKeyNotDefinedYet = function _guardKeyNotDefinedYet(key) { | ||
if (this._instanceProviders.has(key)) { | ||
throw new Error("[" + key + "] already defined, check singleton/prototype declaration key duplications"); | ||
}, { | ||
key: "_guardInjectIsFunction", | ||
value: function _guardInjectIsFunction(inject) { | ||
if (typeof inject !== 'function') { | ||
throw new Error('To enable injection, inject to define must be a function'); | ||
} | ||
} | ||
}; | ||
}, { | ||
key: "_guardKeyNotDefinedYet", | ||
value: function _guardKeyNotDefinedYet(key) { | ||
if (this._instanceProviders.has(key)) { | ||
throw new Error(`[${key}] already defined, check singleton/prototype declaration key duplications`); | ||
} | ||
} | ||
}], [{ | ||
key: "define", | ||
value: function define(inject) { | ||
return new Brusc(inject); | ||
} | ||
}]); | ||
@@ -109,0 +119,0 @@ return Brusc; |
@@ -8,6 +8,4 @@ "use strict"; | ||
var stringKey = function stringKey(key) { | ||
return key && key.name || key; | ||
}; | ||
const stringKey = key => key && key.name || key; | ||
exports.stringKey = stringKey; |
@@ -10,6 +10,15 @@ "use strict"; | ||
var Container = /*#__PURE__*/function () { | ||
function Container(_ref) { | ||
var instanceProviders = _ref.instanceProviders, | ||
adapters = _ref.adapters; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
let Container = /*#__PURE__*/function () { | ||
function Container({ | ||
instanceProviders, | ||
adapters | ||
}) { | ||
_classCallCheck(this, Container); | ||
this._instanceProviders = instanceProviders; | ||
@@ -21,53 +30,51 @@ this._adapters = adapters; | ||
var _proto = Container.prototype; | ||
_createClass(Container, [{ | ||
key: "provide", | ||
value: function provide(key) { | ||
if (this._keptInstances.has(key)) { | ||
return this._keptInstances.get(key); | ||
} | ||
_proto.provide = function provide(key) { | ||
if (this._keptInstances.has(key)) { | ||
return this._keptInstances.get(key); | ||
} | ||
this._guardNotInCircularDependency(key); | ||
this._guardNotInCircularDependency(key); | ||
this._guardHasInstanceProvider(key); | ||
this._guardHasInstanceProvider(key); | ||
this._circularDependencyControl.add(key); | ||
this._circularDependencyControl.add(key); | ||
const instanceProvider = this._instanceProviders.get(key); | ||
var instanceProvider = this._instanceProviders.get(key); | ||
let instance = instanceProvider.provide(key); | ||
var instance = instanceProvider.provide(key); | ||
this._circularDependencyControl.delete(key); | ||
this._circularDependencyControl["delete"](key); | ||
this._adapters.forEach(adapter => { | ||
instance = adapter.process(key, instance); | ||
}); | ||
this._adapters.forEach(function (adapter) { | ||
instance = adapter.process(key, instance); | ||
}); | ||
if (instanceProvider.isSingleton) { | ||
this._keptInstances.set(key, instance); | ||
} | ||
if (instanceProvider.isSingleton) { | ||
this._keptInstances.set(key, instance); | ||
return instance; | ||
} | ||
return instance; | ||
}; | ||
_proto.loadEagerInstances = function loadEagerInstances() { | ||
var _this = this; | ||
Array.from(this._instanceProviders.values()).forEach(function (instanceProvider) { | ||
return instanceProvider.isEager && _this.provide(instanceProvider.key); | ||
}); | ||
}; | ||
_proto._guardNotInCircularDependency = function _guardNotInCircularDependency(key) { | ||
if (this._circularDependencyControl.has(key)) { | ||
throw new Error("Circular dependency [" + Array.from(this._circularDependencyControl.values()).map(function (k) { | ||
return (0, _utils.stringKey)(k); | ||
}).join('->') + "->" + (0, _utils.stringKey)(key) + "]"); | ||
}, { | ||
key: "loadEagerInstances", | ||
value: function loadEagerInstances() { | ||
Array.from(this._instanceProviders.values()).forEach(instanceProvider => instanceProvider.isEager && this.provide(instanceProvider.key)); | ||
} | ||
}; | ||
_proto._guardHasInstanceProvider = function _guardHasInstanceProvider(key) { | ||
if (!this._instanceProviders.has(key)) { | ||
throw new Error("No instance provider defined for [" + (0, _utils.stringKey)(key) + "]"); | ||
}, { | ||
key: "_guardNotInCircularDependency", | ||
value: function _guardNotInCircularDependency(key) { | ||
if (this._circularDependencyControl.has(key)) { | ||
throw new Error(`Circular dependency [${Array.from(this._circularDependencyControl.values()).map(k => (0, _utils.stringKey)(k)).join('->')}->${(0, _utils.stringKey)(key)}]`); | ||
} | ||
} | ||
}; | ||
}, { | ||
key: "_guardHasInstanceProvider", | ||
value: function _guardHasInstanceProvider(key) { | ||
if (!this._instanceProviders.has(key)) { | ||
throw new Error(`No instance provider defined for [${(0, _utils.stringKey)(key)}]`); | ||
} | ||
} | ||
}]); | ||
@@ -74,0 +81,0 @@ return Container; |
@@ -10,8 +10,16 @@ "use strict"; | ||
var InstanceAdapter = /*#__PURE__*/function () { | ||
function InstanceAdapter(_ref) { | ||
var _ref$name = _ref.name, | ||
name = _ref$name === void 0 ? 'UnnamedInstanceAdapter' : _ref$name, | ||
match = _ref.match, | ||
adapt = _ref.adapt; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
let InstanceAdapter = /*#__PURE__*/function () { | ||
function InstanceAdapter({ | ||
name = 'UnnamedInstanceAdapter', | ||
match, | ||
adapt | ||
}) { | ||
_classCallCheck(this, InstanceAdapter); | ||
this._name = name; | ||
@@ -26,32 +34,36 @@ this._match = match; | ||
var _proto = InstanceAdapter.prototype; | ||
_createClass(InstanceAdapter, [{ | ||
key: "process", | ||
value: function process(key, instance) { | ||
return this._match(key) === true ? this._build(key, instance) : instance; | ||
} | ||
}, { | ||
key: "_build", | ||
value: function _build(key, instance) { | ||
try { | ||
const adapted = this._adapt(instance, key); | ||
_proto.process = function process(key, instance) { | ||
return this._match(key) === true ? this._build(key, instance) : instance; | ||
}; | ||
this._guardAdaptedInstanceExists(key, adapted); | ||
_proto._build = function _build(key, instance) { | ||
try { | ||
var adapted = this._adapt(instance, key); | ||
this._guardAdaptedInstanceExists(key, adapted); | ||
return adapted; | ||
} catch (error) { | ||
error.message = "[" + this._name + "] failed to adapt [" + (0, _utils.stringKey)(key) + "], " + error.message; | ||
throw error; | ||
return adapted; | ||
} catch (error) { | ||
error.message = `[${this._name}] failed to adapt [${(0, _utils.stringKey)(key)}], ${error.message}`; | ||
throw error; | ||
} | ||
} | ||
}; | ||
_proto._guardIsFunction = function _guardIsFunction(property, value) { | ||
if (typeof value !== 'function') { | ||
throw new Error("[" + this._name + "] " + property + " must be a function"); | ||
}, { | ||
key: "_guardIsFunction", | ||
value: function _guardIsFunction(property, value) { | ||
if (typeof value !== 'function') { | ||
throw new Error(`[${this._name}] ${property} must be a function`); | ||
} | ||
} | ||
}; | ||
_proto._guardAdaptedInstanceExists = function _guardAdaptedInstanceExists(key, adapted) { | ||
if (!adapted) { | ||
throw new Error("[" + this._name + "] did not generate an instance"); | ||
}, { | ||
key: "_guardAdaptedInstanceExists", | ||
value: function _guardAdaptedInstanceExists(key, adapted) { | ||
if (!adapted) { | ||
throw new Error(`[${this._name}] did not generate an instance`); | ||
} | ||
} | ||
}; | ||
}]); | ||
@@ -58,0 +70,0 @@ return InstanceAdapter; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -10,14 +8,19 @@ value: true | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/createClass")); | ||
var _utils = require("../common/utils"); | ||
var InstanceProvider = /*#__PURE__*/function () { | ||
function InstanceProvider(_ref) { | ||
var key = _ref.key, | ||
provider = _ref.provider, | ||
_ref$isSingleton = _ref.isSingleton, | ||
isSingleton = _ref$isSingleton === void 0 ? true : _ref$isSingleton, | ||
_ref$isEager = _ref.isEager, | ||
isEager = _ref$isEager === void 0 ? false : _ref$isEager; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
let InstanceProvider = /*#__PURE__*/function () { | ||
function InstanceProvider({ | ||
key, | ||
provider, | ||
isSingleton = true, | ||
isEager = false | ||
}) { | ||
_classCallCheck(this, InstanceProvider); | ||
this._key = key; | ||
@@ -33,33 +36,14 @@ this._provider = provider; | ||
var _proto = InstanceProvider.prototype; | ||
_createClass(InstanceProvider, [{ | ||
key: "provide", | ||
value: function provide() { | ||
const instance = this._provider(); | ||
_proto.provide = function provide() { | ||
var instance = this._provider(); | ||
this._guardInstanceIsGenerated(instance); | ||
this._guardInstanceIsGenerated(instance); | ||
return instance; | ||
}; | ||
_proto._guardKeyHasValue = function _guardKeyHasValue() { | ||
if (!this._key) { | ||
throw new Error('Key has no value'); | ||
return instance; | ||
} | ||
}; | ||
_proto._guardProviderIsFunction = function _guardProviderIsFunction() { | ||
if (typeof this._provider !== 'function') { | ||
throw new Error("Instance provider for [" + (0, _utils.stringKey)(this._key) + "] must be a function"); | ||
} | ||
}; | ||
_proto._guardInstanceIsGenerated = function _guardInstanceIsGenerated(instance) { | ||
if (!instance) { | ||
throw new Error("Instance provider for [" + (0, _utils.stringKey)(this._key) + "] did not generate an instance"); | ||
} | ||
}; | ||
(0, _createClass2["default"])(InstanceProvider, [{ | ||
}, { | ||
key: "key", | ||
get: function get() { | ||
get: function () { | ||
return this._key; | ||
@@ -69,3 +53,3 @@ } | ||
key: "isSingleton", | ||
get: function get() { | ||
get: function () { | ||
return this._isSingleton; | ||
@@ -75,6 +59,28 @@ } | ||
key: "isEager", | ||
get: function get() { | ||
get: function () { | ||
return this._isEager; | ||
} | ||
}, { | ||
key: "_guardKeyHasValue", | ||
value: function _guardKeyHasValue() { | ||
if (!this._key) { | ||
throw new Error('Key has no value'); | ||
} | ||
} | ||
}, { | ||
key: "_guardProviderIsFunction", | ||
value: function _guardProviderIsFunction() { | ||
if (typeof this._provider !== 'function') { | ||
throw new Error(`Instance provider for [${(0, _utils.stringKey)(this._key)}] must be a function`); | ||
} | ||
} | ||
}, { | ||
key: "_guardInstanceIsGenerated", | ||
value: function _guardInstanceIsGenerated(instance) { | ||
if (!instance) { | ||
throw new Error(`Instance provider for [${(0, _utils.stringKey)(this._key)}] did not generate an instance`); | ||
} | ||
} | ||
}]); | ||
return InstanceProvider; | ||
@@ -81,0 +87,0 @@ }(); |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -12,2 +12,2 @@ var _Brusc = require("./brusc/Brusc"); | ||
var _default = _Brusc.Brusc; | ||
exports["default"] = _default; | ||
exports.default = _default; |
{ | ||
"name": "brusc", | ||
"version": "2.0.0", | ||
"version": "2.1.0-beta.1", | ||
"description": "IOC Container for Effective JS development", | ||
@@ -30,11 +30,12 @@ "main": "dist", | ||
"devDependencies": { | ||
"@babel/cli": "7.8.0", | ||
"@babel/core": "7.8.0", | ||
"@babel/plugin-transform-modules-commonjs": "7.8.0", | ||
"@babel/register": "7.8.0", | ||
"@babel/runtime": "7.8.0", | ||
"@babel/cli": "7.13.0", | ||
"@babel/core": "7.13.8", | ||
"@babel/plugin-proposal-class-properties": "7.13.0", | ||
"@babel/plugin-transform-classes": "7.13.0", | ||
"@babel/plugin-transform-modules-commonjs": "7.13.8", | ||
"@babel/register": "7.13.8", | ||
"@babel/runtime": "7.13.9", | ||
"@s-ui/lint": "3", | ||
"@s-ui/test": "4", | ||
"babel-loader": "8.0.6", | ||
"babel-preset-sui": "3", | ||
"chai": "4.2.0", | ||
@@ -48,7 +49,6 @@ "codecov": "3.6.5", | ||
"babel": { | ||
"presets": [ | ||
"sui" | ||
], | ||
"plugins": [ | ||
"@babel/plugin-transform-modules-commonjs" | ||
"@babel/plugin-transform-modules-commonjs", | ||
"@babel/plugin-proposal-class-properties", | ||
"@babel/plugin-transform-classes" | ||
] | ||
@@ -66,2 +66,2 @@ }, | ||
"dependencies": {} | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25662
300
16
2