git ssb

2+

mixmix / ticktack



Commit 4f85a663f11b42d3b209019fcf598a738e7e43ee

refactor backup export to use run-parallel (less callback hell)

mix irving committed on 5/31/2018, 12:25:02 AM
Parent: c5b729d599fff96109a0fd575c0e2a2b1fb35311

Files changed

backup/async/exportIdentity.jschanged
backup/html/backup.jschanged
package-lock.jsonchanged
package.jsonchanged
backup/async/exportIdentity.jsView
@@ -1,8 +1,9 @@
11 const nest = require('depnest')
22 const { onceTrue } = require('mutant')
33 const path = require('path')
44 const fs = require('fs')
5+const parallel = require('run-parallel')
56 const mapLimit = require('map-limit')
67
78 const config = require('../../config').create().config.sync.load()
89 const gossipFile = path.join(config.path, 'gossip.json')
@@ -27,46 +28,59 @@
2728 // gossip: JSON.parse(fs.readFileSync(gossipFile)),
2829 }
2930
3031 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)
3336
34- backup.latestSequence = seq
37+ function getLatestSequence (done) {
38+ sbot.latestSequence(sbot.id, (err, seq) => {
39+ if (err) return done(err)
3540
41+ backup.latestSequence = seq
42+ done(null)
43+ })
44+ }
45+
46+ function getPeersSequence (done) {
3647 sbot.friends.get({ source: sbot.id }, (err, d) => {
37- if (err) return cb(err)
48+ if (err) return done(err)
3849
39- console.log(Object.keys(d).length)
4050 var follows = Object.keys(d).filter(id => d[id] === true)
41- console.log(follows.length)
4251
43- mapLimit(follows, 10,
52+ mapLimit(follows, 5,
4453 (id, cb) => sbot.latestSequence(id, (err, seq) => {
4554 if (err && err.message && err.message.indexOf('not found') > 0) {
4655 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 ])
4960 }),
5061 (err, peers) => {
51- if (err) return cb(err)
62+ if (err) return done(err)
5263
53- console.log(peers.length)
5464 backup.peersSequence = peers
5565 .filter(Boolean)
5666 .reduce((soFar, [ id, seq ]) => {
5767 if (seq) soFar[id] = seq
5868 return soFar
5969 }, {})
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)
6571 }
6672 )
6773 })
68- })
74+ }
6975 })
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+
7084 return true
7185 })
7286 }
backup/html/backup.jsView
@@ -21,8 +21,9 @@
2121 const success = Value()
2222
2323 function exportAction () {
2424 exporting.set(true)
25+ success.set() // the resets the tick if there are multiple backup exports done
2526
2627 let feedFragment = api.keys.sync.id().slice(1, 6)
2728 dialog.showSaveDialog(
2829 {
package-lock.jsonView
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
package.jsonView
@@ -57,8 +57,9 @@
5757 "pull-obv": "^1.3.0",
5858 "pull-stream": "^3.6.0",
5959 "read-directory": "^2.1.0",
6060 "require-style": "^1.0.1",
61+ "run-parallel": "^1.1.9",
6162 "scuttle-blog": "^1.0.1",
6263 "scuttlebot": "10.4.10",
6364 "secret-stack": "4.0.1",
6465 "setimmediate": "^1.0.5",

Built with git-ssb-web