問(wèn)題描述
我已經(jīng)搜索并搜索了這個(gè),但無(wú)濟(jì)于事.我已經(jīng)梳理了網(wǎng)絡(luò)(包括 Stackoverflow)和 Node 文檔以尋找答案,但沒有找到一個(gè)有效的答案(也許這對(duì)我來(lái)說(shuō)只是不好的搜索).我正在處理 gulp 配置文件中的事件流,并且我有一個(gè)特定的任務(wù)正在從 EventEmitter 類中遇到經(jīng)典的檢測(cè)到內(nèi)存泄漏"錯(cuò)誤.根據(jù)我的發(fā)現(xiàn),處理這個(gè)問(wèn)題的答案似乎是調(diào)用 EventEmitter 類的 setMaxListeners 方法;我的研究表明事件流有一個(gè) Stream 對(duì)象,它應(yīng)該是 EventEmitter 的一個(gè)實(shí)例.但是,無(wú)論我嘗試調(diào)用 setMaxListeners 的方式,我都找不到方法.在精疲力盡地研究這個(gè)問(wèn)題之后,我想我會(huì)把它帶到這里的節(jié)點(diǎn)專家那里(我假設(shè)這很簡(jiǎn)單,我只是錯(cuò)過(guò)了一些東西).這是我的代碼:
I have searched and searched for this, to no avail. I've combed the web (including Stackoverflow), and the Node docs for an answer, and not found one---that worked (maybe that's just bad searching on my part). I'm working with an event-stream in a gulp config file, and I have a particular task that is hitting the classic "memory leak detected" error from the EventEmitter class. From what I've found, the answer to dealing with this issue seems to be to call the setMaxListeners method of the EventEmitter class; and my research has suggested that the event-stream has a Stream object, which should be an instance of EventEmitter. However, what ever way I try to call setMaxListeners, I get method not found. After exhausting myself researching the issue, I figured I'd bring it to the Node gurus here (I'm assuming this is simple, and I'm just missing something). Here's my code:
return eventStream.merge(
gulp.src('./jscript/libs/file-one.js'),
gulp.src('./jscript/libs/filder_one/file-two.js'),
gulp.src('./jscript/libs/folder_two/file-three.js'),
gulp.src('./jscript/libs/folder_three/file_four.js'),
gulp.src('./jscript/libs/folder_four/file_five.js'),
gulp.src('./jscript/libs/folder_five/file_six.js'),
gulp.src('./jscript/libs/file_seven.js'),
gulp.src('./jscript/libs/file_eight.js'),
gulp.src('./jscript/lists/rules/*.js')
)
.pipe(concat('my_file.js'))
.pipe(gulp.dest('./jscript/dist/'))
.pipe(rename('my_file.min.js'))
.pipe(uglify())
.pipe(gulp.dest('./jscript/dist/'));
謝謝!
推薦答案
這不是您問(wèn)題的直接答案,但是您可以將 glob 模式數(shù)組傳遞給 'gulp.src()' 而不是合并多個(gè) 'gulp.src() 的.不解決你的問(wèn)題嗎?
This is not a direct answer to your question, but you can pass an array of glob patterns to 'gulp.src()' instead of merging multiple 'gulp.src()'s. Doesn't it solve your problem?
https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpsrcglobs-options
編輯
作為直接答案,以下內(nèi)容對(duì)我有用:
As a direct answer, the following worked for me:
var merged = eventStream.merge(...);
merged.setMaxListeners(0);
return merged.pipe(...);
雖然在合并函數(shù)中添加了事件偵聽器,但似乎在事件循環(huán)的滴答聲之間檢查了偵聽器計(jì)數(shù).所以我們可以在添加監(jiān)聽器后立即設(shè)置MaxListeners.
While event listeners are added in the merge function, the listener count seems to be checked between ticks of the event loop. So we can setMaxListeners right after adding listeners.
這篇關(guān)于Node.js 事件流:在哪里設(shè)置 MaxListeners?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!