@injex/core
Advanced tools
Comparing version 3.5.1 to 4.0.0-alpha.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.alias = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
function alias() { | ||
var names = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
names[_i] = arguments[_i]; | ||
} | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function alias(...names) { | ||
return function (targetConstructor) { | ||
for (var i = 0, len = names.length; i < len; i++) { | ||
for (let i = 0, len = names.length; i < len; i++) { | ||
metadataHandlers_1.default.pushMetadata(targetConstructor.prototype, "aliases", names[i]); | ||
@@ -13,0 +9,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bootstrap = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
var constants_1 = require("../constants"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
const constants_1 = require("../constants"); | ||
function bootstrap() { | ||
@@ -7,0 +7,0 @@ return function (targetConstructor) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.define = void 0; | ||
var stdlib_1 = require("@injex/stdlib"); | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const stdlib_1 = require("@injex/stdlib"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function getDependencyName(item, name) { | ||
@@ -7,0 +7,0 @@ return name || stdlib_1.toCamelCase(item.name); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.init = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function init() { | ||
@@ -6,0 +6,0 @@ return function (targetPrototype, methodName) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.inject = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function inject(dependencyNameOrType) { | ||
@@ -6,0 +6,0 @@ return function (targetPrototype, dependency) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.injectAlias = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function injectAlias(alias, keyBy) { | ||
@@ -9,4 +9,4 @@ return function (targetPrototype, dependency) { | ||
label: dependency, | ||
alias: alias, | ||
keyBy: keyBy | ||
alias, | ||
keyBy | ||
}); | ||
@@ -13,0 +13,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.injectFactory = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function injectFactory(dependencyNameOrType) { | ||
@@ -6,0 +6,0 @@ return function (targetPrototype, dependency) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.injectParam = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
var ARGS_REGEXP = /\(\s*([^)]+?)\s*\)/; | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
const ARGS_REGEXP = /\(\s*([^)]+?)\s*\)/; | ||
function injectParam(dependencyNameOrType) { | ||
return function (targetPrototype, methodName, index) { | ||
var args = ARGS_REGEXP.exec(targetPrototype[methodName].toString()); | ||
const args = ARGS_REGEXP.exec(targetPrototype[methodName].toString()); | ||
if (args[1]) { | ||
var params = args[1].replace(/ /g, '').split(','); | ||
var dependency = params[index]; | ||
const params = args[1].replace(/ /g, '').split(','); | ||
const dependency = params[index]; | ||
metadataHandlers_1.default.pushMetadata(targetPrototype, "paramDependencies", { | ||
methodName: methodName, | ||
index: index, | ||
methodName, | ||
index, | ||
label: dependency, | ||
@@ -16,0 +16,0 @@ value: dependencyNameOrType || dependency |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.lazy = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function lazy() { | ||
@@ -6,0 +6,0 @@ return function (targetConstructor) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ready = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function ready() { | ||
@@ -6,0 +6,0 @@ return function (targetPrototype, methodName) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.singleton = void 0; | ||
var metadataHandlers_1 = require("../metadataHandlers"); | ||
const metadataHandlers_1 = require("../metadataHandlers"); | ||
function singleton() { | ||
@@ -6,0 +6,0 @@ return function (targetConstructor) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FactoryModuleNotExistsError = exports.InvalidPluginError = exports.DuplicateDefinitionError = exports.InitializeMuduleError = exports.BootstrapError = void 0; | ||
var tslib_1 = require("tslib"); | ||
var stdlib_1 = require("@injex/stdlib"); | ||
var BootstrapError = /** @class */ (function (_super) { | ||
tslib_1.__extends(BootstrapError, _super); | ||
function BootstrapError(message) { | ||
return _super.call(this, "Bootstrap failed: " + message) || this; | ||
const stdlib_1 = require("@injex/stdlib"); | ||
class BootstrapError extends Error { | ||
constructor(message) { | ||
super(`Bootstrap failed: ${message}`); | ||
} | ||
return BootstrapError; | ||
}(Error)); | ||
} | ||
exports.BootstrapError = BootstrapError; | ||
var InitializeMuduleError = /** @class */ (function (_super) { | ||
tslib_1.__extends(InitializeMuduleError, _super); | ||
function InitializeMuduleError(moduleName) { | ||
return _super.call(this, "Failed to initialize module '" + String(moduleName) + "'.") || this; | ||
class InitializeMuduleError extends Error { | ||
constructor(moduleName) { | ||
super(`Failed to initialize module '${String(moduleName)}'.`); | ||
} | ||
return InitializeMuduleError; | ||
}(Error)); | ||
} | ||
exports.InitializeMuduleError = InitializeMuduleError; | ||
var DuplicateDefinitionError = /** @class */ (function (_super) { | ||
tslib_1.__extends(DuplicateDefinitionError, _super); | ||
function DuplicateDefinitionError(moduleName) { | ||
return _super.call(this, "Module '" + String(moduleName) + "' already defined.") || this; | ||
class DuplicateDefinitionError extends Error { | ||
constructor(moduleName) { | ||
super(`Module '${String(moduleName)}' already defined.`); | ||
} | ||
return DuplicateDefinitionError; | ||
}(Error)); | ||
} | ||
exports.DuplicateDefinitionError = DuplicateDefinitionError; | ||
var InvalidPluginError = /** @class */ (function (_super) { | ||
tslib_1.__extends(InvalidPluginError, _super); | ||
function InvalidPluginError(plugin) { | ||
return _super.call(this, "Plugin " + stdlib_1.getConstructorName(plugin) + " is not valid. Make sure the 'apply' method exists.") || this; | ||
class InvalidPluginError extends Error { | ||
constructor(plugin) { | ||
super(`Plugin ${stdlib_1.getConstructorName(plugin)} is not valid. Make sure the 'apply' method exists.`); | ||
} | ||
return InvalidPluginError; | ||
}(Error)); | ||
} | ||
exports.InvalidPluginError = InvalidPluginError; | ||
var FactoryModuleNotExistsError = /** @class */ (function (_super) { | ||
tslib_1.__extends(FactoryModuleNotExistsError, _super); | ||
function FactoryModuleNotExistsError(factory) { | ||
return _super.call(this, (typeof factory === "string" ? factory : factory.name) + " is not a factory module.") || this; | ||
class FactoryModuleNotExistsError extends Error { | ||
constructor(factory) { | ||
super(`${typeof factory === "string" ? factory : factory.name} is not a factory module.`); | ||
} | ||
return FactoryModuleNotExistsError; | ||
}(Error)); | ||
} | ||
exports.FactoryModuleNotExistsError = FactoryModuleNotExistsError; | ||
//# sourceMappingURL=errors.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Injex = exports.errors = exports.singleton = exports.ready = exports.lazy = exports.injectParam = exports.injectFactory = exports.injectAlias = exports.inject = exports.init = exports.define = exports.bootstrap = exports.alias = exports.LogLevel = exports.Logger = void 0; | ||
var tslib_1 = require("tslib"); | ||
const tslib_1 = require("tslib"); | ||
var stdlib_1 = require("@injex/stdlib"); | ||
@@ -6,0 +6,0 @@ Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return stdlib_1.Logger; } }); |
560
lib/injex.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var stdlib_1 = require("@injex/stdlib"); | ||
var constants_1 = require("./constants"); | ||
var errors_1 = require("./errors"); | ||
var metadataHandlers_1 = require("./metadataHandlers"); | ||
var InjexContainer = /** @class */ (function () { | ||
function InjexContainer(config) { | ||
const stdlib_1 = require("@injex/stdlib"); | ||
const constants_1 = require("./constants"); | ||
const errors_1 = require("./errors"); | ||
const metadataHandlers_1 = require("./metadataHandlers"); | ||
class InjexContainer { | ||
constructor(config) { | ||
this.config = this.createConfig(config); | ||
@@ -21,82 +20,54 @@ this._logger = new stdlib_1.Logger(this.config.logLevel, this.config.logNamespace); | ||
} | ||
Object.defineProperty(InjexContainer.prototype, "logger", { | ||
get: function () { | ||
return this._logger; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
InjexContainer.create = function (config) { | ||
get logger() { | ||
return this._logger; | ||
} | ||
static create(config) { | ||
throw new Error("Static method create not implemented."); | ||
}; | ||
InjexContainer.prototype.bootstrap = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var bootstrapModule, e_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (this._didBootstrapCalled) { | ||
throw new errors_1.BootstrapError('container bootstrap should run only once.'); | ||
} | ||
this._didBootstrapCalled = true; | ||
return [4 /*yield*/, this._initPlugins()]; | ||
case 1: | ||
_a.sent(); | ||
this.hooks.beforeRegistration.call(); | ||
return [4 /*yield*/, this.loadContainerFiles()]; | ||
case 2: | ||
_a.sent(); | ||
this.hooks.afterRegistration.call(); | ||
if (!this.config.perfMode) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, this._createModulesAsync()]; | ||
case 3: | ||
_a.sent(); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
this._createModules(); | ||
_a.label = 5; | ||
case 5: | ||
bootstrapModule = this.get(constants_1.bootstrapSymbol); | ||
_a.label = 6; | ||
case 6: | ||
_a.trys.push([6, 9, , 10]); | ||
return [4 /*yield*/, this._initializeModules()]; | ||
case 7: | ||
_a.sent(); | ||
this.hooks.bootstrapRun.call(); | ||
return [4 /*yield*/, (bootstrapModule === null || bootstrapModule === void 0 ? void 0 : bootstrapModule.run())]; | ||
case 8: | ||
_a.sent(); | ||
this.hooks.bootstrapComplete.call(); | ||
this._modulesReady(); | ||
return [3 /*break*/, 10]; | ||
case 9: | ||
e_1 = _a.sent(); | ||
this.hooks.bootstrapError.call(e_1); | ||
if (bootstrapModule === null || bootstrapModule === void 0 ? void 0 : bootstrapModule.didCatch) { | ||
bootstrapModule === null || bootstrapModule === void 0 ? void 0 : bootstrapModule.didCatch(e_1); | ||
} | ||
else { | ||
throw e_1; | ||
} | ||
return [3 /*break*/, 10]; | ||
case 10: return [2 /*return*/, this]; | ||
} | ||
}); | ||
}); | ||
}; | ||
InjexContainer.prototype.registerModuleExports = function (moduleExports) { | ||
for (var _i = 0, _a = Object.getOwnPropertyNames(moduleExports); _i < _a.length; _i++) { | ||
var key = _a[_i]; | ||
} | ||
async bootstrap() { | ||
if (this._didBootstrapCalled) { | ||
throw new errors_1.BootstrapError('container bootstrap should run only once.'); | ||
} | ||
this._didBootstrapCalled = true; | ||
await this._initPlugins(); | ||
this.hooks.beforeRegistration.call(); | ||
await this.loadContainerFiles(); | ||
this.hooks.afterRegistration.call(); | ||
if (this.config.perfMode) { | ||
await this._createModulesAsync(); | ||
} | ||
else { | ||
this._createModules(); | ||
} | ||
const bootstrapModule = this.get(constants_1.bootstrapSymbol); | ||
try { | ||
await this._initializeModules(); | ||
this.hooks.bootstrapRun.call(); | ||
await bootstrapModule?.run(); | ||
this.hooks.bootstrapComplete.call(); | ||
this._modulesReady(); | ||
} | ||
catch (e) { | ||
this.hooks.bootstrapError.call(e); | ||
if (bootstrapModule?.didCatch) { | ||
bootstrapModule?.didCatch(e); | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
return this; | ||
} | ||
registerModuleExports(moduleExports) { | ||
for (const key of Object.getOwnPropertyNames(moduleExports)) { | ||
this._register(moduleExports[key]); | ||
} | ||
}; | ||
InjexContainer.prototype._initPlugins = function () { | ||
var plugins = this.config.plugins; | ||
} | ||
_initPlugins() { | ||
const { plugins } = this.config; | ||
if (!plugins || !plugins.length) { | ||
return; | ||
} | ||
var applyPluginPromises = []; | ||
for (var _i = 0, plugins_1 = plugins; _i < plugins_1.length; _i++) { | ||
var plugin = plugins_1[_i]; | ||
const applyPluginPromises = []; | ||
for (const plugin of plugins) { | ||
this._throwIfInvalidPlugin(plugin); | ||
@@ -106,19 +77,19 @@ applyPluginPromises.push((plugin.apply(this) || Promise.resolve())); | ||
return Promise.all(applyPluginPromises); | ||
}; | ||
InjexContainer.prototype._throwIfAlreadyDefined = function (name) { | ||
} | ||
_throwIfAlreadyDefined(name) { | ||
if (this._moduleRegistry.has(name)) { | ||
throw new errors_1.DuplicateDefinitionError(name); | ||
} | ||
}; | ||
InjexContainer.prototype._throwIfModuleExists = function (name) { | ||
} | ||
_throwIfModuleExists(name) { | ||
if (this._modules.has(name)) { | ||
throw new errors_1.DuplicateDefinitionError(name); | ||
} | ||
}; | ||
InjexContainer.prototype._throwIfInvalidPlugin = function (plugin) { | ||
} | ||
_throwIfInvalidPlugin(plugin) { | ||
if (!plugin.apply || !stdlib_1.isFunction(plugin.apply)) { | ||
throw new errors_1.InvalidPluginError(plugin); | ||
} | ||
}; | ||
InjexContainer.prototype._createHooks = function () { | ||
} | ||
_createHooks() { | ||
this.hooks = { | ||
@@ -135,40 +106,25 @@ beforeRegistration: new stdlib_1.Hook(), | ||
}; | ||
}; | ||
InjexContainer.prototype._createModulesAsync = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var modules, item; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this.hooks.beforeCreateModules.call(); | ||
modules = Array.from(this._moduleRegistry.values()); | ||
_a.label = 1; | ||
case 1: | ||
if (!modules.length) return [3 /*break*/, 3]; | ||
item = modules.shift(); | ||
this._createModule(item); | ||
return [4 /*yield*/, stdlib_1.yieldToMain()]; | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 1]; | ||
case 3: | ||
this.hooks.afterCreateModules.call(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
InjexContainer.prototype._createModules = function () { | ||
var _this = this; | ||
} | ||
async _createModulesAsync() { | ||
this.hooks.beforeCreateModules.call(); | ||
this._moduleRegistry.forEach(function (item) { return _this._createModule(item); }); | ||
const modules = Array.from(this._moduleRegistry.values()); | ||
while (modules.length) { | ||
const item = modules.shift(); | ||
this._createModule(item); | ||
await stdlib_1.yieldToMain(); | ||
} | ||
this.hooks.afterCreateModules.call(); | ||
}; | ||
InjexContainer.prototype._createModule = function (item) { | ||
var metadata = metadataHandlers_1.default.getMetadata(item.prototype); | ||
} | ||
_createModules() { | ||
this.hooks.beforeCreateModules.call(); | ||
this._moduleRegistry.forEach((item) => this._createModule(item)); | ||
this.hooks.afterCreateModules.call(); | ||
} | ||
_createModule(item) { | ||
const metadata = metadataHandlers_1.default.getMetadata(item.prototype); | ||
this._throwIfModuleExists(metadata.name); | ||
var module; | ||
let module; | ||
switch (true) { | ||
case metadata.lazy: | ||
var _a = this._createLazyModuleFactoryMethod(item, metadata), loaderFn = _a[0], loaderInstance = _a[1]; | ||
const [loaderFn, loaderInstance] = this._createLazyModuleFactoryMethod(item, metadata); | ||
metadata.lazyLoader = loaderInstance; | ||
@@ -183,9 +139,9 @@ module = loaderFn; | ||
} | ||
var moduleWithMetadata = { | ||
module: module, metadata: metadata | ||
const moduleWithMetadata = { | ||
module, metadata | ||
}; | ||
this._modules.set(metadata.name, moduleWithMetadata); | ||
if (metadata.aliases) { | ||
var alias = void 0; | ||
for (var i = 0, len = metadata.aliases.length; i < len; i++) { | ||
let alias; | ||
for (let i = 0, len = metadata.aliases.length; i < len; i++) { | ||
alias = metadata.aliases[i]; | ||
@@ -199,62 +155,44 @@ if (!this._aliases.has(alias)) { | ||
this.hooks.afterModuleCreation.call(moduleWithMetadata); | ||
}; | ||
InjexContainer.prototype._createLazyModuleFactoryMethod = function (construct, metadata) { | ||
var self = this; | ||
var loaderInstance = this._createInstance(construct, constants_1.EMPTY_ARGS); | ||
function loaderFn() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
_createLazyModuleFactoryMethod(construct, metadata) { | ||
const self = this; | ||
const loaderInstance = this._createInstance(construct, constants_1.EMPTY_ARGS); | ||
async function loaderFn(...args) { | ||
const Ctor = await loaderInstance.import.apply(loaderInstance, args); | ||
const lazyMetadata = metadataHandlers_1.default.getMetadata(Ctor.prototype); | ||
let lazyInstance; | ||
if (lazyMetadata && lazyMetadata.singleton && self.get(Ctor)) { | ||
lazyInstance = self.get(Ctor); | ||
} | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var Ctor, lazyMetadata, lazyInstance, moduleWithMetadata, alias, i, len; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, loaderInstance.import.apply(loaderInstance, args)]; | ||
case 1: | ||
Ctor = _a.sent(); | ||
lazyMetadata = metadataHandlers_1.default.getMetadata(Ctor.prototype); | ||
if (!(lazyMetadata && lazyMetadata.singleton && self.get(Ctor))) return [3 /*break*/, 2]; | ||
lazyInstance = self.get(Ctor); | ||
return [3 /*break*/, 4]; | ||
case 2: | ||
lazyInstance = self._createInstance(Ctor, args); | ||
self._injectModuleDependencies(lazyInstance); | ||
return [4 /*yield*/, self._invokeModuleInitMethod(lazyInstance, lazyMetadata)]; | ||
case 3: | ||
_a.sent(); | ||
self._lazyInvokeModuleReadyMethod(lazyInstance, lazyMetadata); | ||
moduleWithMetadata = { | ||
module: lazyInstance, metadata: lazyMetadata | ||
}; | ||
self._modules.set(lazyMetadata.name, moduleWithMetadata); | ||
if (lazyMetadata.aliases) { | ||
alias = void 0; | ||
for (i = 0, len = lazyMetadata.aliases.length; i < len; i++) { | ||
alias = lazyMetadata.aliases[i]; | ||
if (!self._aliases.has(alias)) { | ||
self._aliases.set(alias, []); | ||
} | ||
self._aliases.get(alias).push(moduleWithMetadata); | ||
} | ||
} | ||
_a.label = 4; | ||
case 4: return [2 /*return*/, lazyInstance]; | ||
else { | ||
lazyInstance = self._createInstance(Ctor, args); | ||
self._injectModuleDependencies(lazyInstance); | ||
await self._invokeModuleInitMethod(lazyInstance, lazyMetadata); | ||
self._lazyInvokeModuleReadyMethod(lazyInstance, lazyMetadata); | ||
const moduleWithMetadata = { | ||
module: lazyInstance, metadata: lazyMetadata | ||
}; | ||
self._modules.set(lazyMetadata.name, moduleWithMetadata); | ||
if (lazyMetadata.aliases) { | ||
let alias; | ||
for (let i = 0, len = lazyMetadata.aliases.length; i < len; i++) { | ||
alias = lazyMetadata.aliases[i]; | ||
if (!self._aliases.has(alias)) { | ||
self._aliases.set(alias, []); | ||
} | ||
self._aliases.get(alias).push(moduleWithMetadata); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
return lazyInstance; | ||
} | ||
return [loaderFn, loaderInstance]; | ||
}; | ||
InjexContainer.prototype._createModuleFactoryMethod = function (construct, metadata) { | ||
var self = this; | ||
return function factory() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var instance = self._createInstance(construct, args); | ||
} | ||
_createModuleFactoryMethod(construct, metadata) { | ||
const self = this; | ||
return function factory(...args) { | ||
const instance = self._createInstance(construct, args); | ||
self._injectModuleDependencies(instance); | ||
var initValue = self._invokeModuleInitMethod(instance, metadata); | ||
var _invokeReadyMethod = function () { | ||
const initValue = self._invokeModuleInitMethod(instance, metadata); | ||
const _invokeReadyMethod = () => { | ||
self._lazyInvokeModuleReadyMethod(instance, metadata); | ||
@@ -264,7 +202,7 @@ }; | ||
return initValue | ||
.then(function () { | ||
.then(() => { | ||
_invokeReadyMethod(); | ||
return instance; | ||
}) | ||
.catch(function (err) { return self._onInitModuleError(metadata, err); }); | ||
.catch((err) => self._onInitModuleError(metadata, err)); | ||
} | ||
@@ -274,55 +212,32 @@ _invokeReadyMethod(); | ||
}; | ||
}; | ||
InjexContainer.prototype._initializeModules = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, Promise.all(Array | ||
.from(this._modules.values()) | ||
.map(function (_a) { | ||
var module = _a.module, metadata = _a.metadata; | ||
if (metadata && metadata.singleton) { | ||
_this._injectModuleDependencies(metadata.lazyLoader || module); | ||
} | ||
return { module: module, metadata: metadata }; | ||
}) | ||
.map(function (_a) { | ||
var module = _a.module, metadata = _a.metadata; | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!(metadata && metadata.singleton)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, stdlib_1.yieldToMain()]; | ||
case 1: | ||
_b.sent(); | ||
return [2 /*return*/, this._invokeModuleInitMethod(metadata.lazyLoader || module, metadata)]; | ||
case 2: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}))]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
InjexContainer.prototype._modulesReady = function () { | ||
var _this = this; | ||
} | ||
async _initializeModules() { | ||
await Promise.all(Array | ||
.from(this._modules.values()) | ||
.map(({ module, metadata }) => { | ||
if (metadata && metadata.singleton) { | ||
this._injectModuleDependencies(metadata.lazyLoader || module); | ||
} | ||
return { module, metadata }; | ||
}) | ||
.map(async ({ module, metadata }) => { | ||
if (metadata && metadata.singleton) { | ||
await stdlib_1.yieldToMain(); | ||
return this._invokeModuleInitMethod(metadata.lazyLoader || module, metadata); | ||
} | ||
})); | ||
} | ||
_modulesReady() { | ||
Array | ||
.from(this._modules.values()) | ||
.map(function (_a) { | ||
var module = _a.module, metadata = _a.metadata; | ||
.map(({ module, metadata }) => { | ||
if (metadata && metadata.singleton) { | ||
_this._invokeModuleReadyMethod(metadata.lazyLoader || module, metadata); | ||
this._invokeModuleReadyMethod(metadata.lazyLoader || module, metadata); | ||
} | ||
}); | ||
}; | ||
InjexContainer.prototype._invokeMetadataModuleMethod = function (module, metadata, method, onError) { | ||
var chain = []; | ||
metadataHandlers_1.default.forEachProtoMetadata(module, function (_, meta) { | ||
if ((meta === null || meta === void 0 ? void 0 : meta[method]) && chain.indexOf(meta[method]) < 0) { | ||
} | ||
_invokeMetadataModuleMethod(module, metadata, method, onError) { | ||
const chain = []; | ||
metadataHandlers_1.default.forEachProtoMetadata(module, (_, meta) => { | ||
if (meta?.[method] && chain.indexOf(meta[method]) < 0) { | ||
chain.push(meta[method]); | ||
@@ -332,5 +247,5 @@ } | ||
try { | ||
var promises = []; | ||
for (var i = chain.length - 1; i >= 0; i--) { | ||
var res = module[chain[i]].call(module); | ||
const promises = []; | ||
for (let i = chain.length - 1; i >= 0; i--) { | ||
const res = module[chain[i]].call(module); | ||
if (stdlib_1.isPromise(res)) { | ||
@@ -341,4 +256,4 @@ promises.push(res); | ||
if (promises.length) { | ||
return Promise.all(promises).catch(function (e) { | ||
onError === null || onError === void 0 ? void 0 : onError(metadata, e); | ||
return Promise.all(promises).catch((e) => { | ||
onError?.(metadata, e); | ||
}); | ||
@@ -348,29 +263,27 @@ } | ||
catch (e) { | ||
onError === null || onError === void 0 ? void 0 : onError(metadata, e); | ||
onError?.(metadata, e); | ||
} | ||
}; | ||
InjexContainer.prototype._lazyInvokeModuleReadyMethod = function (module, metadata) { | ||
} | ||
_lazyInvokeModuleReadyMethod(module, metadata) { | ||
if (this.hooks.bootstrapComplete.calledOnce) { | ||
this._invokeModuleReadyMethod(metadata.lazyLoader || module, metadata); | ||
} | ||
}; | ||
InjexContainer.prototype._invokeModuleReadyMethod = function (module, metadata) { | ||
} | ||
_invokeModuleReadyMethod(module, metadata) { | ||
return this._invokeMetadataModuleMethod(module, metadata, 'readyMethod'); | ||
}; | ||
InjexContainer.prototype._invokeModuleInitMethod = function (module, metadata) { | ||
} | ||
_invokeModuleInitMethod(module, metadata) { | ||
return this._invokeMetadataModuleMethod(module, metadata, 'initMethod', this._onInitModuleError); | ||
}; | ||
InjexContainer.prototype._onInitModuleError = function (metadata, err) { | ||
var _a; | ||
} | ||
_onInitModuleError(metadata, err) { | ||
this._logger.error(err); | ||
var error = new errors_1.InitializeMuduleError(metadata.name); | ||
error.stack = (_a = err === null || err === void 0 ? void 0 : err.stack) !== null && _a !== void 0 ? _a : error.stack; | ||
const error = new errors_1.InitializeMuduleError(metadata.name); | ||
error.stack = err?.stack ?? error.stack; | ||
throw error; | ||
}; | ||
InjexContainer.prototype._createInstance = function (construct, args) { | ||
if (args === void 0) { args = []; } | ||
} | ||
_createInstance(construct, args = []) { | ||
this.hooks.beforeCreateInstance.call(construct, args); | ||
return new (construct.bind.apply(construct, tslib_1.__spreadArrays([void 0], args)))(); | ||
}; | ||
InjexContainer.prototype.getModuleDefinition = function (moduleNameOrType) { | ||
return new construct(...args); | ||
} | ||
getModuleDefinition(moduleNameOrType) { | ||
if (this._modules.has(moduleNameOrType)) { | ||
@@ -380,73 +293,51 @@ return this._modules.get(moduleNameOrType); | ||
else if (moduleNameOrType instanceof Function && metadataHandlers_1.default.hasMetadata(moduleNameOrType.prototype)) { | ||
var metadata = metadataHandlers_1.default.getMetadata(moduleNameOrType.prototype); | ||
const metadata = metadataHandlers_1.default.getMetadata(moduleNameOrType.prototype); | ||
return this._modules.get(metadata.name); | ||
} | ||
return null; | ||
}; | ||
InjexContainer.prototype._injectModuleDependencies = function (module) { | ||
var _this = this; | ||
var self = this; | ||
metadataHandlers_1.default.forEachProtoMetadata(module, function (_, meta) { | ||
var dependencies = meta.dependencies || []; | ||
var aliasDependencies = meta.aliasDependencies || []; | ||
var factoryDependencies = meta.factoryDependencies || []; | ||
var paramDependencies = meta.paramDependencies || []; | ||
var _loop_1 = function (label, value) { | ||
} | ||
_injectModuleDependencies(module) { | ||
const self = this; | ||
metadataHandlers_1.default.forEachProtoMetadata(module, (_, meta) => { | ||
const dependencies = meta.dependencies || []; | ||
const aliasDependencies = meta.aliasDependencies || []; | ||
const factoryDependencies = meta.factoryDependencies || []; | ||
const paramDependencies = meta.paramDependencies || []; | ||
for (const { label, value } of dependencies) { | ||
Object.defineProperty(module, label, { | ||
configurable: true, | ||
get: function () { return _this.get(value) || null; } | ||
get: () => this.get(value) || null | ||
}); | ||
}; | ||
for (var _i = 0, dependencies_1 = dependencies; _i < dependencies_1.length; _i++) { | ||
var _a = dependencies_1[_i], label = _a.label, value = _a.value; | ||
_loop_1(label, value); | ||
} | ||
var _loop_2 = function (label, alias, keyBy) { | ||
for (const { label, alias, keyBy } of aliasDependencies) { | ||
Object.defineProperty(module, label, { | ||
configurable: true, | ||
get: function () { return _this.getAlias(alias, keyBy); } | ||
get: () => this.getAlias(alias, keyBy) | ||
}); | ||
}; | ||
for (var _b = 0, aliasDependencies_1 = aliasDependencies; _b < aliasDependencies_1.length; _b++) { | ||
var _c = aliasDependencies_1[_b], label = _c.label, alias = _c.alias, keyBy = _c.keyBy; | ||
_loop_2(label, alias, keyBy); | ||
} | ||
var _loop_3 = function (label, value) { | ||
var factory = _this.get(value); | ||
for (const { label, value } of factoryDependencies) { | ||
const factory = this.get(value); | ||
if (!factory) { | ||
throw new errors_1.FactoryModuleNotExistsError(value); | ||
} | ||
var item = factory(); | ||
const item = factory(); | ||
Object.defineProperty(module, label, { | ||
configurable: true, | ||
get: function () { return stdlib_1.isPromise(item) ? item.then(function (instance) { return instance; }) : item; } | ||
get: () => stdlib_1.isPromise(item) ? item.then((instance) => instance) : item | ||
}); | ||
}; | ||
for (var _d = 0, factoryDependencies_1 = factoryDependencies; _d < factoryDependencies_1.length; _d++) { | ||
var _e = factoryDependencies_1[_d], label = _e.label, value = _e.value; | ||
_loop_3(label, value); | ||
} | ||
var _loop_4 = function (methodName, index, value) { | ||
var org = module[methodName]; | ||
module[methodName] = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
for (const { methodName, index, value } of paramDependencies) { | ||
const org = module[methodName]; | ||
module[methodName] = function (...args) { | ||
args[index] = self.get(value); | ||
return org.apply(module, args); | ||
}; | ||
}; | ||
for (var _f = 0, paramDependencies_1 = paramDependencies; _f < paramDependencies_1.length; _f++) { | ||
var _g = paramDependencies_1[_f], methodName = _g.methodName, index = _g.index, value = _g.value; | ||
_loop_4(methodName, index, value); | ||
} | ||
}); | ||
}; | ||
InjexContainer.prototype._register = function (item) { | ||
var _a; | ||
if ((_a = !(item === null || item === void 0 ? void 0 : item.prototype)) !== null && _a !== void 0 ? _a : !metadataHandlers_1.default.hasMetadata(item.prototype)) { | ||
} | ||
_register(item) { | ||
if (!item?.prototype ?? !metadataHandlers_1.default.hasMetadata(item.prototype)) { | ||
return; | ||
} | ||
var metadata = metadataHandlers_1.default.getMetadata(item.prototype); | ||
const metadata = metadataHandlers_1.default.getMetadata(item.prototype); | ||
if (!metadata || !metadata.name) { | ||
@@ -457,3 +348,3 @@ return; | ||
this._moduleRegistry.set(metadata.name, item); | ||
}; | ||
} | ||
/** | ||
@@ -464,4 +355,3 @@ * Manually add module with metadata | ||
*/ | ||
InjexContainer.prototype.addModule = function (item) { | ||
var _this = this; | ||
addModule(item) { | ||
if (!metadataHandlers_1.default.hasMetadata(item.prototype)) { | ||
@@ -476,4 +366,4 @@ this._logger.debug("You're trying to add module without any metadata."); | ||
this._createModule(item); | ||
var _a = this.getModuleDefinition(item), module = _a.module, metadata = _a.metadata; | ||
var optionalPromise = null; | ||
const { module, metadata } = this.getModuleDefinition(item); | ||
let optionalPromise = null; | ||
if (metadata && metadata.singleton) { | ||
@@ -483,7 +373,7 @@ this._injectModuleDependencies(metadata.lazyLoader || module); | ||
} | ||
return (stdlib_1.isPromise(optionalPromise) ? optionalPromise : Promise.resolve()).then(function () { | ||
_this._lazyInvokeModuleReadyMethod(metadata.lazyLoader || module, metadata); | ||
return _this; | ||
return (stdlib_1.isPromise(optionalPromise) ? optionalPromise : Promise.resolve()).then(() => { | ||
this._lazyInvokeModuleReadyMethod(metadata.lazyLoader || module, metadata); | ||
return this; | ||
}); | ||
}; | ||
} | ||
/** | ||
@@ -495,8 +385,8 @@ * Manually add object to the container as singleton | ||
*/ | ||
InjexContainer.prototype.addObject = function (obj, name) { | ||
addObject(obj, name) { | ||
this._throwIfModuleExists(name); | ||
var metadata = { singleton: true, item: obj, name: name }; | ||
this._modules.set(name, { module: obj, metadata: metadata }); | ||
const metadata = { singleton: true, item: obj, name }; | ||
this._modules.set(name, { module: obj, metadata }); | ||
return this; | ||
}; | ||
} | ||
/** | ||
@@ -507,13 +397,9 @@ * Remove object from container | ||
*/ | ||
InjexContainer.prototype.removeObject = function (name) { | ||
removeObject(name) { | ||
this._modules.delete(name); | ||
this._moduleRegistry.delete(name); | ||
return this; | ||
}; | ||
InjexContainer.prototype._getMany = function () { | ||
var itemNameOrType = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
itemNameOrType[_i] = arguments[_i]; | ||
} | ||
var results = []; | ||
} | ||
_getMany(...itemNameOrType) { | ||
const results = []; | ||
while (itemNameOrType.length) { | ||
@@ -523,12 +409,8 @@ results.push(this.get(itemNameOrType.shift())); | ||
return results; | ||
}; | ||
InjexContainer.prototype.get = function () { | ||
var itemNameOrType = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
itemNameOrType[_i] = arguments[_i]; | ||
} | ||
} | ||
get(...itemNameOrType) { | ||
if (itemNameOrType.length > 1) { | ||
return this._getMany.apply(this, itemNameOrType); | ||
return this._getMany(...itemNameOrType); | ||
} | ||
var definition = this.getModuleDefinition(itemNameOrType[0]); | ||
const definition = this.getModuleDefinition(itemNameOrType[0]); | ||
if (!definition) { | ||
@@ -538,9 +420,8 @@ return constants_1.UNDEFINED; | ||
return definition.module; | ||
}; | ||
InjexContainer.prototype.getAlias = function (alias, keyBy) { | ||
var aliasModules = this._aliases.get(alias) || []; | ||
var useSet = !keyBy; | ||
var mapOrSet = useSet ? [] : {}; | ||
for (var _i = 0, aliasModules_1 = aliasModules; _i < aliasModules_1.length; _i++) { | ||
var aliasModule = aliasModules_1[_i]; | ||
} | ||
getAlias(alias, keyBy) { | ||
const aliasModules = this._aliases.get(alias) || []; | ||
const useSet = !keyBy; | ||
const mapOrSet = useSet ? [] : {}; | ||
for (const aliasModule of aliasModules) { | ||
if (useSet) { | ||
@@ -550,3 +431,3 @@ mapOrSet.push(aliasModule.module); | ||
else { | ||
var keyValue = void 0; | ||
let keyValue; | ||
switch (true) { | ||
@@ -568,6 +449,5 @@ case aliasModule.metadata.lazy: | ||
return mapOrSet; | ||
}; | ||
return InjexContainer; | ||
}()); | ||
} | ||
} | ||
exports.default = InjexContainer; | ||
//# sourceMappingURL=injex.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var stdlib_1 = require("@injex/stdlib"); | ||
var metadataSymbol = Symbol("metadata"); | ||
const stdlib_1 = require("@injex/stdlib"); | ||
const metadataSymbol = Symbol("metadata"); | ||
exports.default = stdlib_1.createMetadataHandlers(metadataSymbol); | ||
//# sourceMappingURL=metadataHandlers.js.map |
{ | ||
"name": "@injex/core", | ||
"version": "3.5.1", | ||
"version": "4.0.0-alpha.0", | ||
"description": "Simple, Decorated, Pluggable dependency-injection framework for TypeScript apps", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@injex/stdlib": "^3.5.1" | ||
"@injex/stdlib": "^4.0.0-alpha.0" | ||
}, | ||
@@ -57,3 +57,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "32ef46a3a5f5447e1320598e4bc71e03903d0a00" | ||
"gitHead": "c573d14be7e2530d19be7ebd7a6d4a8faf45b507" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
87956
1531
1
+ Added@injex/stdlib@4.0.0-alpha.2(transitive)
- Removed@injex/stdlib@3.5.1(transitive)
- Removedreflect-metadata@0.1.14(transitive)
Updated@injex/stdlib@^4.0.0-alpha.0