Skip to content

Commit

Permalink
Merge branch 'FreeTubeApp:development' into community-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lamemakes authored Aug 11, 2023
2 parents 409c286 + 079417d commit d02591b
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 89 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
87 changes: 53 additions & 34 deletions src/datastores/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -24,148 +28,163 @@ 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 }
)
}

static upsertVideoIdsByPlaylistId(_id, videoIds) {
return db.playlists.update(
return db.playlists.updateAsync(
{ _id },
{ $push: { videos: { $each: videoIds } } },
{ upsert: true }
)
}

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 }
)
}

static deleteVideoIdsByPlaylistName(playlistName, videoIds) {
return db.playlists.update(
return db.playlists.updateAsync(
{ playlistName },
{ $pull: { videos: { $in: videoIds } } },
{ upsert: true }
)
}

static deleteAllVideosByPlaylistName(playlistName) {
return db.playlists.update(
return db.playlists.updateAsync(
{ playlistName },
{ $set: { videos: [] } },
{ upsert: true }
)
}

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
10 changes: 5 additions & 5 deletions src/datastores/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Datastore from 'nedb-promises'
import Datastore from '@seald-io/nedb'

let dbPath = null

Expand All @@ -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
43 changes: 24 additions & 19 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/renderer/components/data-settings/data-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,10 +1137,8 @@ export default defineComponent({

...mapActions([
'updateProfile',
'compactProfiles',
'updateShowProgressBar',
'updateHistory',
'compactHistory',
'addPlaylist',
'addVideo'
]),
Expand Down
4 changes: 0 additions & 4 deletions src/renderer/store/modules/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ const actions = {
} catch (errMessage) {
console.error(errMessage)
}
},

compactHistory(_) {
DBHistoryHandlers.persist()
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/renderer/store/modules/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ const actions = {
}
},

compactProfiles(_) {
DBProfileHandlers.persist()
},

updateActiveProfile({ commit }, id) {
commit('setActiveProfile', id)
}
Expand Down
Loading

0 comments on commit d02591b

Please sign in to comment.