-
Notifications
You must be signed in to change notification settings - Fork 31.3k
/
Copy pathwrapInReloadableClass.ts
62 lines (52 loc) · 2.57 KB
/
wrapInReloadableClass.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { isHotReloadEnabled } from '../../../base/common/hotReload.js';
import { readHotReloadableExport } from '../../../base/common/hotReloadHelpers.js';
import { IDisposable } from '../../../base/common/lifecycle.js';
import { autorunWithStore } from '../../../base/common/observable.js';
import { BrandedService, GetLeadingNonServiceArgs, IInstantiationService } from '../../instantiation/common/instantiation.js';
/**
* Wrap a class in a reloadable wrapper.
* When the wrapper is created, the original class is created.
* When the original class changes, the instance is re-created.
*/
export function wrapInReloadableClass0<TArgs extends BrandedService[]>(getClass: () => Result<TArgs>): Result<GetLeadingNonServiceArgs<TArgs>> {
return !isHotReloadEnabled() ? getClass() : createWrapper(getClass, BaseClass0);
}
type Result<TArgs extends any[]> = new (...args: TArgs) => IDisposable;
class BaseClass {
constructor(
public readonly instantiationService: IInstantiationService,
) { }
public init(...params: any[]): void { }
}
function createWrapper<T extends any[]>(getClass: () => any, B: new (...args: T) => BaseClass) {
return (class ReloadableWrapper extends B {
private _autorun: IDisposable | undefined = undefined;
override init(...params: any[]) {
this._autorun = autorunWithStore((reader, store) => {
const clazz = readHotReloadableExport(getClass(), reader);
store.add(this.instantiationService.createInstance(clazz as any, ...params) as IDisposable);
});
}
dispose(): void {
this._autorun?.dispose();
}
}) as any;
}
class BaseClass0 extends BaseClass {
constructor(@IInstantiationService i: IInstantiationService) { super(i); this.init(); }
}
/**
* Wrap a class in a reloadable wrapper.
* When the wrapper is created, the original class is created.
* When the original class changes, the instance is re-created.
*/
export function wrapInReloadableClass1<TArgs extends [any, ...BrandedService[]]>(getClass: () => Result<TArgs>): Result<GetLeadingNonServiceArgs<TArgs>> {
return !isHotReloadEnabled() ? getClass() as any : createWrapper(getClass, BaseClass1);
}
class BaseClass1 extends BaseClass {
constructor(param1: any, @IInstantiationService i: IInstantiationService,) { super(i); this.init(param1); }
}