From be11e3d8cb28c1629e805d20f19909095f76b55f Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Thu, 17 Jun 2021 04:16:52 +0100 Subject: [PATCH] Chore+Refactor: Replace `nedb` package with `nedb-promises` The 'nedb' package is unmaintained (last update was 5 years ago) and has a couple of high severity vulnerabilities. In addition, the use of callbacks is somewhat cumbersome for the project's current workflow. Therefore, I've decided to replace it with the 'nedb-promises' package, which, internally, makes use of a maintained fork of 'nedb' and wraps its API with Promises. --- package-lock.json | 55 +++---- package.json | 2 +- src/main/index.js | 184 +++++++++++------------ src/renderer/store/datastores.js | 24 ++- src/renderer/store/modules/history.js | 61 +++----- src/renderer/store/modules/playlists.js | 192 +++++++++++------------- src/renderer/store/modules/profiles.js | 129 +++++++--------- src/renderer/store/modules/settings.js | 81 +++++----- 8 files changed, 329 insertions(+), 399 deletions(-) diff --git a/package-lock.json b/package-lock.json index d0f6af6b..1a7c3dfc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3577,6 +3577,21 @@ "fastq": "^1.6.0" } }, + "@seald-io/binary-search-tree": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@seald-io/binary-search-tree/-/binary-search-tree-1.0.2.tgz", + "integrity": "sha512-+pYGvPFAk7wUR+ONMOlc6A+LUN4kOCFwyPLjyaeS7wVibADPHWYJNYsNtyIAwjF1AXQkuaXElnIc4XjKt55QZA==" + }, + "@seald-io/nedb": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@seald-io/nedb/-/nedb-2.0.3.tgz", + "integrity": "sha512-NLnaDav050gnAMM8mnKQE9oH3F9RzGdKornukxLJ7BCf2YXEWndTf3Bmd+1mOqFs5DZnTY0SRigIwVXd8L39Xg==", + "requires": { + "@seald-io/binary-search-tree": "^1.0.2", + "async": "0.2.10", + "localforage": "^1.9.0" + } + }, "@silvermine/videojs-quality-selector": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/@silvermine/videojs-quality-selector/-/videojs-quality-selector-1.2.5.tgz", @@ -5449,21 +5464,6 @@ "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", "dev": true }, - "binary-search-tree": { - "version": "0.2.5", - "resolved": "https://registry.npmjs.org/binary-search-tree/-/binary-search-tree-0.2.5.tgz", - "integrity": "sha1-fbs7IQ/coIJFDa0jNMMErzm9x4Q=", - "requires": { - "underscore": "~1.4.4" - }, - "dependencies": { - "underscore": { - "version": "1.4.4", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz", - "integrity": "sha1-YaajIBBiKvoHljvzJSA88SI51gQ=" - } - } - }, "bindings": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", @@ -12841,7 +12841,8 @@ "minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "dev": true }, "minipass": { "version": "3.1.3", @@ -12887,6 +12888,7 @@ "version": "0.5.5", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", + "dev": true, "requires": { "minimist": "^1.2.5" } @@ -12988,23 +12990,12 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, - "nedb": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/nedb/-/nedb-1.8.0.tgz", - "integrity": "sha1-DjUCzYLABNU1WkPJ5VV3vXvZHYg=", + "nedb-promises": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/nedb-promises/-/nedb-promises-5.0.0.tgz", + "integrity": "sha512-PZ+V+1RScHr3mZHflAMMkx35naGHKVVGtqLwdK2aado7uN2HcIcubiaVlE/X8BTa34RnIHmusVM+VeTj/pnfew==", "requires": { - "async": "0.2.10", - "binary-search-tree": "0.2.5", - "localforage": "^1.3.0", - "mkdirp": "~0.5.1", - "underscore": "~1.4.4" - }, - "dependencies": { - "underscore": { - "version": "1.4.4", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz", - "integrity": "sha1-YaajIBBiKvoHljvzJSA88SI51gQ=" - } + "@seald-io/nedb": "^2.0.3" } }, "needle": { diff --git a/package.json b/package.json index 14067c18..353338bb 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "lodash.uniqwith": "^4.5.0", "marked": "^2.1.1", "material-design-icons": "^3.0.1", - "nedb": "^1.8.0", + "nedb-promises": "^5.0.0", "node-forge": "^0.10.0", "opml-to-json": "^1.0.1", "rss-parser": "^3.12.0", diff --git a/src/main/index.js b/src/main/index.js index d22b6ed6..502f889c 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -2,7 +2,7 @@ import { app, BrowserWindow, dialog, Menu, ipcMain, powerSaveBlocker, screen, session, shell } from 'electron' -import Datastore from 'nedb' +import Datastore from 'nedb-promises' import path from 'path' import cp from 'child_process' @@ -23,7 +23,7 @@ function runApp() { const localDataStorage = app.getPath('userData') // Grabs the userdata directory based on the user's OS - const settingsDb = new Datastore({ + const settingsDb = Datastore.create({ filename: localDataStorage + '/settings.db', autoload: true }) @@ -83,86 +83,88 @@ function runApp() { }) } - app.on('ready', (_, __) => { - settingsDb.find({ - $or: [ - { _id: 'disableSmoothScrolling' }, - { _id: 'useProxy' }, - { _id: 'proxyProtocol' }, - { _id: 'proxyHostname' }, - { _id: 'proxyPort' } - ] - }, function (err, doc) { - if (err) { - app.exit() - return - } - - let disableSmoothScrolling = false - let useProxy = false - let proxyProtocol = 'socks5' - let proxyHostname = '127.0.0.1' - let proxyPort = '9050' - - if (typeof doc === 'object' && doc.length > 0) { - doc.forEach((dbItem) => { - switch (dbItem._id) { - case 'disableSmoothScrolling': - disableSmoothScrolling = dbItem.value - break - case 'useProxy': - useProxy = dbItem.value - break - case 'proxyProtocol': - proxyProtocol = dbItem.value - break - case 'proxyHostname': - proxyHostname = dbItem.value - break - case 'proxyPort': - proxyPort = dbItem.value - break - } - }) - } - - if (disableSmoothScrolling) { - app.commandLine.appendSwitch('disable-smooth-scrolling') - } else { - app.commandLine.appendSwitch('enable-smooth-scrolling') - } - - if (useProxy) { - session.defaultSession.setProxy({ - proxyRules: `${proxyProtocol}://${proxyHostname}:${proxyPort}` - }) - } - - // Set CONSENT cookie on reasonable domains - const consentCookieDomains = [ - 'http://www.youtube.com', - 'https://www.youtube.com', - 'http://youtube.com', - 'https://youtube.com' - ] - consentCookieDomains.forEach(url => { - session.defaultSession.cookies.set({ - url: url, - name: 'CONSENT', - value: 'YES+' - }) + app.on('ready', async (_, __) => { + let docArray + try { + docArray = await settingsDb.find({ + $or: [ + { _id: 'disableSmoothScrolling' }, + { _id: 'useProxy' }, + { _id: 'proxyProtocol' }, + { _id: 'proxyHostname' }, + { _id: 'proxyPort' } + ] }) + } catch (err) { + console.error(err) + app.exit() + return + } - createWindow() + let disableSmoothScrolling = false + let useProxy = false + let proxyProtocol = 'socks5' + let proxyHostname = '127.0.0.1' + let proxyPort = '9050' - if (isDev) { - installDevTools() - } + if (docArray?.length > 0) { + docArray.forEach((doc) => { + switch (doc._id) { + case 'disableSmoothScrolling': + disableSmoothScrolling = doc.value + break + case 'useProxy': + useProxy = doc.value + break + case 'proxyProtocol': + proxyProtocol = doc.value + break + case 'proxyHostname': + proxyHostname = doc.value + break + case 'proxyPort': + proxyPort = doc.value + break + } + }) + } - if (isDebug) { - mainWindow.webContents.openDevTools() - } + if (disableSmoothScrolling) { + app.commandLine.appendSwitch('disable-smooth-scrolling') + } else { + app.commandLine.appendSwitch('enable-smooth-scrolling') + } + + if (useProxy) { + session.defaultSession.setProxy({ + proxyRules: `${proxyProtocol}://${proxyHostname}:${proxyPort}` + }) + } + + // Set CONSENT cookie on reasonable domains + const consentCookieDomains = [ + 'http://www.youtube.com', + 'https://www.youtube.com', + 'http://youtube.com', + 'https://youtube.com' + ] + consentCookieDomains.forEach(url => { + session.defaultSession.cookies.set({ + url: url, + name: 'CONSENT', + value: 'YES+' + }) }) + + await createWindow() + + if (isDev) { + installDevTools() + } + + if (isDebug) { + mainWindow.webContents.openDevTools() + } }) async function installDevTools() { @@ -175,7 +177,7 @@ function runApp() { } } - function createWindow(replaceMainWindow = true) { + async function createWindow(replaceMainWindow = true) { /** * Initial window options */ @@ -206,18 +208,9 @@ function runApp() { height: 800 }) - settingsDb.findOne({ - _id: 'bounds' - }, function (err, doc) { - if (doc === null || err) { - return - } - - if (typeof doc !== 'object' || typeof doc.value !== 'object') { - return - } - - const { maximized, ...bounds } = doc.value + const boundsDoc = await settingsDb.findOne({ _id: 'bounds' }) + if (typeof boundsDoc?.value === 'object') { + const { maximized, ...bounds } = boundsDoc.value const allDisplaysSummaryWidth = screen .getAllDisplays() .reduce((accumulator, { size: { width } }) => accumulator + width, 0) @@ -233,7 +226,7 @@ function runApp() { if (maximized) { newWindow.maximize() } - }) + } // If called multiple times // Duplicate menu items will be added @@ -274,18 +267,19 @@ function runApp() { } // Save closing window bounds & maximized status - ipcMain.on('setBounds', (event) => { + ipcMain.on('setBounds', async (event) => { const value = { ...mainWindow.getNormalBounds(), maximized: mainWindow.isMaximized() } - settingsDb.update( + await settingsDb.update( { _id: 'bounds' }, { _id: 'bounds', value }, - { upsert: true }, - () => { event.returnValue = 0 } + { upsert: true } ) + + event.returnValue = 0 }) ipcMain.on('appReady', () => { diff --git a/src/renderer/store/datastores.js b/src/renderer/store/datastores.js index 8ec225eb..5a236538 100644 --- a/src/renderer/store/datastores.js +++ b/src/renderer/store/datastores.js @@ -1,4 +1,4 @@ -import Datastore from 'nedb' +import Datastore from 'nedb-promises' // Initialize all datastores and export their references // Current dbs: @@ -7,36 +7,34 @@ import Datastore from 'nedb' // `playlists.db` // `history.db` +let buildFileName = null + // Check if using Electron -let userDataPath const usingElectron = window?.process?.type === 'renderer' if (usingElectron) { const { ipcRenderer } = require('electron') - userDataPath = ipcRenderer.sendSync('getUserDataPathSync') + const userDataPath = ipcRenderer.sendSync('getUserDataPathSync') + buildFileName = (dbName) => userDataPath + '/' + dbName + '.db' +} else { + buildFileName = (dbName) => dbName + '.db' } -const buildFileName = (dbName) => { - return usingElectron - ? userDataPath + '/' + dbName + '.db' - : dbName + '.db' -} - -const settingsDb = new Datastore({ +const settingsDb = Datastore.create({ filename: buildFileName('settings'), autoload: true }) -const playlistsDb = new Datastore({ +const playlistsDb = Datastore.create({ filename: buildFileName('playlists'), autoload: true }) -const profilesDb = new Datastore({ +const profilesDb = Datastore.create({ filename: buildFileName('profiles'), autoload: true }) -const historyDb = new Datastore({ +const historyDb = Datastore.create({ filename: buildFileName('history'), autoload: true }) diff --git a/src/renderer/store/modules/history.js b/src/renderer/store/modules/history.js index 47bddcf9..5e41ce4c 100644 --- a/src/renderer/store/modules/history.js +++ b/src/renderer/store/modules/history.js @@ -11,57 +11,46 @@ const getters = { } const actions = { - grabHistory ({ commit }) { - historyDb.find({}).sort({ - timeWatched: -1 - }).exec((err, results) => { - if (err) { - console.log(err) - return - } - commit('setHistoryCache', results) - }) + async grabHistory({ commit }) { + const results = await historyDb.find({}).sort({ timeWatched: -1 }) + commit('setHistoryCache', results) }, - updateHistory ({ dispatch }, videoData) { - historyDb.update({ videoId: videoData.videoId }, videoData, { upsert: true }, (err, numReplaced) => { - if (!err) { - dispatch('grabHistory') - } - }) + async updateHistory({ dispatch }, videoData) { + await historyDb.update( + { videoId: videoData.videoId }, + videoData, + { upsert: true } + ) + dispatch('grabHistory') }, - removeFromHistory ({ dispatch }, videoId) { - historyDb.remove({ videoId: videoId }, (err, numReplaced) => { - if (!err) { - dispatch('grabHistory') - } - }) + async removeFromHistory({ dispatch }, videoId) { + await historyDb.remove({ videoId: videoId }) + dispatch('grabHistory') }, - removeAllHistory ({ dispatch }) { - historyDb.remove({}, { multi: true }, (err, numReplaced) => { - if (!err) { - dispatch('grabHistory') - } - }) + async removeAllHistory({ dispatch }) { + await historyDb.remove({}, { multi: true }) + dispatch('grabHistory') }, - updateWatchProgress ({ dispatch }, videoData) { - historyDb.update({ videoId: videoData.videoId }, { $set: { watchProgress: videoData.watchProgress } }, { upsert: true }, (err, numReplaced) => { - if (!err) { - dispatch('grabHistory') - } - }) + async updateWatchProgress({ dispatch }, videoData) { + await historyDb.update( + { videoId: videoData.videoId }, + { $set: { watchProgress: videoData.watchProgress } }, + { upsert: true } + ) + dispatch('grabHistory') }, - compactHistory (_) { + compactHistory(_) { historyDb.persistence.compactDatafile() } } const mutations = { - setHistoryCache (state, historyCache) { + setHistoryCache(state, historyCache) { state.historyCache = historyCache } } diff --git a/src/renderer/store/modules/playlists.js b/src/renderer/store/modules/playlists.js index 50f457a5..2e587906 100644 --- a/src/renderer/store/modules/playlists.js +++ b/src/renderer/store/modules/playlists.js @@ -24,156 +24,146 @@ const getters = { } const actions = { - addPlaylist ({ commit }, payload) { - playlistsDb.insert(payload, (err, payload) => { - if (err) { - console.error(err) - } else { - commit('addPlaylist', payload) - } - }) + async addPlaylist({ commit }, payload) { + await playlistsDb.insert(payload) + commit('addPlaylist', payload) }, - addPlaylists ({ commit }, payload) { - playlistsDb.insert(payload, (err, payload) => { - if (err) { - console.error(err) - } else { - commit('addPlaylists', payload) - } - }) + + async addPlaylists({ commit }, payload) { + await playlistsDb.insert(payload) + commit('addPlaylists', payload) }, - addVideo ({ commit }, payload) { - playlistsDb.update({ playlistName: payload.playlistName }, { $push: { videos: payload.videoData } }, { upsert: true }, err => { - if (err) { - console.error(err) - } else { - commit('addVideo', payload) - } - }) + + async addVideo({ commit }, payload) { + await playlistsDb.update( + { playlistName: payload.playlistName }, + { $push: { videos: payload.videoData } }, + { upsert: true } + ) + commit('addVideo', payload) }, - addVideos ({ commit }, payload) { - playlistsDb.update({ _id: payload.playlistId }, { $push: { videos: { $each: payload.videosIds } } }, { upsert: true }, err => { - if (err) { - console.error(err) - } else { - commit('addVideos', payload) - } - }) + + async addVideos({ commit }, payload) { + await playlistsDb.update( + { _id: payload.playlistId }, + { $push: { videos: { $each: payload.videosIds } } }, + { upsert: true } + ) + commit('addVideos', payload) }, - grabAllPlaylists({ commit, dispatch }) { - playlistsDb.find({}, (err, payload) => { - if (err) { - console.error(err) - } else { - if (payload.length === 0) { - commit('setAllPlaylists', state.playlists) - dispatch('addPlaylists', payload) - } else { - commit('setAllPlaylists', payload) - } - } - }) + + async grabAllPlaylists({ commit, dispatch }) { + const payload = await playlistsDb.find({}) + if (payload.length === 0) { + commit('setAllPlaylists', state.playlists) + dispatch('addPlaylists', payload) + } else { + commit('setAllPlaylists', payload) + } }, - removeAllPlaylists ({ commit }) { - playlistsDb.remove({ protected: { $ne: true } }, err => { - if (err) { - console.error(err) - } else { - commit('removeAllPlaylists') - } - }) + + async removeAllPlaylists({ commit }) { + await playlistsDb.remove({ protected: { $ne: true } }) + commit('removeAllPlaylists') }, - removeAllVideos ({ commit }, playlistName) { - playlistsDb.update({ playlistName: playlistName }, { $set: { videos: [] } }, { upsert: true }, err => { - if (err) { - console.error(err) - } else { - commit('removeAllVideos', playlistName) - } - }) + + async removeAllVideos({ commit }, playlistName) { + await playlistsDb.update( + { playlistName: playlistName }, + { $set: { videos: [] } }, + { upsert: true } + ) + commit('removeAllVideos', playlistName) }, - removePlaylist ({ commit }, playlistId) { - playlistsDb.remove({ _id: playlistId, protected: { $ne: true } }, (err, playlistId) => { - if (err) { - console.error(err) - } else { - commit('removePlaylist', playlistId) - } + + async removePlaylist({ commit }, playlistId) { + await playlistsDb.remove({ + _id: playlistId, + protected: { $ne: true } }) + commit('removePlaylist', playlistId) }, - removePlaylists ({ commit }, playlistIds) { - playlistsDb.remove({ _id: { $in: playlistIds }, protected: { $ne: true } }, (err, playlistIds) => { - if (err) { - console.error(err) - } else { - commit('removePlaylists', playlistIds) - } + + async removePlaylists({ commit }, playlistIds) { + await playlistsDb.remove({ + _id: { $in: playlistIds }, + protected: { $ne: true } }) + commit('removePlaylists', playlistIds) }, - removeVideo ({ commit }, payload) { - playlistsDb.update({ playlistName: payload.playlistName }, { $pull: { videos: { videoId: payload.videoId } } }, { upsert: true }, (err, numRemoved) => { - if (err) { - console.error(err) - } else { - commit('removeVideo', payload) - } - }) + + async removeVideo({ commit }, payload) { + await playlistsDb.update( + { playlistName: payload.playlistName }, + { $pull: { videos: { videoId: payload.videoId } } }, + { upsert: true } + ) + commit('removeVideo', payload) }, - removeVideos ({ commit }, payload) { - playlistsDb.update({ _id: payload.playlistName }, { $pull: { videos: { $in: payload.videoId } } }, { upsert: true }, err => { - if (err) { - console.error(err) - } else { - commit('removeVideos', payload) - } - }) + + async removeVideos({ commit }, payload) { + await playlistsDb.update( + { _id: payload.playlistName }, + { $pull: { videos: { $in: payload.videoId } } }, + { upsert: true } + ) + commit('removeVideos', payload) } } const mutations = { - addPlaylist (state, payload) { + addPlaylist(state, payload) { state.playlists.push(payload) }, - addPlaylists (state, payload) { + + addPlaylists(state, payload) { state.playlists = state.playlists.concat(payload) }, - addVideo (state, payload) { + + addVideo(state, payload) { const playlist = state.playlists.find(playlist => playlist.playlistName === payload.playlistName) if (playlist) { playlist.videos.push(payload.videoData) } }, - addVideos (state, payload) { + + addVideos(state, payload) { const playlist = state.playlists.find(playlist => playlist._id === payload.playlistId) if (playlist) { playlist.videos = playlist.videos.concat(payload.playlistIds) } }, - removeAllPlaylists (state) { + + removeAllPlaylists(state) { state.playlists = state.playlists.filter(playlist => playlist.protected !== true) }, - removeAllVideos (state, playlistName) { + + removeAllVideos(state, playlistName) { const playlist = state.playlists.find(playlist => playlist.playlistName === playlistName) if (playlist) { playlist.videos = [] } }, - removeVideo (state, payload) { + + removeVideo(state, payload) { const playlist = state.playlists.findIndex(playlist => playlist.playlistName === payload.playlistName) if (playlist !== -1) { state.playlists[playlist].videos = state.playlists[playlist].videos.filter(video => video.videoId !== payload.videoId) } }, - removeVideos (state, payload) { + + removeVideos(state, payload) { const playlist = state.playlists.findIndex(playlist => playlist._id === payload.playlistId) if (playlist !== -1) { playlist.videos = playlist.videos.filter(video => payload.videoId.indexOf(video) === -1) } }, - removePlaylist (state, playlistId) { + + removePlaylist(state, playlistId) { state.playlists = state.playlists.filter(playlist => playlist._id !== playlistId || playlist.protected) }, - setAllPlaylists (state, payload) { + + setAllPlaylists(state, payload) { state.playlists = payload } } diff --git a/src/renderer/store/modules/profiles.js b/src/renderer/store/modules/profiles.js index ed112a03..8b467d8d 100644 --- a/src/renderer/store/modules/profiles.js +++ b/src/renderer/store/modules/profiles.js @@ -22,60 +22,45 @@ const getters = { } const actions = { - grabAllProfiles ({ rootState, dispatch, commit }, defaultName = null) { - return new Promise((resolve, reject) => { - profilesDb.find({}, (err, results) => { - if (!err) { - if (results.length === 0) { - dispatch('createDefaultProfile', defaultName) - } else { - // We want the primary profile to always be first - // So sort with that then sort alphabetically by profile name - const profiles = results.sort((a, b) => { - if (a._id === 'allChannels') { - return -1 - } + async grabAllProfiles({ rootState, dispatch, commit }, defaultName = null) { + let profiles = await profilesDb.find({}) + if (profiles.length === 0) { + dispatch('createDefaultProfile', defaultName) + return + } + // We want the primary profile to always be first + // So sort with that then sort alphabetically by profile name + profiles = profiles.sort((a, b) => { + if (a._id === 'allChannels') { + return -1 + } - if (b._id === 'allChannels') { - return 1 - } + if (b._id === 'allChannels') { + return 1 + } - return b.name - a.name - }) - - if (state.profileList.length < profiles.length) { - const profileIndex = profiles.findIndex((profile) => { - return profile._id === rootState.settings.defaultProfile - }) - - if (profileIndex !== -1) { - commit('setActiveProfile', profileIndex) - } - } - - commit('setProfileList', profiles) - } - - resolve() - } else { - reject(err) - } - }) + return b.name - a.name }) + + if (state.profileList.length < profiles.length) { + const profileIndex = profiles.findIndex((profile) => { + return profile._id === rootState.settings.defaultProfile + }) + + if (profileIndex !== -1) { + commit('setActiveProfile', profileIndex) + } + } + + commit('setProfileList', profiles) }, - grabProfileInfo (_, profileId) { - return new Promise((resolve, reject) => { - console.log(profileId) - profilesDb.findOne({ _id: profileId }, (err, results) => { - if (!err) { - resolve(results) - } - }) - }) + async grabProfileInfo(_, profileId) { + console.log(profileId) + return await profilesDb.findOne({ _id: profileId }) }, - async createDefaultProfile ({ dispatch }, defaultName) { + async createDefaultProfile({ dispatch }, defaultName) { const randomColor = await dispatch('getRandomColor') const textColor = await dispatch('calculateColorLuminance', randomColor) const defaultProfile = { @@ -86,51 +71,47 @@ const actions = { subscriptions: [] } - profilesDb.update({ _id: 'allChannels' }, defaultProfile, { upsert: true }, (err, numReplaced) => { - if (!err) { - dispatch('grabAllProfiles') - } - }) + await profilesDb.update( + { _id: 'allChannels' }, + defaultProfile, + { upsert: true } + ) + dispatch('grabAllProfiles') }, - updateProfile ({ dispatch }, profile) { - profilesDb.update({ _id: profile._id }, profile, { upsert: true }, (err, numReplaced) => { - if (!err) { - dispatch('grabAllProfiles') - } - }) + async updateProfile({ dispatch }, profile) { + await profilesDb.update( + { _id: profile._id }, + profile, + { upsert: true } + ) + dispatch('grabAllProfiles') }, - insertProfile ({ dispatch }, profile) { - profilesDb.insert(profile, (err, newDocs) => { - if (!err) { - dispatch('grabAllProfiles') - } - }) + async insertProfile({ dispatch }, profile) { + await profilesDb.insert(profile) + dispatch('grabAllProfiles') }, - removeProfile ({ dispatch }, profileId) { - profilesDb.remove({ _id: profileId }, (err, numReplaced) => { - if (!err) { - dispatch('grabAllProfiles') - } - }) + async removeProfile({ dispatch }, profileId) { + await profilesDb.remove({ _id: profileId }) + dispatch('grabAllProfiles') }, - compactProfiles (_) { + compactProfiles(_) { profilesDb.persistence.compactDatafile() }, - updateActiveProfile ({ commit }, index) { + updateActiveProfile({ commit }, index) { commit('setActiveProfile', index) } } const mutations = { - setProfileList (state, profileList) { + setProfileList(state, profileList) { state.profileList = profileList }, - setActiveProfile (state, activeProfile) { + setActiveProfile(state, activeProfile) { state.activeProfile = activeProfile } } diff --git a/src/renderer/store/modules/settings.js b/src/renderer/store/modules/settings.js index c12356b3..6e93ea4a 100644 --- a/src/renderer/store/modules/settings.js +++ b/src/renderer/store/modules/settings.js @@ -264,29 +264,19 @@ Object.assign(customGetters, { /**********/ const customActions = { - grabUserSettings: ({ commit, dispatch, getters }) => { - return new Promise((resolve, reject) => { - settingsDb.find( - { _id: { $ne: 'bounds' } }, - (err, userSettings) => { - if (err) { - reject(err) - return - } - - for (const setting of userSettings) { - const { _id, value } = setting - if (getters.settingHasSideEffects(_id)) { - dispatch(defaultSideEffectsTriggerId(_id), value) - } - - commit(defaultMutationId(_id), value) - } - - resolve() - } - ) + grabUserSettings: async ({ commit, dispatch, getters }) => { + const userSettings = await settingsDb.find({ + _id: { $ne: 'bounds' } }) + + for (const setting of userSettings) { + const { _id, value } = setting + if (getters.settingHasSideEffects(_id)) { + dispatch(defaultSideEffectsTriggerId(_id), value) + } + + commit(defaultMutationId(_id), value) + } }, setUpListenerToSyncSettings: ({ commit, dispatch, getters }) => { @@ -347,34 +337,31 @@ for (const settingId of Object.keys(state)) { actions[triggerId] = stateWithSideEffects[settingId].sideEffectsHandler } - actions[updaterId] = ({ commit, dispatch, getters }, value) => { - settingsDb.update( + actions[updaterId] = async ({ commit, dispatch, getters }, value) => { + await settingsDb.update( { _id: settingId }, { _id: settingId, value: value }, - { upsert: true }, - (err, _) => { - if (err) return - - const { - getUsingElectron: usingElectron, - settingHasSideEffects - } = getters - - if (settingHasSideEffects(settingId)) { - dispatch(triggerId, value) - } - commit(mutationId, value) - - if (usingElectron) { - const { ipcRenderer } = require('electron') - - // Propagate settings to all other existing windows - ipcRenderer.send('syncSetting', { - _id: settingId, value: value - }) - } - } + { upsert: true } ) + + const { + getUsingElectron: usingElectron, + settingHasSideEffects + } = getters + + if (settingHasSideEffects(settingId)) { + dispatch(triggerId, value) + } + commit(mutationId, value) + + if (usingElectron) { + const { ipcRenderer } = require('electron') + + // Propagate settings to all other existing windows + ipcRenderer.send('syncSetting', { + _id: settingId, value: value + }) + } } }