@baizey/dependency-injection
Version:
A simple typescript dependency injection
110 lines (109 loc) • 4.16 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebugServiceProvider = void 0;
var utils_1 = require("../utils");
var Errors_1 = require("../Errors");
var proxyOf = function (self, context) {
return new Proxy(self, { get: function (t, p) { return t.provide(p, context); } });
};
var DebugServiceProvider = /** @class */ (function () {
function DebugServiceProvider(lifetimes) {
Object.defineProperty(this, "instances", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
Object.defineProperty(this, "proxy", {
enumerable: true,
configurable: true,
writable: true,
value: proxyOf(this)
});
Object.defineProperty(this, "lifetimes", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.lifetimes = lifetimes;
}
Object.defineProperty(DebugServiceProvider.prototype, "createProxy", {
enumerable: false,
configurable: true,
writable: true,
value: function (context) {
return proxyOf(this, context);
}
});
Object.defineProperty(DebugServiceProvider.prototype, "provide", {
enumerable: false,
configurable: true,
writable: true,
value: function (selector, context) {
var key = (0, utils_1.extractSelector)(selector);
if (this.instances[key])
return this.instances[key];
if (context === null || context === void 0 ? void 0 : context.instances[key])
return context.instances[key];
var lifetime = this.lifetimes[key];
var newContext = this.enterLifetime(key, context, lifetime);
var result = lifetime.validate(this, newContext);
newContext.isEscaped = true;
return result;
}
});
Object.defineProperty(DebugServiceProvider.prototype, "enterLifetime", {
enumerable: false,
configurable: true,
writable: true,
value: function (key, currentContext, lifetime) {
var _a;
if (!currentContext)
currentContext = this.createContext(true);
if (!lifetime)
throw new Errors_1.ExistenceDependencyError(key);
if (lifetime.name in currentContext.lookup) {
throw new Errors_1.CircularDependencyError(lifetime.name, currentContext.ordered.map(function (e) { return e.name; }));
}
return {
parent: currentContext,
instances: currentContext.instances,
ordered: currentContext.ordered.map(function (e) { return e; }).concat([lifetime]),
lookup: __assign(__assign({}, currentContext.lookup), (_a = {}, _a[lifetime.name] = lifetime, _a)),
depth: currentContext.depth + 1,
lastSingleton: lifetime.isSingleton ? lifetime : currentContext.lastSingleton,
isEscaped: false,
};
}
});
Object.defineProperty(DebugServiceProvider.prototype, "createContext", {
enumerable: false,
configurable: true,
writable: true,
value: function (createDummyParent) {
return {
instances: {},
depth: 0,
isEscaped: false,
lastSingleton: null,
parent: createDummyParent ? this.createContext(false) : null,
lookup: {},
ordered: [],
};
}
});
return DebugServiceProvider;
}());
exports.DebugServiceProvider = DebugServiceProvider;