refactor: Use invoke for ipc to get local account

This commit is contained in:
AkiraFukushima 2020-11-28 23:07:18 +09:00
parent 07a94c96da
commit 5cfc344806
2 changed files with 12 additions and 34 deletions

View File

@ -432,16 +432,9 @@ ipcMain.handle('list-accounts', async (_: IpcMainInvokeEvent) => {
return accounts
})
ipcMain.on('get-local-account', (event: IpcMainEvent, id: string) => {
accountManager
.getAccount(id)
.catch(err => {
log.error(err)
event.sender.send('error-get-local-account', err)
})
.then(account => {
event.sender.send('response-get-local-account', account)
})
ipcMain.handle('get-local-account', async (_: IpcMainInvokeEvent, id: string) => {
const account = await accountManager.getAccount(id)
return account
})
ipcMain.on('update-account', async (event: IpcMainEvent, acct: LocalAccount) => {

View File

@ -122,30 +122,15 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
// Accounts
// -------------------------------------------------
localAccount: async ({ dispatch, commit }, id: string): Promise<LocalAccount> => {
return new Promise((resolve, reject) => {
win.ipcRenderer.send('get-local-account', id)
win.ipcRenderer.once('error-get-local-account', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-get-local-account')
reject(err)
})
win.ipcRenderer.once('response-get-local-account', (_, account: LocalAccount) => {
win.ipcRenderer.removeAllListeners('error-get-local-account')
if (account.username === undefined || account.username === null || account.username === '') {
dispatch('fetchAccount', account)
.then((acct: LocalAccount) => {
commit(MUTATION_TYPES.UPDATE_ACCOUNT, acct)
resolve(acct)
})
.catch(err => {
reject(err)
})
} else {
commit(MUTATION_TYPES.UPDATE_ACCOUNT, account)
resolve(account)
}
})
})
const account: LocalAccount = await win.ipcRenderer.invoke('get-local-account', id)
if (account.username === undefined || account.username === null || account.username === '') {
const acct: LocalAccount = await dispatch('fetchAccount', account)
commit(MUTATION_TYPES.UPDATE_ACCOUNT, acct)
return acct
} else {
commit(MUTATION_TYPES.UPDATE_ACCOUNT, account)
return account
}
},
fetchAccount: (_, account: LocalAccount): Promise<LocalAccount> => {
return new Promise((resolve, reject) => {