diff --git a/package.json b/package.json index be1ebbbc6a11..bf7fc10063e0 100644 --- a/package.json +++ b/package.json @@ -58,12 +58,12 @@ "@fortawesome/free-brands-svg-icons": "^6.4.2", "@fortawesome/free-solid-svg-icons": "^6.4.2", "@fortawesome/vue-fontawesome": "^2.0.10", + "@seald-io/nedb": "^4.0.2", "@silvermine/videojs-quality-selector": "^1.3.0", "autolinker": "^4.0.0", "electron-context-menu": "^3.6.1", "lodash.debounce": "^4.0.8", "marked": "^7.0.0", - "nedb-promises": "^6.2.1", "path-browserify": "^1.0.1", "process": "^0.11.10", "video.js": "7.21.5", diff --git a/src/datastores/handlers/base.js b/src/datastores/handlers/base.js index 11eebc32e2d0..71e1ca388968 100644 --- a/src/datastores/handlers/base.js +++ b/src/datastores/handlers/base.js @@ -2,17 +2,21 @@ import db from '../index' class Settings { static find() { - return db.settings.find({ _id: { $ne: 'bounds' } }) + return db.settings.findAsync({ _id: { $ne: 'bounds' } }) } static upsert(_id, value) { - return db.settings.update({ _id }, { _id, value }, { upsert: true }) + return db.settings.updateAsync({ _id }, { _id, value }, { upsert: true }) + } + + static persist() { + return db.settings.compactDatafileAsync() } // ******************** // // Unique Electron main process handlers static _findAppReadyRelatedSettings() { - return db.settings.find({ + return db.settings.findAsync({ $or: [ { _id: 'disableSmoothScrolling' }, { _id: 'useProxy' }, @@ -24,92 +28,92 @@ class Settings { } static _findBounds() { - return db.settings.findOne({ _id: 'bounds' }) + return db.settings.findOneAsync({ _id: 'bounds' }) } static _findTheme() { - return db.settings.findOne({ _id: 'baseTheme' }) + return db.settings.findOneAsync({ _id: 'baseTheme' }) } static _findSidenavSettings() { return { - hideTrendingVideos: db.settings.findOne({ _id: 'hideTrendingVideos' }), - hidePopularVideos: db.settings.findOne({ _id: 'hidePopularVideos' }), - backendFallback: db.settings.findOne({ _id: 'backendFallback' }), - backendPreference: db.settings.findOne({ _id: 'backendPreference' }), - hidePlaylists: db.settings.findOne({ _id: 'hidePlaylists' }), + hideTrendingVideos: db.settings.findOneAsync({ _id: 'hideTrendingVideos' }), + hidePopularVideos: db.settings.findOneAsync({ _id: 'hidePopularVideos' }), + backendFallback: db.settings.findOneAsync({ _id: 'backendFallback' }), + backendPreference: db.settings.findOneAsync({ _id: 'backendPreference' }), + hidePlaylists: db.settings.findOneAsync({ _id: 'hidePlaylists' }), } } static _updateBounds(value) { - return db.settings.update({ _id: 'bounds' }, { _id: 'bounds', value }, { upsert: true }) + return db.settings.updateAsync({ _id: 'bounds' }, { _id: 'bounds', value }, { upsert: true }) } // ******************** // } class History { static find() { - return db.history.find({}).sort({ timeWatched: -1 }) + return db.history.findAsync({}).sort({ timeWatched: -1 }) } static upsert(record) { - return db.history.update({ videoId: record.videoId }, record, { upsert: true }) + return db.history.updateAsync({ videoId: record.videoId }, record, { upsert: true }) } static updateWatchProgress(videoId, watchProgress) { - return db.history.update({ videoId }, { $set: { watchProgress } }, { upsert: true }) + return db.history.updateAsync({ videoId }, { $set: { watchProgress } }, { upsert: true }) } static updateLastViewedPlaylist(videoId, lastViewedPlaylistId) { - return db.history.update({ videoId }, { $set: { lastViewedPlaylistId } }, { upsert: true }) + return db.history.updateAsync({ videoId }, { $set: { lastViewedPlaylistId } }, { upsert: true }) } static delete(videoId) { - return db.history.remove({ videoId }) + return db.history.removeAsync({ videoId }) } static deleteAll() { - return db.history.remove({}, { multi: true }) + return db.history.removeAsync({}, { multi: true }) } static persist() { - db.history.persistence.compactDatafile() + return db.history.compactDatafileAsync() } } class Profiles { static create(profile) { - return db.profiles.insert(profile) + return db.profiles.insertAsync(profile) } static find() { - return db.profiles.find({}) + return db.profiles.findAsync({}) } static upsert(profile) { - return db.profiles.update({ _id: profile._id }, profile, { upsert: true }) + return db.profiles.updateAsync({ _id: profile._id }, profile, { upsert: true }) } static delete(id) { - return db.profiles.remove({ _id: id }) + return db.profiles.removeAsync({ _id: id }) } static persist() { - db.profiles.persistence.compactDatafile() + return db.profiles.compactDatafileAsync() } } class Playlists { static create(playlists) { - return db.playlists.insert(playlists) + return db.playlists.insertAsync(playlists) } static find() { - return db.playlists.find({}) + return db.playlists.findAsync({}) } static upsertVideoByPlaylistName(playlistName, videoData) { - return db.playlists.update( + return db.playlists.updateAsync( { playlistName }, { $push: { videos: videoData } }, { upsert: true } @@ -117,7 +121,7 @@ class Playlists { } static upsertVideoIdsByPlaylistId(_id, videoIds) { - return db.playlists.update( + return db.playlists.updateAsync( { _id }, { $push: { videos: { $each: videoIds } } }, { upsert: true } @@ -125,11 +129,11 @@ class Playlists { } static delete(_id) { - return db.playlists.remove({ _id, protected: { $ne: true } }) + return db.playlists.removeAsync({ _id, protected: { $ne: true } }) } static deleteVideoIdByPlaylistName(playlistName, videoId) { - return db.playlists.update( + return db.playlists.updateAsync( { playlistName }, { $pull: { videos: { videoId } } }, { upsert: true } @@ -137,7 +141,7 @@ class Playlists { } static deleteVideoIdsByPlaylistName(playlistName, videoIds) { - return db.playlists.update( + return db.playlists.updateAsync( { playlistName }, { $pull: { videos: { $in: videoIds } } }, { upsert: true } @@ -145,7 +149,7 @@ class Playlists { } static deleteAllVideosByPlaylistName(playlistName) { - return db.playlists.update( + return db.playlists.updateAsync( { playlistName }, { $set: { videos: [] } }, { upsert: true } @@ -153,19 +157,34 @@ class Playlists { } static deleteMultiple(ids) { - return db.playlists.remove({ _id: { $in: ids }, protected: { $ne: true } }) + return db.playlists.removeAsync({ _id: { $in: ids }, protected: { $ne: true } }) } static deleteAll() { - return db.playlists.remove({ protected: { $ne: true } }) + return db.playlists.removeAsync({ protected: { $ne: true } }) } + + static persist() { + return db.playlists.compactDatafileAsync() + } +} + +function compactAllDatastores() { + return Promise.allSettled([ + Settings.persist(), + History.persist(), + Profiles.persist(), + Playlists.persist() + ]) } const baseHandlers = { settings: Settings, history: History, profiles: Profiles, - playlists: Playlists + playlists: Playlists, + + compactAllDatastores } export default baseHandlers diff --git a/src/datastores/index.js b/src/datastores/index.js index 644981634e4c..a7038fec9665 100644 --- a/src/datastores/index.js +++ b/src/datastores/index.js @@ -1,4 +1,4 @@ -import Datastore from 'nedb-promises' +import Datastore from '@seald-io/nedb' let dbPath = null @@ -23,9 +23,9 @@ if (process.env.IS_ELECTRON_MAIN) { } const db = {} -db.settings = Datastore.create({ filename: dbPath('settings'), autoload: true }) -db.profiles = Datastore.create({ filename: dbPath('profiles'), autoload: true }) -db.playlists = Datastore.create({ filename: dbPath('playlists'), autoload: true }) -db.history = Datastore.create({ filename: dbPath('history'), autoload: true }) +db.settings = new Datastore({ filename: dbPath('settings'), autoload: true }) +db.profiles = new Datastore({ filename: dbPath('profiles'), autoload: true }) +db.playlists = new Datastore({ filename: dbPath('playlists'), autoload: true }) +db.history = new Datastore({ filename: dbPath('history'), autoload: true }) export default db diff --git a/src/main/index.js b/src/main/index.js index b5f08d62f01c..2dbf2907480d 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -993,28 +993,33 @@ function runApp() { // ************************************************* // app.on('window-all-closed', () => { - // Clear cache and storage if it's the last window - session.defaultSession.clearCache() - session.defaultSession.clearStorageData({ - storages: [ - 'appcache', - 'cookies', - 'filesystem', - 'indexdb', - 'shadercache', - 'websql', - 'serviceworkers', - 'cachestorage' - ] + // Clean up resources (datastores' compaction + Electron cache and storage data clearing) + cleanUpResources().finally(() => { + if (process.platform !== 'darwin') { + app.quit() + } }) - - // For MacOS the app would still "run in background" - // and create new window on event `activate` - if (process.platform !== 'darwin') { - app.quit() - } }) + function cleanUpResources() { + return Promise.allSettled([ + baseHandlers.compactAllDatastores(), + session.defaultSession.clearCache(), + session.defaultSession.clearStorageData({ + storages: [ + 'appcache', + 'cookies', + 'filesystem', + 'indexdb', + 'shadercache', + 'websql', + 'serviceworkers', + 'cachestorage' + ] + }) + ]) + } + // MacOS event // https://www.electronjs.org/docs/latest/api/app#event-activate-macos app.on('activate', () => { diff --git a/src/renderer/components/data-settings/data-settings.js b/src/renderer/components/data-settings/data-settings.js index bb2050fc8f40..da11301b82a3 100644 --- a/src/renderer/components/data-settings/data-settings.js +++ b/src/renderer/components/data-settings/data-settings.js @@ -1137,10 +1137,8 @@ export default defineComponent({ ...mapActions([ 'updateProfile', - 'compactProfiles', 'updateShowProgressBar', 'updateHistory', - 'compactHistory', 'addPlaylist', 'addVideo' ]), diff --git a/src/renderer/store/modules/history.js b/src/renderer/store/modules/history.js index 4ea6e5918335..037c7a6a084b 100644 --- a/src/renderer/store/modules/history.js +++ b/src/renderer/store/modules/history.js @@ -63,10 +63,6 @@ const actions = { } catch (errMessage) { console.error(errMessage) } - }, - - compactHistory(_) { - DBHistoryHandlers.persist() } } diff --git a/src/renderer/store/modules/profiles.js b/src/renderer/store/modules/profiles.js index e330be284809..55069a19a571 100644 --- a/src/renderer/store/modules/profiles.js +++ b/src/renderer/store/modules/profiles.js @@ -142,10 +142,6 @@ const actions = { } }, - compactProfiles(_) { - DBProfileHandlers.persist() - }, - updateActiveProfile({ commit }, id) { commit('setActiveProfile', id) } diff --git a/yarn.lock b/yarn.lock index b7edc2e1baba..bc35877c31dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1379,17 +1379,17 @@ resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33" integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg== -"@seald-io/binary-search-tree@^1.0.2": - version "1.0.2" - resolved "https://registry.yarnpkg.com/@seald-io/binary-search-tree/-/binary-search-tree-1.0.2.tgz#9f0e5cec5e0acf97f1b495f2f6d3476ddb6a94ed" - integrity sha512-+pYGvPFAk7wUR+ONMOlc6A+LUN4kOCFwyPLjyaeS7wVibADPHWYJNYsNtyIAwjF1AXQkuaXElnIc4XjKt55QZA== +"@seald-io/binary-search-tree@^1.0.3": + version "1.0.3" + resolved "https://registry.yarnpkg.com/@seald-io/binary-search-tree/-/binary-search-tree-1.0.3.tgz#165a9a456eaa30d15885b25db83861bcce2c6a74" + integrity sha512-qv3jnwoakeax2razYaMsGI/luWdliBLHTdC6jU55hQt1hcFqzauH/HsBollQ7IR4ySTtYhT+xyHoijpA16C+tA== -"@seald-io/nedb@^3.1.0": - version "3.1.0" - resolved "https://registry.yarnpkg.com/@seald-io/nedb/-/nedb-3.1.0.tgz#6105345a18596f6f9d69f46d7f572efdae32af8e" - integrity sha512-5G0hCQGJjOelOutvW1l4VD581XMhTPxpj1BUaCWTEM2MPXR9TzIr0MKMnEjnTA5nEKfujPyvVW7iF3etm1/gKQ== +"@seald-io/nedb@^4.0.2": + version "4.0.2" + resolved "https://registry.yarnpkg.com/@seald-io/nedb/-/nedb-4.0.2.tgz#44bc5f9b86e44f7434c5af8064cc7f8e079fc3a8" + integrity sha512-gJ91fT1sgh2cLXYVcTSh7khZ8LdemI8+SojCdpZ5wy+DUQ4fSrEwGqOwbdV49NDs2BBO6GeBpSb8CnhG2IW1rw== dependencies: - "@seald-io/binary-search-tree" "^1.0.2" + "@seald-io/binary-search-tree" "^1.0.3" localforage "^1.9.0" util "^0.12.4" @@ -5938,13 +5938,6 @@ natural-compare@^1.4.0: resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc= -nedb-promises@^6.2.1: - version "6.2.1" - resolved "https://registry.yarnpkg.com/nedb-promises/-/nedb-promises-6.2.1.tgz#5280a32fb9c9996e77a93195f595860f88e8454a" - integrity sha512-vurL/Hfsk37mbsjYTu+MKnMUytboKBjWWEA0N35ArCBMdDW5x2BAE7Xny4qnlpR90ZuaI8gdWbbGQ2ZoJSp/FQ== - dependencies: - "@seald-io/nedb" "^3.1.0" - negotiator@0.6.3: version "0.6.3" resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"