2020-02-16 18:30:00 +00:00
|
|
|
import Datastore from 'nedb'
|
2020-04-18 03:17:45 +00:00
|
|
|
|
|
|
|
let dbLocation
|
|
|
|
|
|
|
|
if (window && window.process && window.process.type === 'renderer') {
|
|
|
|
// Electron is being used
|
|
|
|
let dbLocation = localStorage.getItem('dbLocation')
|
|
|
|
|
|
|
|
if (dbLocation === null) {
|
|
|
|
const electron = require('electron')
|
|
|
|
dbLocation = electron.remote.app.getPath('userData')
|
|
|
|
}
|
|
|
|
|
|
|
|
dbLocation += '/subscriptions.db'
|
|
|
|
} else {
|
|
|
|
dbLocation = 'subscriptions.db'
|
|
|
|
}
|
2020-02-16 18:30:00 +00:00
|
|
|
|
|
|
|
const subDb = new Datastore({
|
2020-04-18 03:17:45 +00:00
|
|
|
filename: dbLocation,
|
2020-02-16 18:30:00 +00:00
|
|
|
autoload: true
|
|
|
|
})
|
|
|
|
|
|
|
|
const state = {
|
|
|
|
subscriptions: []
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutations = {
|
|
|
|
addSubscription (state, payload) {
|
|
|
|
state.subscriptions.push(payload)
|
|
|
|
},
|
|
|
|
setSubscriptions (state, payload) {
|
|
|
|
state.subscriptions = payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
addSubscriptions ({ commit }, payload) {
|
|
|
|
subDb.insert(payload, (err, payload) => {
|
|
|
|
if (!err) {
|
|
|
|
commit('addSubscription', payload)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
getSubscriptions ({ commit }, payload) {
|
|
|
|
subDb.find({}, (err, payload) => {
|
|
|
|
if (!err) {
|
|
|
|
commit('setSubscriptions', payload)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
removeSubscription ({ commit }, channelId) {
|
|
|
|
subDb.remove({ channelId: channelId }, {}, () => {
|
|
|
|
commit('setSubscriptions', this.state.subscriptions.filter(sub => sub.channelId !== channelId))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const getters = {}
|
|
|
|
export default {
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations
|
|
|
|
}
|