Skip to content

Commit

Permalink
Refactor and restore datasource deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Rik Smith-Unna committed Jul 2, 2017
1 parent 831cb55 commit fae9bb5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
17 changes: 14 additions & 3 deletions app/client/lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Datasource (key, opts) {
self.loading = true
self.queuedDownloads = []

self.name = 'new datasource ' + key.substring(0, 6)
self.name = 'key:' + key.substring(0, 6) + '...'
self.key = key
self.datadir = path.join(C.DATASOURCES_PATH, key)
mkdirp(self.datadir)
Expand Down Expand Up @@ -340,11 +340,22 @@ function Datasource (key, opts) {
// close all databases
self.close = cb => {
self.stats.write()
self.metadata.close(() => self.archive.close(() => self.db.close(cb)))
const toclose = [self.metadata, self.articles, self.db]
const done = alldone(toclose.length, cb)

toclose.forEach(item => {
try {
item ? item.close.bind(item)(done) : done()
} catch (err) {
debug(err)
}
})
}

// close all databases, then delete the datasource directory
self.remove = cb => self.close(() => fs.remove(self.datadir, cb))
self.remove = cb => self.close(
err => err ? cb(err) : fs.remove(self.datadir, cb)
)

self.articlestats = (files, cb) => process.nextTick(() => {
const stats = {
Expand Down
19 changes: 12 additions & 7 deletions app/client/lib/getdatasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ const Datasource = require('./datasource')

const datasources = {}

const fetchds = (opts, cb) => {
let key = typeof opts === 'string' ? opts : opts.key
const ds = datasources[key] || Datasource(key, opts)
datasources[key] = ds
return cb(null, ds)
}

module.exports = {
fetch: (opts, cb) => {
let key = typeof opts === 'string' ? opts : opts.key
const ds = datasources[key] || Datasource(key, opts)
datasources[key] = ds
return cb(null, ds)
},
fetch: fetchds,
all: () => Object.keys(datasources).map(key => datasources[key]),
del: key => datasources[key].remove(() => { delete datasources[key] })
del: key => fetchds(key, (err, ds) => {
if (err) throw err
ds.remove(() => { delete datasources[key] })
})
}
21 changes: 17 additions & 4 deletions app/client/models/datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,29 @@ module.exports = (state, bus) => {
if (datasource.all().length > 1) {
bus.emit('notification:add', {
title: 'Datasource added',
message: 'datasource added:\n' + source.name
message: 'datasource added:\n' + ds.name
})
}

ds.on('connected', () => bus.emit('initialising:stop'))
ds.on('connected', () => {
if (state.initialising) bus.emit('initialising:stop')
})
ds.on('progress', () => { if (state.initialising) render() })
})
}

const remove = key => datasource.del(key)
const remove = source => {
datasource.fetch(source, (err, ds) => {
if (err) throw err

datasource.del(source)

bus.emit('notification:add', {
title: 'Datasource removed',
message: 'datasource removed:\n' + ds.name
})
})
}

let activesearches = []

Expand Down Expand Up @@ -179,7 +192,7 @@ module.exports = (state, bus) => {
setInterval(poll, 1000)

bus.on('datasources:add', add)
bus.on('datasources:remove', add)
bus.on('datasources:remove', remove)
bus.on('datasources:search', search)
bus.on('datasources:cancel-search', cancelsearch)
bus.on('datasources:show', show)
Expand Down
4 changes: 3 additions & 1 deletion app/client/views/datasource_checklist_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ module.exports = (datasource, state, emit) => {

deletebtn.onclick = e => {
e.preventDefault()
emit('renderer:freeze')
require('./overlay_confirm')(
`Really delete datasource ${datasource.name} and all its data?`,
really => {
emit('renderer:unfreeze')
if (really) emit('datasources:remove', datasource.key)
}
)
Expand All @@ -225,7 +227,7 @@ module.exports = (datasource, state, emit) => {
</div>
</div>
<div class=${style.right}>
${state.datasources.length > 1 ? deletebtn : null}
${state.datasources.list.length > 1 ? deletebtn : null}
</div>
</div>
Expand Down

0 comments on commit fae9bb5

Please sign in to comment.