smartdi 0.1.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.
Files changed (3) hide show
  1. package/index.d.ts +6 -20
  2. package/index.js +20 -14
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,33 +1,19 @@
1
1
  export type Class<T> = {
2
2
  new (...args: any[]): T;
3
3
  };
4
- declare const $registry: unique symbol;
4
+ export type AbstractClass<T> = abstract new (...args: any[]) => T;
5
5
  export interface InjectableOptions {
6
6
  name?: string;
7
7
  args?: any[];
8
8
  multiple?: boolean;
9
9
  }
10
- interface RegistryItem {
11
- name: string;
12
- class: Class<any>;
13
- args: any[];
14
- singleton: boolean;
15
- instance: any;
16
- }
17
10
  export declare abstract class SmartDI {
18
- static readonly [$registry]: {
19
- [key: string]: RegistryItem;
20
- };
21
- static get<T>(clue: Class<T>, ...args: any[]): T;
11
+ static register<T>(clazz: Class<T>, options?: InjectableOptions): void;
12
+ static get<T>(clue: Class<T> | AbstractClass<T>, ...args: any[]): T;
22
13
  static get<T>(name: string, ...args: any[]): T;
23
- private static internalGet;
24
- private static internalGetArray;
25
- private static obtainInstance;
26
- private static resovleByConstructor;
27
- private static resovleOneByConstructor;
14
+ static getArray<T>(clue: Class<T> | AbstractClass<T>, ...args: any[]): T[];
28
15
  }
29
16
  export declare function Injectable(options?: InjectableOptions): ClassDecorator;
30
- export declare function Inject(constructor: Class<any>, ...args: any[]): PropertyDecorator;
17
+ export declare function Inject(constructor: Class<any> | AbstractClass<any>, ...args: any[]): PropertyDecorator;
31
18
  export declare function Inject(name: string, ...args: any[]): PropertyDecorator;
32
- export declare function InjectArray(constructor: Class<any>, ...args: any[]): PropertyDecorator;
33
- export {};
19
+ export declare function InjectArray(constructor: Class<any> | AbstractClass<any>, ...args: any[]): PropertyDecorator;
package/index.js CHANGED
@@ -4,9 +4,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.InjectArray = exports.Inject = exports.Injectable = exports.SmartDI = void 0;
5
5
  const $registry = Symbol('registry');
6
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
+ }
7
21
  static get(clue, ...args) {
8
22
  return SmartDI.internalGet(clue, new Set(), ...args);
9
23
  }
24
+ static getArray(clue, ...args) {
25
+ return SmartDI.internalGetArray(clue, new Set(), ...args);
26
+ }
10
27
  static internalGet(clue, creatings, ...args) {
11
28
  let constructor, name;
12
29
  if (typeof clue === 'function') {
@@ -57,10 +74,10 @@ class SmartDI {
57
74
  let matched = [];
58
75
  function isSubclass(sub, sup) {
59
76
  let proto = Object.getPrototypeOf(sub);
60
- while (proto.name) {
77
+ while (proto) {
61
78
  if (proto === sup)
62
79
  return true;
63
- proto = Object.getPrototypeOf(sub);
80
+ proto = Object.getPrototypeOf(proto);
64
81
  }
65
82
  return false;
66
83
  }
@@ -83,18 +100,7 @@ _a = $registry;
83
100
  SmartDI[_a] = {};
84
101
  function Injectable(options) {
85
102
  return (target) => {
86
- const name = (options === null || options === void 0 ? void 0 : options.name) || target.name;
87
- const args = (options === null || options === void 0 ? void 0 : options.args) || [];
88
- const singleton = !(options === null || options === void 0 ? void 0 : options.multiple);
89
- if (SmartDI[$registry][name])
90
- throw new Error(`Duplicated injectable name '${name}'!`);
91
- SmartDI[$registry][name] = {
92
- name,
93
- class: target,
94
- args: args,
95
- singleton,
96
- instance: undefined
97
- };
103
+ SmartDI.register(target, options);
98
104
  };
99
105
  }
100
106
  exports.Injectable = Injectable;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smartdi",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "A simple dependency injector based on decorators.",
5
5
  "main": "index.js",
6
6
  "scripts": {