IPC: Replace single sync channel for one channel per sync type

This commit is contained in:
Svallinn 2021-08-29 17:07:39 +01:00
parent 672cffdc6b
commit 86d0109406
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
5 changed files with 57 additions and 45 deletions

View File

@ -11,7 +11,10 @@ const IpcChannels = {
STOP_POWER_SAVE_BLOCKER: 'stop-power-save-blocker',
START_POWER_SAVE_BLOCKER: 'start-power-save-blocker',
CREATE_NEW_WINDOW: 'create-new-window',
SYNC_WINDOWS: 'sync-windows',
SYNC_SETTINGS: 'sync-settings',
SYNC_HISTORY: 'sync-history',
SYNC_PROFILES: 'sync-profiles',
SYNC_PLAYLISTS: 'sync-playlists',
OPEN_IN_EXTERNAL_PLAYER: 'open-in-external-player'
}

View File

@ -368,18 +368,32 @@ function runApp() {
createWindow(false)
})
ipcMain.on(IpcChannels.SYNC_WINDOWS, (event, payload) => {
const otherWindows = BrowserWindow.getAllWindows().filter(
(window) => {
return window.webContents.id !== event.sender.id
}
)
for (const window of otherWindows) {
window.webContents.send(IpcChannels.SYNC_WINDOWS, payload)
}
ipcMain.on(IpcChannels.SYNC_SETTINGS, (event, payload) => {
passOntoSiblingWindows(IpcChannels.SYNC_SETTINGS, event, payload)
})
ipcMain.on(IpcChannels.SYNC_HISTORY, (event, payload) => {
passOntoSiblingWindows(IpcChannels.SYNC_HISTORY, event, payload)
})
ipcMain.on(IpcChannels.SYNC_PROFILES, (event, payload) => {
passOntoSiblingWindows(IpcChannels.SYNC_PROFILES, event, payload)
})
ipcMain.on(IpcChannels.SYNC_PLAYLISTS, (event, payload) => {
passOntoSiblingWindows(IpcChannels.SYNC_PLAYLISTS, event, payload)
})
function passOntoSiblingWindows(channel, event, payload) {
const siblingWindows = BrowserWindow.getAllWindows().filter((window) => {
return window.webContents.id !== event.sender.id
})
for (const window of siblingWindows) {
window.webContents.send(channel, payload)
}
}
ipcMain.on(IpcChannels.OPEN_IN_EXTERNAL_PLAYER, (_, payload) => {
const child = cp.spawn(payload.executable, payload.args, { detached: true, stdio: 'ignore' })
child.unref()

View File

@ -142,7 +142,7 @@ export default Vue.extend({
if (this.usingElectron) {
console.log('User is using Electron')
ipcRenderer = require('electron').ipcRenderer
this.setupListenerToSyncWindows()
this.setupListenersToSyncWindows()
this.activateKeyboardShortcuts()
this.openAllLinksExternally()
this.enableOpenUrl()
@ -468,7 +468,7 @@ export default Vue.extend({
'getExternalPlayerCmdArgumentsData',
'fetchInvidiousInstances',
'setRandomCurrentInvidiousInstance',
'setupListenerToSyncWindows'
'setupListenersToSyncWindows'
])
}
})

View File

@ -50,10 +50,7 @@ const actions = {
propagateHistory({ getters: { getUsingElectron: usingElectron } }) {
if (usingElectron) {
const { ipcRenderer } = require('electron')
ipcRenderer.send(IpcChannels.SYNC_WINDOWS, {
type: 'history',
data: state.historyCache
})
ipcRenderer.send(IpcChannels.SYNC_HISTORY, state.historyCache)
}
},

View File

@ -327,34 +327,35 @@ const customActions = {
},
// Should be a root action, but we'll tolerate
setupListenerToSyncWindows: ({ commit, dispatch, getters }) => {
setupListenersToSyncWindows: ({ commit, dispatch, getters }) => {
// Already known to be Electron, no need to check
const { ipcRenderer } = require('electron')
ipcRenderer.on(IpcChannels.SYNC_WINDOWS, (_, payload) => {
const { type, data } = payload
switch (type) {
case 'setting':
// `data` is a single setting => { _id, value }
if (getters.settingHasSideEffects(data._id)) {
dispatch(defaultSideEffectsTriggerId(data._id), data.value)
}
commit(defaultMutationId(data._id), data.value)
break
case 'history':
// `data` is the whole history => Array of history entries
commit('setHistoryCache', data)
break
case 'playlist':
// TODO: Not implemented
break
case 'profile':
// TODO: Not implemented
break
ipcRenderer.on(IpcChannels.SYNC_SETTINGS, (_, payload) => {
// Payload is a single setting => { _id, value }
if (getters.settingHasSideEffects(payload._id)) {
dispatch(defaultSideEffectsTriggerId(payload._id), payload.value)
}
commit(defaultMutationId(payload._id), payload.value)
})
ipcRenderer.on(IpcChannels.SYNC_HISTORY, (_, payload) => {
// Payload is the entire history => array of history entries
/*
* FIXME: Depending on how big the history size is, this could
* potentially become slow. While I'm sure there isn't a problem
* with this approach, I believe this can be done better.
*/
commit('setHistoryCache', payload)
})
ipcRenderer.on(IpcChannels.SYNC_PROFILES, (_, __) => {
// TODO: Not implemented
})
ipcRenderer.on(IpcChannels.SYNC_PLAYLISTS, (_, __) => {
// TODO: Not implemented
})
}
}
@ -418,10 +419,7 @@ for (const settingId of Object.keys(state)) {
const { ipcRenderer } = require('electron')
// Propagate settings to all other existing windows
ipcRenderer.send(IpcChannels.SYNC_WINDOWS, {
type: 'setting',
data: { _id: settingId, value: value }
})
ipcRenderer.send(IpcChannels.SYNC_SETTINGS, { _id: settingId, value: value })
}
}
}