Skip to content

Commit e7bf06c

Browse files
committed
Added support for postInit
1 parent 5871dd1 commit e7bf06c

1 file changed

Lines changed: 7 additions & 28 deletions

File tree

src/di/di.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,9 @@ const serviceProvider = Symbol("serviceProvider");
1818

1919
const globalServiceProvider = Symbol("globalInstance");
2020

21-
let newServiceTarget = null;
22-
23-
const patched = Symbol("serviceConstructorPatched");
24-
25-
const patchClass = (type) => {
26-
if (type[patched]) {
27-
return;
28-
}
29-
const parent = Object.getPrototypeOf(type);
30-
if (parent === null || parent === Object || parent === Object.prototype) {
31-
// eslint-disable-next-line @typescript-eslint/ban-types
32-
const old = type.constructor as Function;
33-
type.constructor = function (...a) {
34-
this[serviceProvider] = newServiceTarget;
35-
return old.apply(this, a);
36-
};
37-
type[patched] = true;
38-
return;
39-
}
40-
patchClass(parent);
41-
type[patched] = true;
42-
};
21+
export abstract class ServiceObject {
22+
abstract postInit();
23+
}
4324

4425
export class ServiceProvider implements IDisposable {
4526

@@ -190,14 +171,10 @@ export class ServiceProvider implements IDisposable {
190171
}
191172

192173
private createFromType(type, key = type): any {
193-
const old = newServiceTarget;
194-
newServiceTarget = this;
195174
const injectTypes = type[injectServiceTypesSymbol] as any[];
196175
const injectServices = injectTypes
197176
? injectTypes.map((x) => this.resolve(x))
198177
: [];
199-
patchClass(type.prototype);
200-
type = type.prototype?.constructor ?? type;
201178
const instance = new type(... injectServices);
202179
this.map.set(key, instance);
203180
instance[serviceProvider] = this;
@@ -207,7 +184,9 @@ export class ServiceProvider implements IDisposable {
207184
}
208185
// initialize properties...
209186
this.resolveProperties(instance, type);
210-
newServiceTarget = old;
187+
if (instance instanceof ServiceObject) {
188+
instance.postInit()?.catch(console.error);
189+
}
211190
return instance;
212191
}
213192

@@ -294,7 +273,7 @@ export default function Inject(target, key, index?: number): any {
294273
(target[injectServiceKeysSymbol] ??= {})[key] = pType;
295274
const descriptor = {
296275
get() {
297-
const result = ServiceProvider.resolve(newServiceTarget ?? this, pType);
276+
const result = ServiceProvider.resolve(this, pType);
298277
// get is compatible with AtomWatcher
299278
// as it will ignore getter and it will
300279
// not try to set a binding refresher

0 commit comments

Comments
 (0)