Skip to content

Commit

Permalink
Added some very basic caching mecanism to avoid directory relisting.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreh committed Sep 9, 2011
1 parent a240323 commit abcbac8
Showing 1 changed file with 48 additions and 37 deletions.
85 changes: 48 additions & 37 deletions project.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,52 +111,59 @@ exports.chdir = function(dir) {
}
}

exports.list = function() {
var ee = new EventEmitter();
var listCache = undefined
var addToListCache = function(path) {
var cwd = process.cwd()
var cwd_length = cwd.length + 1;
var result = {
name: "",
type: "directory",
path: "",
children: {}
}
var add = function(path) {
var current = result
var composite_path = ""
var components = path.substr(cwd_length).split('/')
for (var i = 0; i < components.length - 1; i++) {
composite_path = composite_path + "/" + components[i]
if (!current.children[components[i]]) {
current.children[components[i]] = {
name: components[i],
type: "directory",
path: composite_path,
children: {}
}
}
current = current.children[components[i]]
}
if (components[i] != '.') {
composite_path = composite_path + "/" + components[i]
var current = listCache
var composite_path = ""
var components = path.substr(cwd_length).split('/')
for (var i = 0; i < components.length - 1; i++) {
composite_path = composite_path + "/" + components[i]
if (!current.children[components[i]]) {
current.children[components[i]] = {
name: components[i],
type: "file",
type: "directory",
path: composite_path,
children: {}
}
}
current = current.children[components[i]]
}
if (components[i] != '.') {
composite_path = composite_path + "/" + components[i]
current.children[components[i]] = {
name: components[i],
type: "file",
path: composite_path,
children: {}
}
}
find(cwd)
.on('file', function(path) {
add(path)
})
.on('directory', function(path) {
add(path + "/.")
})
.on('end', function() {
ee.emit('success', result)
})
}
exports.list = function() {
var ee = new EventEmitter();
if (!listCache) {
listCache = {
name: "",
type: "directory",
path: "",
children: {}
}
find(process.cwd())
.on('file', function(path) {
addToListCache(path)
})
.on('directory', function(path) {
addToListCache(path + "/.")
})
.on('end', function() {
ee.emit('success', listCache)
})
} else {
process.nextTick(function() {
ee.emit('success', listCache)
})
}
return ee
}

Expand All @@ -174,6 +181,7 @@ exports.add = function(path) {
fs.writeFile(process.cwd() + path, '', 'utf8', function(err) {
if (err) ee.emit('error', err);
else {
addToListCache(process.cwd() + path)
ee.emit('success');
}
})
Expand All @@ -198,6 +206,7 @@ exports.addFolder = function(path) {
fs.mkdir(process.cwd() + path, '755', function(err) {
if (err) ee.emit('error', err);
else {
addToListCache(path + '/.')
ee.emit('success');
}
})
Expand All @@ -216,6 +225,8 @@ exports.remove = function(path) {
} else {
exec('rm -rf -- ' + process.cwd() + path, function(err) {
if (!err) {
// TODO: Implement removal from list cache
listCache = undefined
ee.emit('success')
} else {
ee.emit('err', err)
Expand Down

0 comments on commit abcbac8

Please sign in to comment.