-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.ts
72 lines (55 loc) · 1.31 KB
/
service.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
export enum Lifetime {
Transient,
Scoped,
Singleton,
}
export enum Kind {
Newable,
Dynamic,
Static,
}
export interface Newable<T> {
new (...args: any[]): T;
}
export interface Abstract<T> {
prototype: T;
}
export type ServiceIdent<T> =
| string
| symbol
| Newable<T>
| Abstract<T>;
export function isServiceIdent<T>(ident: unknown): ident is ServiceIdent<T> {
if (typeof ident === "string" || typeof ident === "symbol") {
return true;
}
if (typeof ident === "function") {
return ident !== Object;
}
return false;
}
export interface GenericService<T extends Kind> {
kind: T;
ident: ServiceIdent<any>;
}
export interface LifetimedService<T extends Kind> extends GenericService<T> {
lifetime: Lifetime;
}
export interface Cacheable<T> {
cache?: T;
}
export interface NewableService
extends LifetimedService<Kind.Newable>, Cacheable<Newable<any>> {
impl: Newable<any>;
}
export type DynamicValue = () => any;
export interface DynamicService
extends LifetimedService<Kind.Dynamic>, Cacheable<any> {
fn: DynamicValue;
}
export type StaticValue = any;
export interface StaticService extends GenericService<Kind.Static> {
value: StaticValue;
}
export type Service = NewableService | DynamicService | StaticService;
export type ServiceStore = Map<ServiceIdent<any>, Service>;