UNPKG

@micro.ts/core

Version:

Microservice framework with Typescript

63 lines (62 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.printContainer = exports.Container = exports.BaseContainer = void 0; require("reflect-metadata"); const ContainerModule_1 = require("./ContainerModule"); const DiRegistry_1 = require("./DiRegistry"); const DiOptionsTypes_1 = require("./types/DiOptionsTypes"); class BaseContainer { constructor(registry = DiRegistry_1.ContainerRegistry) { this.registry = registry; this.root = new ContainerModule_1.ContainerModule(this.registry); } /** * Bind a Class constructor to the container * @param type * @param options */ registerService(type, options) { this.registry.bind(type, options); } newModule() { return new ContainerModule_1.ContainerModule(this.registry, this.root); } /** * Bind a resolver function to a specific key * @param key * @param resolver * @param scope */ bindResolver(key, resolver, scope = DiOptionsTypes_1.ServiceScope.Transient) { this.registry.bindResolver(key, resolver, scope); } /** * Check if a resolver with the given key is registered in the registry * @param key */ hasResolver(key) { return this.registry.hasResolver(key); } /** * Explicitly set a value to the container in global scope * @param key * @param value */ set(key, value) { this.root.set(key, value); } /** * Get the instance from the container, * if it does not exist, create a new instance * @param key Key or Class constructor of the instance */ get(key) { return this.root.get(key); } } exports.BaseContainer = BaseContainer; exports.Container = new BaseContainer(); function printContainer() { console.dir(exports.Container, { depth: null }); } exports.printContainer = printContainer;