Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attach queue to fs module instead of global #184

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions graceful-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ if (typeof Symbol === 'function' && typeof Symbol.for === 'function') {

function noop () {}

function publishQueue(context, queue) {
Object.defineProperty(context, gracefulQueue, {
get: function() {
return queue
}
})
}

var debug = noop
if (util.debuglog)
debug = util.debuglog('gfs4')
Expand All @@ -32,14 +40,10 @@ else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || ''))
}

// Once time initialization
if (!global[gracefulQueue]) {
if (!fs[gracefulQueue]) {
// This queue can be shared by multiple loaded instances
var queue = []
Object.defineProperty(global, gracefulQueue, {
get: function() {
return queue
}
})
var queue = global[gracefulQueue] || []
publishQueue(fs, queue)

// Patch fs.close/closeSync to shared queue version, because we need
// to retry() whenever a close happens *anywhere* in the program.
Expand Down Expand Up @@ -79,12 +83,16 @@ if (!global[gracefulQueue]) {

if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) {
process.on('exit', function() {
debug(global[gracefulQueue])
require('assert').equal(global[gracefulQueue].length, 0)
debug(fs[gracefulQueue])
require('assert').equal(fs[gracefulQueue].length, 0)
})
}
}

if (!global[gracefulQueue]) {
publishQueue(global, fs[gracefulQueue]);
}

module.exports = patch(clone(fs))
if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH && !fs.__patched) {
module.exports = patch(fs)
Expand Down Expand Up @@ -334,11 +342,11 @@ function patch (fs) {

function enqueue (elem) {
debug('ENQUEUE', elem[0].name, elem[1])
global[gracefulQueue].push(elem)
fs[gracefulQueue].push(elem)
}

function retry () {
var elem = global[gracefulQueue].shift()
var elem = fs[gracefulQueue].shift()
if (elem) {
debug('RETRY', elem[0].name, elem[1])
elem[0].apply(null, elem[1])
Expand Down