問題描述
我已經搜索并搜索了這個,但無濟于事.我已經梳理了網絡(包括 Stackoverflow)和 Node 文檔以尋找答案,但沒有找到一個有效的答案(也許這對我來說只是不好的搜索).我正在處理 gulp 配置文件中的事件流,并且我有一個特定的任務正在從 EventEmitter 類中遇到經典的檢測到內存泄漏"錯誤.根據我的發現,處理這個問題的答案似乎是調用 EventEmitter 類的 setMaxListeners 方法;我的研究表明事件流有一個 Stream 對象,它應該是 EventEmitter 的一個實例.但是,無論我嘗試調用 setMaxListeners 的方式,我都找不到方法.在精疲力盡地研究這個問題之后,我想我會把它帶到這里的節點專家那里(我假設這很簡單,我只是錯過了一些東西).這是我的代碼:
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/'));
謝謝!
推薦答案
這不是您問題的直接答案,但是您可以將 glob 模式數組傳遞給 'gulp.src()' 而不是合并多個 'gulp.src() 的.不解決你的問題嗎?
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
編輯
作為直接答案,以下內容對我有用:
As a direct answer, the following worked for me:
var merged = eventStream.merge(...);
merged.setMaxListeners(0);
return merged.pipe(...);
雖然在合并函數中添加了事件偵聽器,但似乎在事件循環的滴答聲之間檢查了偵聽器計數.所以我們可以在添加監聽器后立即設置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.
這篇關于Node.js 事件流:在哪里設置 MaxListeners?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!