1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Error_1 = require("../Error");
|
4 | const InjectedServiceFactory_1 = require("./InjectedServiceFactory");
|
5 | const decorators_1 = require("./decorators");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | class ServiceManager {
|
12 | constructor(config) {
|
13 | this.services = new Map();
|
14 | this.factories = new Map();
|
15 | this.aliases = {};
|
16 | this.shared = new Map();
|
17 | this.sharedByDefault = true;
|
18 | this.creationContext = this;
|
19 | if (config) {
|
20 | this.configure(config);
|
21 | }
|
22 | }
|
23 | get(Service, forceTransient = false) {
|
24 | const resolvedName = this.resolveName(Service);
|
25 | if (!this.has(resolvedName)) {
|
26 | throw new Error_1.NotFoundError(`Unable to locate service "${typeof Service === 'string' ? Service : Service.name}".`);
|
27 | }
|
28 | if (forceTransient || !this.services.has(resolvedName)) {
|
29 | const service = this.factories.get(resolvedName)(this.creationContext);
|
30 | const shared = this.shared.has(resolvedName) ? this.shared.get(resolvedName) : this.sharedByDefault;
|
31 |
|
32 | decorators_1.applyPatches(this.creationContext, service);
|
33 |
|
34 | if (forceTransient || !shared) {
|
35 | return service;
|
36 | }
|
37 | this.services.set(resolvedName, service);
|
38 | }
|
39 | return this.services.get(resolvedName);
|
40 | }
|
41 | has(Service) {
|
42 | const resolvedName = this.resolveName(Service);
|
43 | return this.services.has(resolvedName) || this.factories.has(resolvedName);
|
44 | }
|
45 | registerFactory(key, value) {
|
46 | this.factories.set(key, value);
|
47 | return this;
|
48 | }
|
49 | registerFactories(factories) {
|
50 | factories.forEach((value, key) => {
|
51 | this.registerFactory(key, value);
|
52 | });
|
53 | return this;
|
54 | }
|
55 | registerService(key, service) {
|
56 | this.services.set(key, service);
|
57 | return this;
|
58 | }
|
59 | configure(config) {
|
60 | if (typeof config.sharedByDefault === 'boolean') {
|
61 | this.sharedByDefault = config.sharedByDefault;
|
62 | }
|
63 | if (config.shared instanceof Map) {
|
64 | config.shared.forEach((value, key) => this.shared.set(key, value));
|
65 | }
|
66 | if (config.services instanceof Map) {
|
67 | config.services.forEach((value, key) => this.services.set(key, value));
|
68 | }
|
69 | if (config.invokables instanceof Map) {
|
70 | config.invokables.forEach((value, key) => {
|
71 | this.registerInvokable(key, value);
|
72 | });
|
73 | }
|
74 | if (config.factories instanceof Map) {
|
75 | this.registerFactories(config.factories);
|
76 | }
|
77 | if (config.aliases) {
|
78 | Object.assign(this.aliases, config.aliases);
|
79 | }
|
80 | return this;
|
81 | }
|
82 | registerInvokable(key, value) {
|
83 | this.factories.set(key, InjectedServiceFactory_1.InjectedServiceFactory(value));
|
84 | }
|
85 | registerAliases(aliases) {
|
86 | Object.assign(this.aliases, aliases);
|
87 | return this;
|
88 | }
|
89 | registerAlias(alias, to) {
|
90 | this.aliases[alias] = to;
|
91 | return this;
|
92 | }
|
93 | resolveName(name) {
|
94 | if (typeof name !== 'string') {
|
95 | return name;
|
96 | }
|
97 | return this.aliases[name] || name;
|
98 | }
|
99 | }
|
100 | exports.ServiceManager = ServiceManager;
|
101 |
|
\ | No newline at end of file |