-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
429 lines (317 loc) · 7.81 KB
/
index.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* Created by jason on 2017/7/27. license MIT
*/
var slice = [].slice;
/**
* convert some primary value to corresponding Object
* */
var _convert = function (mo) {
if ("number" === typeof mo && !(mo instanceof Number)) {
return new Number(mo);
}
if ("string" === typeof mo && !(mo instanceof String)) {
return new String(mo);
}
if ("boolean" === typeof mo && !(mo instanceof Boolean)) {
return new Boolean(mo);
}
return mo;
}
var module = require("module");
/**
* add hook to module system
* */
if (!module.__registed__) {
var load = module._load;
module._load = function () {
var args = slice.call(arguments);
var filename = module._resolveFilename.apply(module, args);
var mo = load.apply(module, args);
if (undefined !== mo && null !== mo) {
/**
* convert some primary value to corresponding Object
* */
mo = _convert(mo);
if (null !== mo && undefined !== mo) {
/**
* add an identity to the module
* */
mo.__uid_tag__ = filename;
Object.defineProperty(mo, "__uid_tag__", {
enumerable: false
});
}
}
return mo;
}
module.__registed__ = true;
}
var monitors = {};
/**
* create a monitor for modules
* */
var new_monitor = function () {
var caller_file = new_monitor.caller.arguments[3];
monitors[caller_file] = null;
var mon = {};
monitors[caller_file] = mon;
return mon;
}
/**
* delete the old module from monitors
* */
var _delete = function (uid_tag) {
var _has_released = {};
for (var p in monitors) {
var mon = monitors[p];
for (var p in mon) {
if (mon[p].__uid_tag__ === uid_tag) {
/**
* for some modules which need to release resource manually
* */
if ("function" === typeof mon[p]._release) {
if (!_has_released[uid_tag]) {
_has_released[uid_tag] = true;
mon[p]._release();
}
}
/**
* delete the reference
* */
mon[p] = null;
mon[p] = {
"__uid_tag__": uid_tag
};
}
}
}
}
/**
* return who is the user of target module
* */
var _find_module_user = function (uid_tag) {
var users = [];
for (var p in monitors) {
var mon = monitors[p];
for (var q in mon) {
if (mon[q].__uid_tag__ === uid_tag) {
users.push(p);
break;
}
}
}
return users;
}
/**
* clear
* */
var _clear = function (uid_tag) {
for (var p in monitors) {
var mon = monitors[p];
for (var p in mon) {
if (mon[p].__uid_tag__ === uid_tag) {
delete mon[p];
}
}
}
}
/**
* update the module in monitors
* */
var _update = function (uid_tag, new_mo, except) {
for (var q in monitors) {
var mon = monitors[q];
for (var p in mon) {
if (mon[p].__uid_tag__ === uid_tag) {
if (except && except.includes(q)) {}
else {
mon[p] = new_mo;
}
}
}
}
}
/**
* clear the cache hold by module system recursively ,and return what module was be deleted
*
* */
var _caculate_clear_modules = function (uid_tag) {
var old_mo = require.cache[uid_tag];
var delete_sets = [];
var children = [];
for (var i = 0; i < old_mo.children.length; i++) {
if (old_mo.children[i].parent && old_mo.children[i].parent.filename == uid_tag) {
children.push(old_mo.children[i].filename);
}
}
for (var i = 0; i < children.length; i++) {
delete_sets = delete_sets.concat(_caculate_clear_modules(children[i]));
}
delete_sets.push(uid_tag);
return delete_sets;
}
var _clear_module_system_cache = function (uid_tag) {
var old_mo = require.cache[uid_tag];
if (!old_mo) {
return null;
}
/**
* remove the reference from child module
* */
var children = [];
for (var i = 0; i < old_mo.children.length; i++) {
if (old_mo.children[i].parent && old_mo.children[i].parent.filename == uid_tag) {
children.push(old_mo.children[i].filename);
}
}
for (var i = 0; i < children.length; i++) {
_clear_module_system_cache(children[i]);
}
/**
* remove the reference from parent module
* */
if (old_mo.parent) {
var parent_mo = old_mo.parent;
var children = [];
for (var i = 0; i < parent_mo.children.length; i++) {
if (parent_mo.children[i].filename !== uid_tag) {
children.push(parent_mo.children[i]);
} else {
parent_mo.children[i] = null;
}
}
parent_mo.children = children;
}
old_mo = null;
require.cache[uid_tag] = null;
delete require.cache[uid_tag];
return null;
}
/**
* reload target module
* 感觉又回到了C++时代,小心地处理内存。。
* */
var reload = function (file_path) {
var uid_tag = require.resolve(file_path);
if (!uid_tag) {
return null;
}
var delete_sets = _caculate_clear_modules(uid_tag);
var temp_exports = {};
for (var i = 0; i < delete_sets.length; i++) {
/**
* there exists runtime-status in some modules ,such as an counter. we should keep it temporarily.
* */
temp_exports[delete_sets[i]] = require.cache[delete_sets[i]].exports;
/**
* delete the reference in module system
* */
_clear_module_system_cache(delete_sets[i]);
}
/**
* re-require
* */
require(file_path);
var un_reload = [];
for (var i = 0; i < delete_sets.length; i++) {
if (require.cache[delete_sets[i]]) {
delete temp_exports[delete_sets[i]];
_delete(delete_sets[i]);
_update(delete_sets[i], require.cache[delete_sets[i]].exports);
} else {
un_reload.push(delete_sets[i]);
}
}
/**
* some modules are not been reloaded ,because the module (file_path) don't use it directly or indirectly anymore .
* but some of the modules maybe be used in other place ,we need to check if it is safe to delete it completely.
* */
for (var i = 0; i < un_reload.length; i++) {
var users = _find_module_user(un_reload[i]);
var tag = true;
for (var j = 0; j < users.length; j++) {
if (!delete_sets.includes(users[j])) { //imply that the module is also used in other place
tag = false;
break;
}
}
if (!tag) { //the module also be used in other place
require(un_reload[i]); //re-require it
/**
* recovery from temp
* 从保存的缓存恢复,保存原模块的缓存是由于模块本身可能有状态,而重新加载会重新刷新状态,所以需要从缓存恢复
*
* */
if (temp_exports[un_reload[i]]) {
var exp = require.cache[un_reload[i]].exports;
if ("function" === typeof exp._release) {
exp._release();
}
require.cache[un_reload[i]].exports = temp_exports[un_reload[i]];
delete temp_exports[un_reload[i]];
}
_update(un_reload[i], require.cache[un_reload[i]].exports, delete_sets);
} else {
delete temp_exports[un_reload[i]];
delete monitors[un_reload[i]];
_delete(un_reload[i]);
_clear(un_reload[i]);
}
}
};
/**
* just clean the cache of a single file ,and reload it
* */
var reload_one_file = function (full_file_path) {
var uid_tag = require.resolve(full_file_path);
if (!uid_tag) {
return null;
}
var old_mo = require.cache[uid_tag];
if (!old_mo) {
return null;
}
/**
* delete the reference in children
* */
for (var i = 0; i < old_mo.children.length; i++) {
if (old_mo.children[i].parent && old_mo.children[i].parent.filename == uid_tag) {
old_mo.children[i].parent = null;
}
}
/**
* delete the reference in parent module
* */
if (old_mo.parent) {
var parent_mo = old_mo.parent;
var children = [];
for (var i = 0; i < parent_mo.children.length; i++) {
if (parent_mo.children[i].filename !== uid_tag) {
children.push(parent_mo.children[i]);
} else {
parent_mo.children[i] = null;
}
}
parent_mo.children = children;
}
old_mo = null;
/**
* delete the reference in require.cache
* */
delete require.cache[uid_tag];
/**
* delete the reference in monitors
* */
_delete(uid_tag);
/**
* re-require
* */
var new_mo = require(full_file_path);
/**
* update globally
* */
_update(uid_tag, new_mo);
}
exports.new = new_monitor;
exports.reload = reload;
exports.reload_one_file = reload_one_file;