-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
Provider.ts
171 lines (143 loc) · 3.76 KB
/
Provider.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
import {classOf, getClassOrSymbol, isClass, Metadata, methodsOf, nameOf, Store, Type} from "@tsed/core";
import {ProviderOpts} from "../interfaces/ProviderOpts";
import {TokenProvider} from "../interfaces/TokenProvider";
import {ProviderScope} from "./ProviderScope";
import {ProviderType} from "./ProviderType";
export type ProviderHookCallback<T = any> = (instance: T, ...args: any[]) => Promise<void> | void;
export class Provider<T = any> implements ProviderOpts<T> {
/**
* Token group provider to retrieve all provider from the same type
*/
public type: TokenProvider | ProviderType = ProviderType.PROVIDER;
public deps: TokenProvider[];
public imports: any[];
public useFactory: Function;
public useAsyncFactory: Function;
public useValue: any;
public hooks?: Record<string, ProviderHookCallback<T>>;
private _useClass: Type<T>;
private _provide: TokenProvider;
private _store: Store;
private _tokenStore: Store;
[key: string]: any;
constructor(token: TokenProvider, options: Partial<Provider> = {}) {
this.provide = token;
this.useClass = token;
Object.assign(this, options);
}
get token() {
return this._provide;
}
get provide(): TokenProvider {
return this._provide;
}
set provide(value: TokenProvider) {
if (value) {
this._provide = getClassOrSymbol(value);
this._tokenStore = this._store = Store.from(value);
}
}
get useClass(): Type<T> {
return this._useClass;
}
/**
* Create a new store if the given value is a class. Otherwise the value is ignored.
* @param value
*/
set useClass(value: Type<T>) {
if (isClass(value)) {
this._useClass = classOf(value);
this._store = Store.from(value);
this.hooks = methodsOf(this._useClass).reduce((hooks, {propertyKey}) => {
if (String(propertyKey).startsWith("$")) {
return {
...hooks,
[propertyKey]: (instance: any, ...args: any[]) => instance[propertyKey](...args)
};
}
return hooks;
}, {} as any);
}
}
get className() {
return this.name;
}
get name() {
return nameOf(this.provide);
}
get store(): Store {
return this._store;
}
get path() {
return this.store.get("path", "/");
}
set path(path: string) {
this.store.set("path", path);
}
/**
* Get the scope of the provider.
*
* ::: tip Note
* Async provider is always a SINGLETON
* :::
*
* @returns {boolean}
*/
get scope(): ProviderScope {
if (this.isAsync()) {
return ProviderScope.SINGLETON;
}
return this.get("scope");
}
/**
* Change the scope value of the provider.
* @param scope
*/
set scope(scope: ProviderScope) {
this.store.set("scope", scope);
}
get configuration(): Partial<TsED.Configuration> {
return this.get("configuration");
}
set configuration(configuration: Partial<TsED.Configuration>) {
this.store.set("configuration", configuration);
}
get children(): TokenProvider[] {
return this.store.get("childrenControllers", []);
}
get(key: string) {
return this.store.get(key) || this._tokenStore.get(key);
}
isAsync(): boolean {
return !!this.useAsyncFactory;
}
clone(): Provider {
return new (classOf(this))(this._provide, this);
}
/**
*
* @returns {boolean}
*/
public hasChildren(): boolean {
return !!this.children.length;
}
/**
*
* @returns {boolean}
*/
public hasParent(): boolean {
return !!this.store.get("parentController");
}
toString() {
return [
"Token",
this.name,
this.useClass && nameOf(this.useClass),
this.useFactory && "Factory",
this.useValue && "Value",
this.useAsyncFactory && "AsyncFactory"
]
.filter(Boolean)
.join(":");
}
}