-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice_collection.ts
176 lines (158 loc) · 4.43 KB
/
service_collection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { isClassTagged } from "./metadata.ts";
import { resolve } from "./resolution.ts";
import {
DynamicValue,
Kind,
Lifetime,
Newable,
Service,
ServiceIdent,
ServiceStore,
StaticValue,
} from "./service.ts";
export interface IServiceCollection {
/**
* Gets a service from the collection using the rest of the services in the
* collection to resolve the dependencies.
*/
get<T>(ident: ServiceIdent<T>): T;
}
/**
* A collection of services.
*/
export class ServiceCollection implements IServiceCollection {
private _services: ServiceStore = new Map();
/**
* Gets a service from the collection using the rest of the services in the
* collection to resolve the dependencies.
*/
public get<T>(ident: ServiceIdent<T>): T {
return resolve(ident, [this._services]);
}
/**
* Adds a class to the collection using itself as the identifier.
* The service will be added with a transient lifetime.
*/
public addTransient<T>(impl: Newable<T>): void;
/**
* Adds a class to the collection using a custom identifier.
* The service will be added with a transient lifetime.
*/
public addTransient<T>(ident: ServiceIdent<T>, impl: Newable<T>): void;
/**
* @ignore
*/
public addTransient<T>(ident: ServiceIdent<T>, impl?: Newable<T>) {
this._addNewable(ident, newableImpl(ident, impl), Lifetime.Transient);
}
/**
* Adds a class to the collection using itself as the identifier.
* The service will be added with a scoped lifetime.
*/
public addScoped<T>(impl: Newable<T>): void;
/**
* Adds a class to the collection using a custom identifier.
* The service will be added with a scoped lifetime.
*/
public addScoped<T>(ident: ServiceIdent<T>, impl: Newable<T>): void;
/**
* @ignore
*/
public addScoped<T>(ident: ServiceIdent<T>, impl?: Newable<T>) {
this._addNewable(ident, newableImpl(ident, impl), Lifetime.Scoped);
}
/**
* Adds a class to the collection using itself as the identifier.
* The service will be added with a singleton lifetime.
*/
public addSingleton<T>(impl: Newable<T>): void;
/**
* Adds a class to the collection using a custom identifier.
* The service will be added with a singleton lifetime.
*/
public addSingleton<T>(ident: ServiceIdent<T>, impl: Newable<T>): void;
/**
* @ignore
*/
public addSingleton<T>(ident: ServiceIdent<T>, impl?: Newable<T>) {
this._addNewable(ident, newableImpl(ident, impl), Lifetime.Singleton);
}
/**
* Adds a function to the collection using a custom identifier.
* The service will be added with a transient lifetime.
*/
public addTransientDynamic<T>(ident: ServiceIdent<T>, fn: DynamicValue) {
this._addDynamic(ident, fn, Lifetime.Transient);
}
/**
* Adds a function to the collection using a custom identifier.
* The service will be added with a scoped lifetime.
*/
public addScopedDynamic<T>(ident: ServiceIdent<T>, fn: DynamicValue) {
this._addDynamic(ident, fn, Lifetime.Scoped);
}
/**
* Adds a function to the collection using a custom identifier.
* The service will be added with a singleton lifetime.
*/
public addSingletonDynamic<T>(ident: ServiceIdent<T>, fn: DynamicValue) {
this._addDynamic(ident, fn, Lifetime.Singleton);
}
/**
* Adds a static to the collection using a custom identifier.
*/
public addStatic<T>(ident: ServiceIdent<T>, value: StaticValue) {
this._add({
kind: Kind.Static,
ident,
value,
});
}
private _addNewable<T>(
ident: ServiceIdent<T>,
impl: Newable<T>,
lifetime: Lifetime,
) {
if (!isClassTagged(impl)) {
throw new Error(
`Class ${impl.name} has not been decorated with @Service`,
);
}
this._add({
kind: Kind.Newable,
ident,
lifetime,
impl,
});
}
private _addDynamic<T>(
ident: ServiceIdent<T>,
fn: DynamicValue,
lifetime: Lifetime,
) {
this._add({
kind: Kind.Dynamic,
ident,
lifetime,
fn,
});
}
private _add(service: Service) {
if (this._services.has(service.ident)) {
throw new Error("Service already added to container");
}
this._services.set(service.ident, service);
}
}
function newableImpl<T>(
ident: ServiceIdent<T>,
impl: Newable<T> | undefined,
): Newable<T> {
if (!impl) {
if (typeof ident !== "function") {
throw new Error("Cannot self bind to a non-class");
}
return ident;
}
return impl;
}