brusc
Version:
IOC Container for Effective JS development
72 lines (57 loc) • 2.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.InstanceAdapter = void 0;
var _utils = require("../common/utils");
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; }
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;
_classCallCheck(this, InstanceAdapter);
this._name = name;
this._match = match;
this._adapt = adapt;
this._guardIsFunction('match', match);
this._guardIsFunction('adapt', adapt);
}
_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 {
var adapted = this._adapt(instance, key);
this._guardAdaptedInstanceExists(key, adapted);
return adapted;
} catch (error) {
error.message = "[".concat(this._name, "] failed to adapt [").concat((0, _utils.stringKey)(key), "], ").concat(error.message);
throw error;
}
}
}, {
key: "_guardIsFunction",
value: function _guardIsFunction(property, value) {
if (typeof value !== 'function') {
throw new Error("[".concat(this._name, "] ").concat(property, " must be a function"));
}
}
}, {
key: "_guardAdaptedInstanceExists",
value: function _guardAdaptedInstanceExists(key, adapted) {
if (!adapted) {
throw new Error("[".concat(this._name, "] did not generate an instance"));
}
}
}]);
return InstanceAdapter;
}();
exports.InstanceAdapter = InstanceAdapter;
;