diff options
Diffstat (limited to 'lib/app.js')
-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) + )) + }) }) }) } |