Commit 4f85a663f11b42d3b209019fcf598a738e7e43ee
refactor backup export to use run-parallel (less callback hell)
mix irving committed on 5/31/2018, 12:25:02 AMParent: c5b729d599fff96109a0fd575c0e2a2b1fb35311
Files changed
backup/async/exportIdentity.js | changed |
backup/html/backup.js | changed |
package-lock.json | changed |
package.json | changed |
backup/async/exportIdentity.js | ||
---|---|---|
@@ -1,8 +1,9 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | 2 | const { onceTrue } = require('mutant') |
3 | 3 | const path = require('path') |
4 | 4 | const fs = require('fs') |
5 | +const parallel = require('run-parallel') | |
5 | 6 | const mapLimit = require('map-limit') |
6 | 7 | |
7 | 8 | const config = require('../../config').create().config.sync.load() |
8 | 9 | const gossipFile = path.join(config.path, 'gossip.json') |
@@ -27,46 +28,59 @@ | ||
27 | 28 | // gossip: JSON.parse(fs.readFileSync(gossipFile)), |
28 | 29 | } |
29 | 30 | |
30 | 31 | onceTrue(api.sbot.obs.connection, sbot => { |
31 | - sbot.latestSequence(sbot.id, (err, seq) => { | |
32 | - if (err) return cb(err) | |
32 | + parallel([ | |
33 | + getLatestSequence, | |
34 | + getPeersSequence | |
35 | + ], save) | |
33 | 36 | |
34 | - backup.latestSequence = seq | |
37 | + function getLatestSequence (done) { | |
38 | + sbot.latestSequence(sbot.id, (err, seq) => { | |
39 | + if (err) return done(err) | |
35 | 40 | |
41 | + backup.latestSequence = seq | |
42 | + done(null) | |
43 | + }) | |
44 | + } | |
45 | + | |
46 | + function getPeersSequence (done) { | |
36 | 47 | sbot.friends.get({ source: sbot.id }, (err, d) => { |
37 | - if (err) return cb(err) | |
48 | + if (err) return done(err) | |
38 | 49 | |
39 | - console.log(Object.keys(d).length) | |
40 | 50 | var follows = Object.keys(d).filter(id => d[id] === true) |
41 | - console.log(follows.length) | |
42 | 51 | |
43 | - mapLimit(follows, 10, | |
52 | + mapLimit(follows, 5, | |
44 | 53 | (id, cb) => sbot.latestSequence(id, (err, seq) => { |
45 | 54 | if (err && err.message && err.message.indexOf('not found') > 0) { |
46 | 55 | console.error(err) |
47 | - cb(null, null) // don't include this user | |
48 | - } else cb(null, [ id, seq ]) | |
56 | + return cb(null, null) // don't include this user | |
57 | + } | |
58 | + | |
59 | + cb(null, [ id, seq ]) | |
49 | 60 | }), |
50 | 61 | (err, peers) => { |
51 | - if (err) return cb(err) | |
62 | + if (err) return done(err) | |
52 | 63 | |
53 | - console.log(peers.length) | |
54 | 64 | backup.peersSequence = peers |
55 | 65 | .filter(Boolean) |
56 | 66 | .reduce((soFar, [ id, seq ]) => { |
57 | 67 | if (seq) soFar[id] = seq |
58 | 68 | return soFar |
59 | 69 | }, {}) |
60 | - | |
61 | - console.log(Object.keys(backup.peersSequence).length) | |
62 | - | |
63 | - fs.writeFileSync(filename, JSON.stringify(backup, null, 2), 'utf8') | |
64 | - cb(null, true) | |
70 | + done(null) | |
65 | 71 | } |
66 | 72 | ) |
67 | 73 | }) |
68 | - }) | |
74 | + } | |
69 | 75 | }) |
76 | + | |
77 | + function save (err, success) { | |
78 | + if (err) return cb(err) | |
79 | + | |
80 | + fs.writeFileSync(filename, JSON.stringify(backup, null, 2), 'utf8') | |
81 | + cb(null, true) | |
82 | + } | |
83 | + | |
70 | 84 | return true |
71 | 85 | }) |
72 | 86 | } |
backup/html/backup.js | ||
---|---|---|
@@ -21,8 +21,9 @@ | ||
21 | 21 | const success = Value() |
22 | 22 | |
23 | 23 | function exportAction () { |
24 | 24 | exporting.set(true) |
25 | + success.set() // the resets the tick if there are multiple backup exports done | |
25 | 26 | |
26 | 27 | let feedFragment = api.keys.sync.id().slice(1, 6) |
27 | 28 | dialog.showSaveDialog( |
28 | 29 | { |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 283693 bytes New file size: 283951 bytes |
Built with git-ssb-web