Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/common/hotReload.ts
3291 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IDisposable } from './lifecycle.js';
7
8
export function isHotReloadEnabled(): boolean {
9
// return env && !!env['VSCODE_DEV_DEBUG'];
10
return false; // TODO@hediet investigate how to get hot reload
11
}
12
export function registerHotReloadHandler(handler: HotReloadHandler): IDisposable {
13
if (!isHotReloadEnabled()) {
14
return { dispose() { } };
15
} else {
16
const handlers = registerGlobalHotReloadHandler();
17
handlers.add(handler);
18
return {
19
dispose() { handlers.delete(handler); }
20
};
21
}
22
}
23
24
/**
25
* Takes the old exports of the module to reload and returns a function to apply the new exports.
26
* If `undefined` is returned, this handler is not able to handle the module.
27
*
28
* If no handler can apply the new exports, the module will not be reloaded.
29
*/
30
export type HotReloadHandler = (args: { oldExports: Record<string, unknown>; newSrc: string; config: IHotReloadConfig }) => AcceptNewExportsHandler | undefined;
31
export type AcceptNewExportsHandler = (newExports: Record<string, unknown>) => boolean;
32
export type IHotReloadConfig = HotReloadConfig;
33
34
function registerGlobalHotReloadHandler() {
35
if (!hotReloadHandlers) {
36
hotReloadHandlers = new Set();
37
}
38
39
const g = globalThis as unknown as GlobalThisAddition;
40
if (!g.$hotReload_applyNewExports) {
41
g.$hotReload_applyNewExports = args => {
42
const args2 = { config: { mode: undefined }, ...args };
43
44
const results: AcceptNewExportsHandler[] = [];
45
for (const h of hotReloadHandlers!) {
46
const result = h(args2);
47
if (result) {
48
results.push(result);
49
}
50
}
51
if (results.length > 0) {
52
return newExports => {
53
let result = false;
54
for (const r of results) {
55
if (r(newExports)) {
56
result = true;
57
}
58
}
59
return result;
60
};
61
}
62
return undefined;
63
};
64
}
65
66
return hotReloadHandlers;
67
}
68
69
let hotReloadHandlers: Set<(args: { oldExports: Record<string, unknown>; newSrc: string; config: HotReloadConfig }) => AcceptNewExportsFn | undefined> | undefined = undefined;
70
71
interface HotReloadConfig {
72
mode?: 'patch-prototype' | undefined;
73
}
74
75
interface GlobalThisAddition {
76
$hotReload_applyNewExports?(args: { oldExports: Record<string, unknown>; newSrc: string; config?: HotReloadConfig }): AcceptNewExportsFn | undefined;
77
}
78
79
type AcceptNewExportsFn = (newExports: Record<string, unknown>) => boolean;
80
81
if (isHotReloadEnabled()) {
82
// This code does not run in production.
83
registerHotReloadHandler(({ oldExports, newSrc, config }) => {
84
if (config.mode !== 'patch-prototype') {
85
return undefined;
86
}
87
88
return newExports => {
89
for (const key in newExports) {
90
const exportedItem = newExports[key];
91
console.log(`[hot-reload] Patching prototype methods of '${key}'`, { exportedItem });
92
if (typeof exportedItem === 'function' && exportedItem.prototype) {
93
const oldExportedItem = oldExports[key];
94
if (oldExportedItem) {
95
for (const prop of Object.getOwnPropertyNames(exportedItem.prototype)) {
96
const descriptor = Object.getOwnPropertyDescriptor(exportedItem.prototype, prop)!;
97
const oldDescriptor = Object.getOwnPropertyDescriptor((oldExportedItem as any).prototype, prop);
98
99
if (descriptor?.value?.toString() !== oldDescriptor?.value?.toString()) {
100
console.log(`[hot-reload] Patching prototype method '${key}.${prop}'`);
101
}
102
103
Object.defineProperty((oldExportedItem as any).prototype, prop, descriptor);
104
}
105
newExports[key] = oldExportedItem;
106
}
107
}
108
}
109
return true;
110
};
111
});
112
}
113
114