diff options
author | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2019-03-11 15:35:16 -1000 |
---|---|---|
committer | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2019-03-11 15:35:16 -1000 |
commit | 0bbc1ad05874c1e6a7c694bd36d6d8882be57011 (patch) | |
tree | c063bb65b8ca9384b4e3623a01e5c7cbb84ec441 | |
parent | bf2555a2efdf6055309402b92288818fdbabea3e (diff) | |
download | patchfoo-0bbc1ad05874c1e6a7c694bd36d6d8882be57011.tar.gz patchfoo-0bbc1ad05874c1e6a7c694bd36d6d8882be57011.zip |
Sort drafts by mtime
-rw-r--r-- | lib/app.js | 34 |
1 files changed, 28 insertions, 6 deletions
@@ -1465,18 +1465,40 @@ App.prototype.discardDraft = function (id, cb) { fs.unlink(path.join(this.draftsDir, id), cb) } +function compareMtime(a, b) { + return b.mtime.getTime() - a.mtime.getTime() +} + +function statAll(files, dir, cb) { + pull( + pull.values(files), + paramap(function (file, cb) { + fs.stat(path.join(dir, file), function (err, stats) { + if (err) return cb(err) + stats.name = file + cb(null, stats) + }) + }, 8), + pull.collect(cb) + ) +} + App.prototype.listDrafts = function () { var self = this return u.readNext(function (cb) { fs.readdir(self.draftsDir, function (err, files) { if (err && err.code === 'ENOENT') return cb(null, pull.empty()) if (err) return cb(err) - cb(null, pull( - pull.values(files), - pull.asyncMap(function (name, cb) { - self.getDraft(name, cb) - }) - )) + statAll(files, self.draftsDir, function (err, stats) { + if (err) return cb(err) + stats.sort(compareMtime) + cb(null, pull( + pull.values(stats), + paramap(function (stat, cb) { + self.getDraft(stat.name, cb) + }, 4) + )) + }) }) }) } |