smartdi 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -17
- package/index.js +22 -16
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -2,33 +2,18 @@ export type Class<T> = {
|
|
|
2
2
|
new (...args: any[]): T;
|
|
3
3
|
};
|
|
4
4
|
export type AbstractClass<T> = abstract new (...args: any[]) => T;
|
|
5
|
-
declare const $registry: unique symbol;
|
|
6
5
|
export interface InjectableOptions {
|
|
7
6
|
name?: string;
|
|
8
7
|
args?: any[];
|
|
9
8
|
multiple?: boolean;
|
|
10
9
|
}
|
|
11
|
-
interface RegistryItem {
|
|
12
|
-
name: string;
|
|
13
|
-
class: Class<any>;
|
|
14
|
-
args: any[];
|
|
15
|
-
singleton: boolean;
|
|
16
|
-
instance: any;
|
|
17
|
-
}
|
|
18
10
|
export declare abstract class SmartDI {
|
|
19
|
-
static
|
|
20
|
-
[key: string]: RegistryItem;
|
|
21
|
-
};
|
|
11
|
+
static register<T>(clazz: Class<T>, options?: InjectableOptions): void;
|
|
22
12
|
static get<T>(clue: Class<T> | AbstractClass<T>, ...args: any[]): T;
|
|
23
13
|
static get<T>(name: string, ...args: any[]): T;
|
|
24
|
-
|
|
25
|
-
private static internalGetArray;
|
|
26
|
-
private static obtainInstance;
|
|
27
|
-
private static resovleByConstructor;
|
|
28
|
-
private static resovleOneByConstructor;
|
|
14
|
+
static getArray<T>(clue: Class<T> | AbstractClass<T>, ...args: any[]): T[];
|
|
29
15
|
}
|
|
30
16
|
export declare function Injectable(options?: InjectableOptions): ClassDecorator;
|
|
31
17
|
export declare function Inject(constructor: Class<any> | AbstractClass<any>, ...args: any[]): PropertyDecorator;
|
|
32
18
|
export declare function Inject(name: string, ...args: any[]): PropertyDecorator;
|
|
33
19
|
export declare function InjectArray(constructor: Class<any> | AbstractClass<any>, ...args: any[]): PropertyDecorator;
|
|
34
|
-
export {};
|
package/index.js
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.SmartDI = void 0;
|
|
5
|
-
exports.Injectable = Injectable;
|
|
6
|
-
exports.Inject = Inject;
|
|
7
|
-
exports.InjectArray = InjectArray;
|
|
4
|
+
exports.InjectArray = exports.Inject = exports.Injectable = exports.SmartDI = void 0;
|
|
8
5
|
const $registry = Symbol('registry');
|
|
9
6
|
class SmartDI {
|
|
7
|
+
static register(clazz, options) {
|
|
8
|
+
const name = (options === null || options === void 0 ? void 0 : options.name) || clazz.name;
|
|
9
|
+
const args = (options === null || options === void 0 ? void 0 : options.args) || [];
|
|
10
|
+
const singleton = !(options === null || options === void 0 ? void 0 : options.multiple);
|
|
11
|
+
if (SmartDI[$registry][name])
|
|
12
|
+
throw new Error(`Duplicated injectable name '${name}'!`);
|
|
13
|
+
SmartDI[$registry][name] = {
|
|
14
|
+
name,
|
|
15
|
+
class: clazz,
|
|
16
|
+
args: args,
|
|
17
|
+
singleton,
|
|
18
|
+
instance: undefined
|
|
19
|
+
};
|
|
20
|
+
}
|
|
10
21
|
static get(clue, ...args) {
|
|
11
22
|
return SmartDI.internalGet(clue, new Set(), ...args);
|
|
12
23
|
}
|
|
24
|
+
static getArray(clue, ...args) {
|
|
25
|
+
return SmartDI.internalGetArray(clue, new Set(), ...args);
|
|
26
|
+
}
|
|
13
27
|
static internalGet(clue, creatings, ...args) {
|
|
14
28
|
let constructor, name;
|
|
15
29
|
if (typeof clue === 'function') {
|
|
@@ -86,20 +100,10 @@ _a = $registry;
|
|
|
86
100
|
SmartDI[_a] = {};
|
|
87
101
|
function Injectable(options) {
|
|
88
102
|
return (target) => {
|
|
89
|
-
|
|
90
|
-
const args = (options === null || options === void 0 ? void 0 : options.args) || [];
|
|
91
|
-
const singleton = !(options === null || options === void 0 ? void 0 : options.multiple);
|
|
92
|
-
if (SmartDI[$registry][name])
|
|
93
|
-
throw new Error(`Duplicated injectable name '${name}'!`);
|
|
94
|
-
SmartDI[$registry][name] = {
|
|
95
|
-
name,
|
|
96
|
-
class: target,
|
|
97
|
-
args: args,
|
|
98
|
-
singleton,
|
|
99
|
-
instance: undefined
|
|
100
|
-
};
|
|
103
|
+
SmartDI.register(target, options);
|
|
101
104
|
};
|
|
102
105
|
}
|
|
106
|
+
exports.Injectable = Injectable;
|
|
103
107
|
function Inject(clue, ...args) {
|
|
104
108
|
return function (target, prop) {
|
|
105
109
|
let injects = Reflect.getMetadata('smartdi:injects', target) || {};
|
|
@@ -107,6 +111,7 @@ function Inject(clue, ...args) {
|
|
|
107
111
|
Reflect.defineMetadata('smartdi:injects', injects, target);
|
|
108
112
|
};
|
|
109
113
|
}
|
|
114
|
+
exports.Inject = Inject;
|
|
110
115
|
function InjectArray(clue, ...args) {
|
|
111
116
|
return function (target, prop) {
|
|
112
117
|
let injects = Reflect.getMetadata('smartdi:injects', target) || {};
|
|
@@ -114,3 +119,4 @@ function InjectArray(clue, ...args) {
|
|
|
114
119
|
Reflect.defineMetadata('smartdi:injects', injects, target);
|
|
115
120
|
};
|
|
116
121
|
}
|
|
122
|
+
exports.InjectArray = InjectArray;
|