Skip to content

Commit 9a59d61

Browse files
committed
fix(server): 修复watch文件可能造成内存泄露问题
1 parent a9c60ee commit 9a59d61

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

lib/commands/server.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,28 @@ exports.run = function (options) {
110110
(function () {
111111

112112
// 监测配置文件变化
113-
var watchConfig = function watchConfig(project, middleware, caches, cacheName) {
114-
var projectConfigFilePath = sysPath.resolve(project.config._config.cwd, project.configFile);
115-
fs.watchFile(projectConfigFilePath, { interval: 2000 }, function () {
116-
caches[cacheName] = null;
117-
UtilFs.deleteFolderRecursive(project.cachePath, true);
118-
});
113+
var watchConfig = function watchConfig(project, middleware, cacheName) {
114+
var cwdConfigPath = sysPath.resolve(project.config._config.cwd, project.configFile);
115+
116+
if (watchCacheNames[cwdConfigPath]) {
117+
if (watchCacheNames[cwdConfigPath].indexOf(cacheName) === -1) {
118+
watchCacheNames[cwdConfigPath].push(cacheName);
119+
}
120+
} else {
121+
watchCacheNames[cwdConfigPath] = [cacheName];
122+
123+
fs.watchFile(cwdConfigPath, { interval: 2000 }, function () {
124+
watchCacheNames[cwdConfigPath].map(function (cacheName) {
125+
middlewareCache[cacheName] = null;
126+
});
127+
UtilFs.deleteFolderRecursive(project.cachePath, true);
128+
});
129+
}
119130
};
120131

121132
var middlewareCache = {},
122-
promiseCache = {};
133+
promiseCache = {},
134+
watchCacheNames = {};
123135

124136
if (middlewares) {
125137
middlewares.split('|').forEach(function (proName) {
@@ -239,7 +251,7 @@ exports.run = function (options) {
239251
middleware(req, res, next);
240252
});
241253
// 检测config文件变化
242-
watchConfig(project, middleware, middlewareCache, cacheId);
254+
watchConfig(project, middleware, cacheId);
243255
} else {
244256
next();
245257
}
@@ -279,7 +291,7 @@ exports.run = function (options) {
279291
resolve(middleware);
280292
});
281293
// 检测config文件变化
282-
watchConfig(project, middleware, middlewareCache, projectName);
294+
watchConfig(project, middleware, projectName);
283295
} else {
284296
next();
285297
}

src/commands/server.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ exports.run = (options) => {
106106

107107
if(isGoingToStartServer) {
108108
let middlewareCache = {},
109-
promiseCache = {};
109+
promiseCache = {},
110+
watchCacheNames = {};
110111

111112
if (middlewares) {
112113
middlewares.split('|').forEach((proName) => {
@@ -227,7 +228,7 @@ exports.run = (options) => {
227228
middleware(req, res, next);
228229
});
229230
// 检测config文件变化
230-
watchConfig(project, middleware, middlewareCache, cacheId)
231+
watchConfig(project, middleware, cacheId)
231232
} else {
232233
next()
233234
}
@@ -261,7 +262,7 @@ exports.run = (options) => {
261262
resolve(middleware);
262263
});
263264
// 检测config文件变化
264-
watchConfig(project, middleware, middlewareCache, projectName)
265+
watchConfig(project, middleware, projectName)
265266
} else {
266267
next()
267268
}
@@ -331,12 +332,23 @@ exports.run = (options) => {
331332
}
332333

333334
// 监测配置文件变化
334-
function watchConfig(project, middleware, caches, cacheName) {
335-
const projectConfigFilePath = sysPath.resolve(project.config._config.cwd, project.configFile)
336-
fs.watchFile(projectConfigFilePath, {interval: 2000}, () => {
337-
caches[cacheName] = null
338-
UtilFs.deleteFolderRecursive(project.cachePath, true)
339-
});
335+
function watchConfig(project, middleware, cacheName) {
336+
const cwdConfigPath = sysPath.resolve(project.config._config.cwd, project.configFile)
337+
338+
if(watchCacheNames[cwdConfigPath]) {
339+
if(watchCacheNames[cwdConfigPath].indexOf(cacheName) === -1) {
340+
watchCacheNames[cwdConfigPath].push(cacheName)
341+
}
342+
} else {
343+
watchCacheNames[cwdConfigPath] = [cacheName]
344+
345+
fs.watchFile(cwdConfigPath, {interval: 2000}, () => {
346+
watchCacheNames[cwdConfigPath].map((cacheName) => {
347+
middlewareCache[cacheName] = null
348+
})
349+
UtilFs.deleteFolderRecursive(project.cachePath, true)
350+
});
351+
}
340352
}
341353
}
342354
};

0 commit comments

Comments
 (0)