@artus/injection
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -6,1 +6,2 @@ export declare const CLASS_CONSTRUCTOR: unique symbol; | ||
export declare const EXECUTION_CONTEXT_KEY = "ctx"; | ||
export declare const CLASS_TAG: unique symbol; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EXECUTION_CONTEXT_KEY = exports.CLASS_ASYNC_INIT_METHOD = exports.CLASS_CONSTRUCTOR_ARGS = exports.CLASS_PROPERTY = exports.CLASS_CONSTRUCTOR = void 0; | ||
exports.CLASS_TAG = exports.EXECUTION_CONTEXT_KEY = exports.CLASS_ASYNC_INIT_METHOD = exports.CLASS_CONSTRUCTOR_ARGS = exports.CLASS_PROPERTY = exports.CLASS_CONSTRUCTOR = void 0; | ||
exports.CLASS_CONSTRUCTOR = Symbol.for('injection:constructor'); | ||
@@ -9,1 +9,2 @@ exports.CLASS_PROPERTY = Symbol.for('injection:property'); | ||
exports.EXECUTION_CONTEXT_KEY = 'ctx'; | ||
exports.CLASS_TAG = Symbol.for('injection:class_tag'); |
@@ -1,4 +0,5 @@ | ||
import { ContainerType, Identifier, InjectableMetadata, InjectableDefinition } from "./types"; | ||
import { ContainerType, Identifier, InjectableMetadata, InjectableDefinition } from './types'; | ||
export default class Container implements ContainerType { | ||
private registry; | ||
private tags; | ||
protected name: string; | ||
@@ -10,2 +11,4 @@ constructor(name: string); | ||
getDefinition<T = unknown>(id: Identifier<T>): InjectableMetadata<T> | undefined; | ||
getInjectableByTag(tag: string): any[]; | ||
getByTag(tag: string): unknown[]; | ||
protected getValue(md: InjectableMetadata): any; | ||
@@ -15,2 +18,3 @@ protected getMetadata(id: Identifier): InjectableMetadata | undefined; | ||
private handleProps; | ||
private handleTag; | ||
} |
@@ -7,3 +7,3 @@ "use strict"; | ||
const errors_1 = require("./errors"); | ||
const mapType = Symbol['map_type']; | ||
const mapType = Symbol('map_type'); | ||
class Container { | ||
@@ -13,2 +13,3 @@ constructor(name) { | ||
this.registry = new Map(); | ||
this.tags = new Map(); | ||
} | ||
@@ -70,2 +71,3 @@ get(id) { | ||
} | ||
this.handleTag(type); | ||
return this; | ||
@@ -76,2 +78,10 @@ } | ||
} | ||
getInjectableByTag(tag) { | ||
const result = this.tags.get(tag); | ||
return result ? [...result] : []; | ||
} | ||
getByTag(tag) { | ||
const clazzes = this.getInjectableByTag(tag); | ||
return clazzes.map(clazz => this.get(clazz)); | ||
} | ||
getValue(md) { | ||
@@ -115,3 +125,18 @@ var _a; | ||
} | ||
handleTag(target) { | ||
let tags = Reflect.getOwnMetadata(constant_1.CLASS_TAG, target); | ||
if (!tags) { | ||
return; | ||
} | ||
if (!Array.isArray(tags)) { | ||
tags = [tags]; | ||
} | ||
tags.forEach(tag => { | ||
if (!this.tags.has(tag)) { | ||
this.tags.set(tag, new Set()); | ||
} | ||
this.tags.get(tag).add(target); | ||
}); | ||
} | ||
} | ||
exports.default = Container; |
@@ -25,2 +25,3 @@ import { ReflectMetadataType } from './types'; | ||
export declare function getDesignTypeMetadata(clazz: any, property: string | symbol): any; | ||
export declare function addTag(tag: string, target: any): void; | ||
export declare function isClass(clazz: any): boolean; | ||
@@ -27,0 +28,0 @@ export declare function isNumber(value: any): boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isPrimitiveFunction = exports.isObject = exports.isUndefined = exports.isNumber = exports.isClass = exports.getDesignTypeMetadata = exports.getParamMetadata = exports.recursiveGetMetadata = exports.setMetadata = exports.getMetadata = void 0; | ||
exports.isPrimitiveFunction = exports.isObject = exports.isUndefined = exports.isNumber = exports.isClass = exports.addTag = exports.getDesignTypeMetadata = exports.getParamMetadata = exports.recursiveGetMetadata = exports.setMetadata = exports.getMetadata = void 0; | ||
const constant_1 = require("./constant"); | ||
const functionPrototype = Object.getPrototypeOf(Function); | ||
@@ -64,2 +65,11 @@ function getMetadata(metadataKey, target, propertyKey) { | ||
exports.getDesignTypeMetadata = getDesignTypeMetadata; | ||
function addTag(tag, target) { | ||
let tags = Reflect.getOwnMetadata(constant_1.CLASS_TAG, target); | ||
if (!tags) { | ||
tags = []; | ||
Reflect.defineMetadata(constant_1.CLASS_TAG, tags, target); | ||
} | ||
tags.push(tag); | ||
} | ||
exports.addTag = addTag; | ||
function isClass(clazz) { | ||
@@ -66,0 +76,0 @@ if (typeof clazz !== 'function') { |
{ | ||
"name": "@artus/injection", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "A IoC Implemention for Artus.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
25848
619