Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix startup problems (fix for #1417) #1419

Merged
merged 2 commits into from
Jun 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/renderer/lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function setupStateSaved (cb) {
const cpFile = require('cp-file')
const fs = require('fs')
const parseTorrent = require('parse-torrent')
const parallel = require('run-parallel')

const saved = {
prefs: {
Expand All @@ -133,26 +132,25 @@ function setupStateSaved (cb) {

const tasks = []

config.DEFAULT_TORRENTS.map(function (t, i) {
config.DEFAULT_TORRENTS.forEach((t, i) => {
const infoHash = saved.torrents[i].infoHash
tasks.push(function (cb) {
tasks.push(
cpFile(
path.join(config.STATIC_PATH, t.posterFileName),
path.join(config.POSTER_PATH, infoHash + path.extname(t.posterFileName))
).then(cb).catch(cb)
})
tasks.push(function (cb) {
)
)
tasks.push(
cpFile(
path.join(config.STATIC_PATH, t.torrentFileName),
path.join(config.TORRENT_PATH, infoHash + '.torrent')
).then(cb).catch(cb)
})
)
)
})

parallel(tasks, function (err) {
if (err) return cb(err)
cb(null, saved)
})
Promise.all(tasks)
.then(() => cb(null, saved))
.catch(err => cb(err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct way to call an error callback with a promise. It's an easy mistake to make.

The .catch will catch more than just the promise rejections from the tasks array. It will also catch any exceptions thrown from the handler in .then() which means that if the cb function throws an exception, then we'll catch it here.

Instead we should use .then(success, fail). I'll send a PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR here: #1679


function createTorrentObject (t) {
// TODO: Doing several fs.readFileSync calls during first startup is not ideal
Expand Down