refactor: Use invoke for ipc to update preferences

This commit is contained in:
AkiraFukushima 2020-11-29 01:47:38 +09:00
parent 070995b465
commit 1b5c5f9c62
4 changed files with 59 additions and 173 deletions

View File

@ -467,18 +467,17 @@ ipcMain.handle('remove-all-accounts', async (_: IpcMainInvokeEvent) => {
await accountManager.removeAll()
})
ipcMain.on('change-auto-launch', (event: IpcMainEvent, enable: boolean) => {
ipcMain.handle('change-auto-launch', async (_: IpcMainInvokeEvent, enable: boolean) => {
if (launcher) {
launcher.isEnabled().then(enabled => {
if (!enabled && enable && launcher) {
launcher.enable()
} else if (enabled && !enable && launcher) {
launcher.disable()
}
event.sender.send('response-change-auto-launch', enable)
})
const enabled = await launcher.isEnabled()
if (!enabled && enable && launcher) {
launcher.enable()
} else if (enabled && !enable && launcher) {
launcher.disable()
}
return enable
} else {
event.sender.send('response-change-auto-launch', false)
return false
}
})
@ -918,16 +917,10 @@ ipcMain.on('get-preferences', async (event: IpcMainEvent) => {
event.sender.send('response-get-preferences', conf)
})
ipcMain.on('update-preferences', (event: IpcMainEvent, data: any) => {
ipcMain.handle('update-preferences', async (_: IpcMainInvokeEvent, data: any) => {
const preferences = new Preferences(preferencesDBPath)
preferences
.update(data)
.then(conf => {
event.sender.send('response-update-preferences', conf)
})
.catch(err => {
event.sender.send('error-update-preferences', err)
})
const conf = await preferences.update(data)
return conf
})
ipcMain.on('change-collapse', (_event: IpcMainEvent, value: boolean) => {

View File

@ -72,91 +72,51 @@ const actions: ActionTree<AppearanceState, RootState> = {
win.ipcRenderer.send('list-fonts')
})
},
updateTheme: ({ dispatch, commit, state }, themeKey: string) => {
updateTheme: async ({ dispatch, commit, state }, themeKey: string) => {
const newAppearance: Appearance = Object.assign({}, state.appearance, {
theme: themeKey
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
win.ipcRenderer.send('update-preferences', config)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
},
updateFontSize: ({ dispatch, commit, state }, fontSize: number) => {
updateFontSize: async ({ dispatch, commit, state }, fontSize: number) => {
const newAppearance: Appearance = Object.assign({}, state.appearance, {
fontSize: fontSize
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
win.ipcRenderer.send('update-preferences', config)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
},
updateDisplayNameStyle: ({ dispatch, commit, state }, value: number) => {
updateDisplayNameStyle: async ({ dispatch, commit, state }, value: number) => {
const newAppearance: Appearance = Object.assign({}, state.appearance, {
displayNameStyle: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
dispatch('App/loadPreferences', null, { root: true })
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
resolve(conf.appearance)
})
win.ipcRenderer.send('update-preferences', config)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
dispatch('App/loadPreferences', null, { root: true })
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
},
updateTimeFormat: ({ dispatch, commit, state }, value: number) => {
updateTimeFormat: async ({ dispatch, commit, state }, value: number) => {
const newAppearance: Appearance = Object.assign({}, state.appearance, {
timeFormat: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
dispatch('App/loadPreferences', null, { root: true })
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
resolve(conf.appearance)
})
win.ipcRenderer.send('update-preferences', config)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
dispatch('App/loadPreferences', null, { root: true })
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
},
updateCustomThemeColor: ({ dispatch, state, commit }, value: object) => {
updateCustomThemeColor: async ({ dispatch, state, commit }, value: object) => {
const newCustom: ThemeColorType = Object.assign({}, state.appearance.customThemeColor, value)
const newAppearance: Appearance = Object.assign({}, state.appearance, {
customThemeColor: newCustom
@ -164,61 +124,31 @@ const actions: ActionTree<AppearanceState, RootState> = {
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
win.ipcRenderer.send('update-preferences', config)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
},
updateFont: ({ dispatch, state, commit }, value: string) => {
updateFont: async ({ dispatch, state, commit }, value: string) => {
const newAppearance: Appearance = Object.assign({}, state.appearance, {
font: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
win.ipcRenderer.send('update-preferences', config)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
},
updateTootPadding: ({ dispatch, state, commit }, value: number) => {
updateTootPadding: async ({ dispatch, state, commit }, value: number) => {
const newAppearance: Appearance = Object.assign({}, state.appearance, {
tootPadding: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
win.ipcRenderer.send('update-preferences', config)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
}
}

View File

@ -62,7 +62,7 @@ const actions: ActionTree<GeneralState, RootState> = {
})
})
},
updateSound: ({ commit, state }, sound: object) => {
updateSound: async ({ commit, state }, sound: object) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newSound: Sound = Object.assign({}, state.general.sound, sound)
const newGeneral: General = Object.assign({}, state.general, {
@ -71,22 +71,12 @@ const actions: ActionTree<GeneralState, RootState> = {
const config = {
general: newGeneral
}
return new Promise((resolve, reject) => {
win.ipcRenderer.send('update-preferences', config)
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
commit(MUTATION_TYPES.CHANGE_LOADING, false)
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
commit(MUTATION_TYPES.CHANGE_LOADING, false)
resolve(conf)
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config).finally(() => {
commit(MUTATION_TYPES.CHANGE_LOADING, false)
})
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
},
updateTimeline: ({ commit, state, dispatch }, timeline: object) => {
updateTimeline: async ({ commit, state, dispatch }, timeline: object) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newTimeline: Timeline = Object.assign({}, state.general.timeline, timeline)
const newGeneral: General = Object.assign({}, state.general, {
@ -95,23 +85,13 @@ const actions: ActionTree<GeneralState, RootState> = {
const config = {
general: newGeneral
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
commit(MUTATION_TYPES.CHANGE_LOADING, false)
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
commit(MUTATION_TYPES.CHANGE_LOADING, false)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf)
})
win.ipcRenderer.send('update-preferences', config)
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config).finally(() => {
commit(MUTATION_TYPES.CHANGE_LOADING, false)
})
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
dispatch('App/loadPreferences', null, { root: true })
},
updateOther: ({ commit, state, dispatch }, other: {}) => {
updateOther: async ({ commit, state, dispatch }, other: {}) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newOther: Other = Object.assign({}, state.general.other, other)
const newGeneral: General = Object.assign({}, state.general, {
@ -120,24 +100,12 @@ const actions: ActionTree<GeneralState, RootState> = {
const config = {
general: newGeneral
}
return new Promise((resolve, reject) => {
win.ipcRenderer.once('response-change-auto-launch', () => {
win.ipcRenderer.once('error-update-preferences', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-preferences')
commit(MUTATION_TYPES.CHANGE_LOADING, false)
reject(err)
})
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
win.ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
commit(MUTATION_TYPES.CHANGE_LOADING, false)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf)
})
win.ipcRenderer.send('update-preferences', config)
})
win.ipcRenderer.send('change-auto-launch', newOther.launch)
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config).finally(() => {
commit(MUTATION_TYPES.CHANGE_LOADING, false)
})
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
dispatch('App/loadPreferences', null, { root: true })
await win.ipcRenderer.invoke('change-auto-launch', newOther.launch)
}
}

View File

@ -48,7 +48,7 @@ const actions: ActionTree<NotificationState, RootState> = {
})
})
},
updateNotify: ({ commit, state, dispatch }, notify: object) => {
updateNotify: async ({ commit, state, dispatch }, notify: object) => {
const newNotify: Notify = Object.assign({}, state.notification.notify, notify)
const newNotification: Notification = Object.assign({}, state.notification, {
notify: newNotify
@ -56,14 +56,9 @@ const actions: ActionTree<NotificationState, RootState> = {
const config = {
notification: newNotification
}
return new Promise(resolve => {
win.ipcRenderer.send('update-preferences', config)
win.ipcRenderer.once('response-update-preferences', (_, conf: BaseConfig) => {
commit(MUTATION_TYPES.UPDATE_NOTIFICATION, conf.notification)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.notification)
})
})
const conf: BaseConfig = await win.ipcRenderer.invoke('update-preferences', config)
commit(MUTATION_TYPES.UPDATE_NOTIFICATION, conf.notification)
dispatch('App/loadPreferences', null, { root: true })
}
}