refactor: Use invoke for ipc to list hashtags

This commit is contained in:
AkiraFukushima 2020-11-29 23:17:02 +09:00
parent b3c03b1406
commit 778ed6f642
3 changed files with 11 additions and 35 deletions

View File

@ -994,16 +994,10 @@ ipcMain.handle('save-hashtag', async (_: IpcMainInvokeEvent, tag: string) => {
await hashtags.insertTag(tag) await hashtags.insertTag(tag)
}) })
ipcMain.on('list-hashtags', (event: IpcMainEvent) => { ipcMain.handle('list-hashtags', async (_: IpcMainInvokeEvent) => {
const hashtags = new Hashtags(hashtagsDB) const hashtags = new Hashtags(hashtagsDB)
hashtags const tags = await hashtags.listTags()
.listTags() return tags
.then(tags => {
event.sender.send('response-list-hashtags', tags)
})
.catch(err => {
event.sender.send('error-list-hashtags', err)
})
}) })
ipcMain.on('remove-hashtag', (event: IpcMainEvent, tag: LocalTag) => { ipcMain.on('remove-hashtag', (event: IpcMainEvent, tag: LocalTag) => {

View File

@ -24,19 +24,10 @@ const mutations: MutationTree<ListState> = {
} }
const actions: ActionTree<ListState, RootState> = { const actions: ActionTree<ListState, RootState> = {
listTags: ({ commit }) => { listTags: async ({ commit }) => {
return new Promise((resolve, reject) => { const tags: Array<LocalTag> = await win.ipcRenderer.invoke('list-hashtags')
win.ipcRenderer.once('response-list-hashtags', (_, tags: Array<LocalTag>) => {
win.ipcRenderer.removeAllListeners('error-list-hashtags')
commit(MUTATION_TYPES.UPDATE_TAGS, tags) commit(MUTATION_TYPES.UPDATE_TAGS, tags)
resolve(tags) return tags
})
win.ipcRenderer.once('error-list-hashtags', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-list-hashtags')
reject(err)
})
win.ipcRenderer.send('list-hashtags')
})
}, },
removeTag: ({ dispatch }, tag: LocalTag) => { removeTag: ({ dispatch }, tag: LocalTag) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -149,19 +149,10 @@ const actions: ActionTree<SideMenuState, RootState> = {
commit(MUTATION_TYPES.CHANGE_COLLAPSE, value) commit(MUTATION_TYPES.CHANGE_COLLAPSE, value)
return value return value
}, },
listTags: ({ commit }) => { listTags: async ({ commit }) => {
return new Promise((resolve, reject) => { const tags: Array<LocalTag> = await win.ipcRenderer.invoke('list-hashtags')
win.ipcRenderer.once('response-list-hashtags', (_, tags: Array<LocalTag>) => {
win.ipcRenderer.removeAllListeners('error-list-hashtags')
commit(MUTATION_TYPES.UPDATE_TAGS, tags) commit(MUTATION_TYPES.UPDATE_TAGS, tags)
resolve(tags) return tags
})
win.ipcRenderer.once('error-list-hashtags', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-list-hashtags')
reject(err)
})
win.ipcRenderer.send('list-hashtags')
})
} }
} }