@spinajs/di 2.0.180 → 2.0.181

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 (60) hide show
  1. package/lib/cjs/array.d.ts +10 -10
  2. package/lib/cjs/array.js +13 -13
  3. package/lib/cjs/binder.d.ts +39 -39
  4. package/lib/cjs/binder.js +96 -96
  5. package/lib/cjs/container-cache.d.ts +17 -17
  6. package/lib/cjs/container-cache.js +63 -63
  7. package/lib/cjs/container.d.ts +133 -133
  8. package/lib/cjs/container.js +466 -466
  9. package/lib/cjs/container.js.map +1 -1
  10. package/lib/cjs/decorators.d.ts +141 -141
  11. package/lib/cjs/decorators.js +303 -303
  12. package/lib/cjs/enums.d.ts +31 -31
  13. package/lib/cjs/enums.js +35 -35
  14. package/lib/cjs/enums.js.map +1 -1
  15. package/lib/cjs/exceptions.d.ts +17 -17
  16. package/lib/cjs/exceptions.js +25 -25
  17. package/lib/cjs/helpers.d.ts +35 -35
  18. package/lib/cjs/helpers.js +86 -86
  19. package/lib/cjs/index.d.ts +12 -12
  20. package/lib/cjs/index.js +41 -41
  21. package/lib/cjs/interfaces.d.ts +172 -172
  22. package/lib/cjs/interfaces.js +53 -53
  23. package/lib/cjs/registry.d.ts +14 -14
  24. package/lib/cjs/registry.js +80 -80
  25. package/lib/cjs/root.d.ts +108 -108
  26. package/lib/cjs/root.js +216 -217
  27. package/lib/cjs/root.js.map +1 -1
  28. package/lib/cjs/types.d.ts +13 -13
  29. package/lib/cjs/types.js +2 -2
  30. package/lib/mjs/array.d.ts +10 -10
  31. package/lib/mjs/array.js +9 -9
  32. package/lib/mjs/binder.d.ts +39 -39
  33. package/lib/mjs/binder.js +92 -92
  34. package/lib/mjs/container-cache.d.ts +17 -17
  35. package/lib/mjs/container-cache.js +59 -59
  36. package/lib/mjs/container.d.ts +133 -133
  37. package/lib/mjs/container.js +459 -459
  38. package/lib/mjs/container.js.map +1 -1
  39. package/lib/mjs/decorators.d.ts +141 -141
  40. package/lib/mjs/decorators.js +266 -266
  41. package/lib/mjs/enums.d.ts +31 -31
  42. package/lib/mjs/enums.js +32 -32
  43. package/lib/mjs/enums.js.map +1 -1
  44. package/lib/mjs/exceptions.d.ts +17 -17
  45. package/lib/mjs/exceptions.js +19 -19
  46. package/lib/mjs/helpers.d.ts +35 -35
  47. package/lib/mjs/helpers.js +72 -72
  48. package/lib/mjs/index.d.ts +12 -12
  49. package/lib/mjs/index.js +12 -12
  50. package/lib/mjs/interfaces.d.ts +172 -172
  51. package/lib/mjs/interfaces.js +45 -45
  52. package/lib/mjs/registry.d.ts +14 -14
  53. package/lib/mjs/registry.js +76 -76
  54. package/lib/mjs/root.d.ts +108 -108
  55. package/lib/mjs/root.js +159 -159
  56. package/lib/mjs/types.d.ts +13 -13
  57. package/lib/mjs/types.js +1 -1
  58. package/lib/tsconfig.cjs.tsbuildinfo +1 -1
  59. package/lib/tsconfig.mjs.tsbuildinfo +1 -1
  60. package/package.json +2 -2
@@ -1,173 +1,173 @@
1
- /// <reference types="node" />
2
- import { ResolveType } from './enums.js';
3
- import { Class, Factory } from './types.js';
4
- import { EventEmitter } from 'events';
5
- import { TypedArray } from './array.js';
6
- import { Registry } from './registry.js';
7
- import { ContainerCache } from './container-cache.js';
8
- export interface IInstanceCheck {
9
- __checkInstance__(creationOptions: any): boolean;
10
- }
11
- /**
12
- * Class info structure
13
- */
14
- export declare class ClassInfo<T> {
15
- /**
16
- * Full file path of loaded class
17
- */
18
- file: string;
19
- /**
20
- * Class name
21
- */
22
- name: string;
23
- /**
24
- * Javascript class object
25
- */
26
- type: any;
27
- /**
28
- * Resolved instance
29
- */
30
- instance?: T;
31
- }
32
- /**
33
- * Interface to describe DI binding behaviour
34
- */
35
- export interface IBind {
36
- /**
37
- * `as` binding (alias)
38
- *
39
- * @param type - base class that is being registered
40
- */
41
- as(type: Class<any> | string): this;
42
- /**
43
- * self bind, class should be resolved by its name. Its default behaviour.
44
- */
45
- asSelf(): this;
46
- /**
47
- * Registers as value, it wont be resolved or called, just stored as is in container
48
- * @param type - name of type / key after whitch implementation will be resolved
49
- * @param override - if true, any value registered before is overriden by new one
50
- */
51
- asValue(type: string, override: boolean): this;
52
- asValue(type: string): this;
53
- /**
54
- *
55
- * Add plain value to container, value is stored in hashmap for quick access
56
- * eg. we have multiple value converters and we wanc o(1) access, instead searching for
57
- * converter for specific type
58
- *
59
- * @param type - name of added value
60
- * @param key - hashmap key
61
- */
62
- asMapValue(type: string, hashKey: string): this;
63
- /**
64
- * Registers object as single instance ( singleton )
65
- */
66
- singleInstance(): this;
67
- }
68
- export interface ResolvableObject {
69
- [key: string]: any;
70
- }
71
- export interface IContainer extends EventEmitter {
72
- Cache: ContainerCache;
73
- Registry: Registry;
74
- Parent: IContainer;
75
- clear(): void;
76
- clearRegistry(): void;
77
- clearCache(): void;
78
- register<T>(implementation: Class<T> | Factory<T> | ResolvableObject): IBind;
79
- unregister<T>(implementation: string | Class<T> | Factory<T> | ResolvableObject): void;
80
- uncache<T>(source: string | Class<T> | TypedArray<T>, parent?: boolean): void;
81
- child(): IContainer;
82
- get<T>(service: TypedArray<T>, parent?: boolean): T[] | null;
83
- get<T>(service: string | Class<T>, parent?: boolean): T | null;
84
- get<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): T | T[] | null;
85
- getRegisteredTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
86
- isResolved<T>(service: string | Class<T>, parent?: boolean): boolean;
87
- hasRegistered<T>(service: Class<T> | string | TypedArray<T>, parent?: boolean): boolean;
88
- hasRegisteredType<T>(source: Class<any> | string | TypedArray<any>, type: Class<T> | string | TypedArray<T> | object, parent?: boolean): boolean;
89
- resolve<T>(type: string, options?: unknown[], check?: boolean): T;
90
- resolve<T>(type: string, check?: boolean): T;
91
- resolve<T>(type: Class<T> | Factory<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncService ? Promise<T> : T;
92
- resolve<T>(type: TypedArray<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncService ? Promise<T[]> : T[];
93
- resolve<T>(type: Class<T> | Factory<T>, check?: boolean): T extends AsyncService ? Promise<T> : T;
94
- resolve<T>(type: TypedArray<T>, check?: boolean): T extends AsyncService ? Promise<T[]> : T[];
95
- resolve<T>(type: Class<T> | TypedArray<T> | string, options?: unknown[] | boolean, check?: boolean): Promise<T | T[]> | T | T[];
96
- dispose(): Promise<void>;
97
- extractDescriptor(type: Class<unknown>): IInjectDescriptor<unknown>;
98
- }
99
- /**
100
- * Injection description definition structure
101
- */
102
- export interface IInjectDescriptor<T> {
103
- inject: IToInject<T>[];
104
- resolver: ResolveType;
105
- }
106
- export interface IToInject<T> {
107
- inject: Class<T> | TypedArray<T>;
108
- autoinject: boolean;
109
- autoinjectKey: string;
110
- /**
111
- * additional data passed to DI when resolving
112
- */
113
- data?: any;
114
- /**
115
- * Additional options passed to resolved options
116
- */
117
- options?: any;
118
- /**
119
- * Callback used to resolve service
120
- * name of service we want to resolve
121
- * eg. we have multiple \@injectable registered
122
- * and we want specific one
123
- *
124
- * It is specifically for use with configuration module
125
- * where services can be changed in configuration files
126
- * and allows to use @AutoinjectService() decorator
127
- */
128
- serviceFunc?: (data: string | any[], container: IContainer) => IServiceFuncResult | IServiceFuncResult[];
129
- mapFunc?: (x: IMappableService) => string;
130
- }
131
- export interface IMappableService {
132
- ServiceName: string;
133
- }
134
- export interface IServiceFuncResult {
135
- service: string;
136
- options?: any;
137
- }
138
- export interface IResolvedInjection {
139
- instance: unknown;
140
- autoinject: boolean;
141
- autoinjectKey: string;
142
- }
143
- /**
144
- * Interface to describe DI resolve strategies. Strategies are used do
145
- * do some work at object creation eg. initialize objects that inherits from same class
146
- * specific way but without need for factory function.
147
- *
148
- *
149
- * @see FrameworkModuleSyncService implementation
150
- */
151
- export declare class Service {
152
- protected resolved: boolean;
153
- get Resolved(): boolean;
154
- /**
155
- * Use to dispose service, relase all resources, stop timers etc.
156
- *
157
- */
158
- dispose(): Promise<void>;
159
- }
160
- export declare abstract class SyncService extends Service {
161
- resolve(): void;
162
- }
163
- export declare class AsyncService extends Service {
164
- resolve(): Promise<void>;
165
- }
166
- export declare abstract class Bootstrapper {
167
- abstract bootstrap(): Promise<void> | void;
168
- }
169
- export interface IAutoinjectOptions {
170
- mapFunc?: (x: IMappableService) => string;
171
- options?: any;
172
- }
1
+ /// <reference types="node" />
2
+ import { ResolveType } from './enums.js';
3
+ import { Class, Factory } from './types.js';
4
+ import { EventEmitter } from 'events';
5
+ import { TypedArray } from './array.js';
6
+ import { Registry } from './registry.js';
7
+ import { ContainerCache } from './container-cache.js';
8
+ export interface IInstanceCheck {
9
+ __checkInstance__(creationOptions: any): boolean;
10
+ }
11
+ /**
12
+ * Class info structure
13
+ */
14
+ export declare class ClassInfo<T> {
15
+ /**
16
+ * Full file path of loaded class
17
+ */
18
+ file: string;
19
+ /**
20
+ * Class name
21
+ */
22
+ name: string;
23
+ /**
24
+ * Javascript class object
25
+ */
26
+ type: any;
27
+ /**
28
+ * Resolved instance
29
+ */
30
+ instance?: T;
31
+ }
32
+ /**
33
+ * Interface to describe DI binding behaviour
34
+ */
35
+ export interface IBind {
36
+ /**
37
+ * `as` binding (alias)
38
+ *
39
+ * @param type - base class that is being registered
40
+ */
41
+ as(type: Class<any> | string): this;
42
+ /**
43
+ * self bind, class should be resolved by its name. Its default behaviour.
44
+ */
45
+ asSelf(): this;
46
+ /**
47
+ * Registers as value, it wont be resolved or called, just stored as is in container
48
+ * @param type - name of type / key after whitch implementation will be resolved
49
+ * @param override - if true, any value registered before is overriden by new one
50
+ */
51
+ asValue(type: string, override: boolean): this;
52
+ asValue(type: string): this;
53
+ /**
54
+ *
55
+ * Add plain value to container, value is stored in hashmap for quick access
56
+ * eg. we have multiple value converters and we wanc o(1) access, instead searching for
57
+ * converter for specific type
58
+ *
59
+ * @param type - name of added value
60
+ * @param key - hashmap key
61
+ */
62
+ asMapValue(type: string, hashKey: string): this;
63
+ /**
64
+ * Registers object as single instance ( singleton )
65
+ */
66
+ singleInstance(): this;
67
+ }
68
+ export interface ResolvableObject {
69
+ [key: string]: any;
70
+ }
71
+ export interface IContainer extends EventEmitter {
72
+ Cache: ContainerCache;
73
+ Registry: Registry;
74
+ Parent: IContainer;
75
+ clear(): void;
76
+ clearRegistry(): void;
77
+ clearCache(): void;
78
+ register<T>(implementation: Class<T> | Factory<T> | ResolvableObject): IBind;
79
+ unregister<T>(implementation: string | Class<T> | Factory<T> | ResolvableObject): void;
80
+ uncache<T>(source: string | Class<T> | TypedArray<T>, parent?: boolean): void;
81
+ child(): IContainer;
82
+ get<T>(service: TypedArray<T>, parent?: boolean): T[] | null;
83
+ get<T>(service: string | Class<T>, parent?: boolean): T | null;
84
+ get<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): T | T[] | null;
85
+ getRegisteredTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
86
+ isResolved<T>(service: string | Class<T>, parent?: boolean): boolean;
87
+ hasRegistered<T>(service: Class<T> | string | TypedArray<T>, parent?: boolean): boolean;
88
+ hasRegisteredType<T>(source: Class<any> | string | TypedArray<any>, type: Class<T> | string | TypedArray<T> | object, parent?: boolean): boolean;
89
+ resolve<T>(type: string, options?: unknown[], check?: boolean): T;
90
+ resolve<T>(type: string, check?: boolean): T;
91
+ resolve<T>(type: Class<T> | Factory<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncService ? Promise<T> : T;
92
+ resolve<T>(type: TypedArray<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncService ? Promise<T[]> : T[];
93
+ resolve<T>(type: Class<T> | Factory<T>, check?: boolean): T extends AsyncService ? Promise<T> : T;
94
+ resolve<T>(type: TypedArray<T>, check?: boolean): T extends AsyncService ? Promise<T[]> : T[];
95
+ resolve<T>(type: Class<T> | TypedArray<T> | string, options?: unknown[] | boolean, check?: boolean): Promise<T | T[]> | T | T[];
96
+ dispose(): Promise<void>;
97
+ extractDescriptor(type: Class<unknown>): IInjectDescriptor<unknown>;
98
+ }
99
+ /**
100
+ * Injection description definition structure
101
+ */
102
+ export interface IInjectDescriptor<T> {
103
+ inject: IToInject<T>[];
104
+ resolver: ResolveType;
105
+ }
106
+ export interface IToInject<T> {
107
+ inject: Class<T> | TypedArray<T>;
108
+ autoinject: boolean;
109
+ autoinjectKey: string;
110
+ /**
111
+ * additional data passed to DI when resolving
112
+ */
113
+ data?: any;
114
+ /**
115
+ * Additional options passed to resolved options
116
+ */
117
+ options?: any;
118
+ /**
119
+ * Callback used to resolve service
120
+ * name of service we want to resolve
121
+ * eg. we have multiple \@injectable registered
122
+ * and we want specific one
123
+ *
124
+ * It is specifically for use with configuration module
125
+ * where services can be changed in configuration files
126
+ * and allows to use @AutoinjectService() decorator
127
+ */
128
+ serviceFunc?: (data: string | any[], container: IContainer) => IServiceFuncResult | IServiceFuncResult[];
129
+ mapFunc?: (x: IMappableService) => string;
130
+ }
131
+ export interface IMappableService {
132
+ ServiceName: string;
133
+ }
134
+ export interface IServiceFuncResult {
135
+ service: string;
136
+ options?: any;
137
+ }
138
+ export interface IResolvedInjection {
139
+ instance: unknown;
140
+ autoinject: boolean;
141
+ autoinjectKey: string;
142
+ }
143
+ /**
144
+ * Interface to describe DI resolve strategies. Strategies are used do
145
+ * do some work at object creation eg. initialize objects that inherits from same class
146
+ * specific way but without need for factory function.
147
+ *
148
+ *
149
+ * @see FrameworkModuleSyncService implementation
150
+ */
151
+ export declare class Service {
152
+ protected resolved: boolean;
153
+ get Resolved(): boolean;
154
+ /**
155
+ * Use to dispose service, relase all resources, stop timers etc.
156
+ *
157
+ */
158
+ dispose(): Promise<void>;
159
+ }
160
+ export declare abstract class SyncService extends Service {
161
+ resolve(): void;
162
+ }
163
+ export declare class AsyncService extends Service {
164
+ resolve(): Promise<void>;
165
+ }
166
+ export declare abstract class Bootstrapper {
167
+ abstract bootstrap(): Promise<void> | void;
168
+ }
169
+ export interface IAutoinjectOptions {
170
+ mapFunc?: (x: IMappableService) => string;
171
+ options?: any;
172
+ }
173
173
  //# sourceMappingURL=interfaces.d.ts.map
@@ -1,54 +1,54 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Bootstrapper = exports.AsyncService = exports.SyncService = exports.Service = exports.ClassInfo = void 0;
4
- /**
5
- * Class info structure
6
- */
7
- class ClassInfo {
8
- }
9
- exports.ClassInfo = ClassInfo;
10
- /**
11
- * Interface to describe DI resolve strategies. Strategies are used do
12
- * do some work at object creation eg. initialize objects that inherits from same class
13
- * specific way but without need for factory function.
14
- *
15
- *
16
- * @see FrameworkModuleSyncService implementation
17
- */
18
- // export interface IStrategy {
19
- // resolve: (target: any, container: IContainer) => void;
20
- // }
21
- // export interface IAsyncStrategy {
22
- // resolveA: (target: any, container: IContainer) => Promise<void>;
23
- // }
24
- class Service {
25
- constructor() {
26
- this.resolved = false;
27
- }
28
- get Resolved() {
29
- return this.resolved;
30
- }
31
- /**
32
- * Use to dispose service, relase all resources, stop timers etc.
33
- *
34
- */
35
- async dispose() { }
36
- }
37
- exports.Service = Service;
38
- class SyncService extends Service {
39
- resolve() {
40
- this.resolved = true;
41
- }
42
- }
43
- exports.SyncService = SyncService;
44
- class AsyncService extends Service {
45
- /* eslint-disable */
46
- async resolve() {
47
- this.resolved = true;
48
- }
49
- }
50
- exports.AsyncService = AsyncService;
51
- class Bootstrapper {
52
- }
53
- exports.Bootstrapper = Bootstrapper;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Bootstrapper = exports.AsyncService = exports.SyncService = exports.Service = exports.ClassInfo = void 0;
4
+ /**
5
+ * Class info structure
6
+ */
7
+ class ClassInfo {
8
+ }
9
+ exports.ClassInfo = ClassInfo;
10
+ /**
11
+ * Interface to describe DI resolve strategies. Strategies are used do
12
+ * do some work at object creation eg. initialize objects that inherits from same class
13
+ * specific way but without need for factory function.
14
+ *
15
+ *
16
+ * @see FrameworkModuleSyncService implementation
17
+ */
18
+ // export interface IStrategy {
19
+ // resolve: (target: any, container: IContainer) => void;
20
+ // }
21
+ // export interface IAsyncStrategy {
22
+ // resolveA: (target: any, container: IContainer) => Promise<void>;
23
+ // }
24
+ class Service {
25
+ constructor() {
26
+ this.resolved = false;
27
+ }
28
+ get Resolved() {
29
+ return this.resolved;
30
+ }
31
+ /**
32
+ * Use to dispose service, relase all resources, stop timers etc.
33
+ *
34
+ */
35
+ async dispose() { }
36
+ }
37
+ exports.Service = Service;
38
+ class SyncService extends Service {
39
+ resolve() {
40
+ this.resolved = true;
41
+ }
42
+ }
43
+ exports.SyncService = SyncService;
44
+ class AsyncService extends Service {
45
+ /* eslint-disable */
46
+ async resolve() {
47
+ this.resolved = true;
48
+ }
49
+ }
50
+ exports.AsyncService = AsyncService;
51
+ class Bootstrapper {
52
+ }
53
+ exports.Bootstrapper = Bootstrapper;
54
54
  //# sourceMappingURL=interfaces.js.map
@@ -1,15 +1,15 @@
1
- import { TypedArray } from './array.js';
2
- import { IContainer } from './interfaces.js';
3
- import { Class, Factory } from './types.js';
4
- export declare class Registry {
5
- protected container: IContainer;
6
- protected registry: Map<string, any[]>;
7
- constructor(container: IContainer);
8
- clear(): void;
9
- unregister(type: any): void;
10
- register(name: string | Class<any> | TypedArray<any>, type: any): void;
11
- hasRegisteredType(source: Class<any> | string | TypedArray<any>, type: Class<any> | string | TypedArray<any> | object, parent?: boolean): boolean;
12
- getTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
13
- hasRegistered<T>(service: TypedArray<T> | Class<T> | string, parent?: boolean): boolean;
14
- }
1
+ import { TypedArray } from './array.js';
2
+ import { IContainer } from './interfaces.js';
3
+ import { Class, Factory } from './types.js';
4
+ export declare class Registry {
5
+ protected container: IContainer;
6
+ protected registry: Map<string, any[]>;
7
+ constructor(container: IContainer);
8
+ clear(): void;
9
+ unregister(type: any): void;
10
+ register(name: string | Class<any> | TypedArray<any>, type: any): void;
11
+ hasRegisteredType(source: Class<any> | string | TypedArray<any>, type: Class<any> | string | TypedArray<any> | object, parent?: boolean): boolean;
12
+ getTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
13
+ hasRegistered<T>(service: TypedArray<T> | Class<T> | string, parent?: boolean): boolean;
14
+ }
15
15
  //# sourceMappingURL=registry.d.ts.map
@@ -1,81 +1,81 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Registry = void 0;
4
- const exceptions_1 = require("@spinajs/exceptions");
5
- const helpers_js_1 = require("./helpers.js");
6
- class Registry {
7
- constructor(container) {
8
- this.container = container;
9
- this.registry = new Map();
10
- }
11
- clear() {
12
- this.registry.clear();
13
- }
14
- unregister(type) {
15
- const tname = (0, helpers_js_1.getTypeName)(type);
16
- this.registry.forEach((value) => {
17
- const index = value.findIndex((x) => (0, helpers_js_1.getTypeName)(x) === tname);
18
- if (index !== -1) {
19
- value.splice(index, 1);
20
- }
21
- });
22
- }
23
- register(name, type) {
24
- if (!(0, helpers_js_1.isConstructor)(type) && !(0, helpers_js_1.isFactory)(type) && !(0, helpers_js_1.isObject)(type)) {
25
- throw new exceptions_1.InvalidOperation('cannot register type if its not an class or factory function');
26
- }
27
- else {
28
- const tname = (0, helpers_js_1.getTypeName)(name);
29
- const value = this.registry.get(tname);
30
- if (value) {
31
- // factory functions, we always add to registry
32
- // its impossible to check for duplicates
33
- // all would return 'Function' type
34
- if ((0, helpers_js_1.isFactory)(type)) {
35
- value.push(type);
36
- }
37
- else if (!value.find((v) => (0, helpers_js_1.getTypeName)(v) === (0, helpers_js_1.getTypeName)(type))) {
38
- value.push(type);
39
- }
40
- }
41
- else {
42
- this.registry.set(tname, [type]);
43
- }
44
- }
45
- }
46
- hasRegisteredType(source, type, parent) {
47
- const sourceName = (0, helpers_js_1.getTypeName)(source);
48
- const targetName = (0, helpers_js_1.getTypeName)(type);
49
- if (this.registry.has(sourceName)) {
50
- return this.registry.get(sourceName).find((s) => s.name === targetName) !== undefined;
51
- }
52
- else if (parent && this.container.Parent) {
53
- return this.container.Parent.hasRegisteredType(source, type, parent);
54
- }
55
- return false;
56
- }
57
- getTypes(service, parent = true) {
58
- if (!service) {
59
- throw new exceptions_1.InvalidArgument('argument "service" cannot be null or empty');
60
- }
61
- const name = (0, helpers_js_1.getTypeName)(service);
62
- if (this.registry.has(name)) {
63
- return this.registry.get(name);
64
- }
65
- if (this.container.Parent && parent) {
66
- return this.container.Parent.getRegisteredTypes(service, parent);
67
- }
68
- return null;
69
- }
70
- hasRegistered(service, parent = true) {
71
- if (this.registry.has((0, helpers_js_1.getTypeName)(service))) {
72
- return true;
73
- }
74
- else if (parent && this.container.Parent) {
75
- return this.container.Parent.hasRegistered(service, parent);
76
- }
77
- return false;
78
- }
79
- }
80
- exports.Registry = Registry;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Registry = void 0;
4
+ const exceptions_1 = require("@spinajs/exceptions");
5
+ const helpers_js_1 = require("./helpers.js");
6
+ class Registry {
7
+ constructor(container) {
8
+ this.container = container;
9
+ this.registry = new Map();
10
+ }
11
+ clear() {
12
+ this.registry.clear();
13
+ }
14
+ unregister(type) {
15
+ const tname = (0, helpers_js_1.getTypeName)(type);
16
+ this.registry.forEach((value) => {
17
+ const index = value.findIndex((x) => (0, helpers_js_1.getTypeName)(x) === tname);
18
+ if (index !== -1) {
19
+ value.splice(index, 1);
20
+ }
21
+ });
22
+ }
23
+ register(name, type) {
24
+ if (!(0, helpers_js_1.isConstructor)(type) && !(0, helpers_js_1.isFactory)(type) && !(0, helpers_js_1.isObject)(type)) {
25
+ throw new exceptions_1.InvalidOperation('cannot register type if its not an class or factory function');
26
+ }
27
+ else {
28
+ const tname = (0, helpers_js_1.getTypeName)(name);
29
+ const value = this.registry.get(tname);
30
+ if (value) {
31
+ // factory functions, we always add to registry
32
+ // its impossible to check for duplicates
33
+ // all would return 'Function' type
34
+ if ((0, helpers_js_1.isFactory)(type)) {
35
+ value.push(type);
36
+ }
37
+ else if (!value.find((v) => (0, helpers_js_1.getTypeName)(v) === (0, helpers_js_1.getTypeName)(type))) {
38
+ value.push(type);
39
+ }
40
+ }
41
+ else {
42
+ this.registry.set(tname, [type]);
43
+ }
44
+ }
45
+ }
46
+ hasRegisteredType(source, type, parent) {
47
+ const sourceName = (0, helpers_js_1.getTypeName)(source);
48
+ const targetName = (0, helpers_js_1.getTypeName)(type);
49
+ if (this.registry.has(sourceName)) {
50
+ return this.registry.get(sourceName).find((s) => s.name === targetName) !== undefined;
51
+ }
52
+ else if (parent && this.container.Parent) {
53
+ return this.container.Parent.hasRegisteredType(source, type, parent);
54
+ }
55
+ return false;
56
+ }
57
+ getTypes(service, parent = true) {
58
+ if (!service) {
59
+ throw new exceptions_1.InvalidArgument('argument "service" cannot be null or empty');
60
+ }
61
+ const name = (0, helpers_js_1.getTypeName)(service);
62
+ if (this.registry.has(name)) {
63
+ return this.registry.get(name);
64
+ }
65
+ if (this.container.Parent && parent) {
66
+ return this.container.Parent.getRegisteredTypes(service, parent);
67
+ }
68
+ return null;
69
+ }
70
+ hasRegistered(service, parent = true) {
71
+ if (this.registry.has((0, helpers_js_1.getTypeName)(service))) {
72
+ return true;
73
+ }
74
+ else if (parent && this.container.Parent) {
75
+ return this.container.Parent.hasRegistered(service, parent);
76
+ }
77
+ return false;
78
+ }
79
+ }
80
+ exports.Registry = Registry;
81
81
  //# sourceMappingURL=registry.js.map