From 0bbc1ad05874c1e6a7c694bd36d6d8882be57011 Mon Sep 17 00:00:00 2001 From: cel Date: Mon, 11 Mar 2019 15:35:16 -1000 Subject: Sort drafts by mtime --- lib/app.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/app.js b/lib/app.js index f853d24..25b5a94 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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) + )) + }) }) }) } -- cgit v1.2.3