1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-01-30 17:15:16 +01:00

Merge pull request #1004 from h3poteto/iss-1002

closes #1002 Cancel requests when suggestion is selected or closed
This commit is contained in:
AkiraFukushima 2019-08-16 22:12:51 +09:00 committed by GitHub
commit 8b2e7eca25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 17 deletions

22
package-lock.json generated
View File

@ -2047,7 +2047,8 @@
"@types/events": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz",
"integrity": "sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g=="
"integrity": "sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==",
"dev": true
},
"@types/glob": {
"version": "7.1.1",
@ -2208,11 +2209,10 @@
}
},
"@types/ws": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-6.0.1.tgz",
"integrity": "sha512-EzH8k1gyZ4xih/MaZTXwT2xOkPiIMSrhQ9b8wrlX88L0T02eYsddatQlwVFlEPyEqV0ChpdpNnE51QPH6NVT4Q==",
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-6.0.2.tgz",
"integrity": "sha512-22XiR1ox9LftTaAtn/c5JCninwc7moaqbkJfaDUb7PkaUitcf5vbTZHdq9dxSMviCm9C3W85rzB8e6yNR70apQ==",
"requires": {
"@types/events": "*",
"@types/node": "*"
}
},
@ -13850,9 +13850,9 @@
"dev": true
},
"megalodon": {
"version": "0.8.2",
"resolved": "https://registry.npmjs.org/megalodon/-/megalodon-0.8.2.tgz",
"integrity": "sha512-s5RY+Fek5Vpb9KnStmyZnrcZhkJB9Pw9DGhWsl7OZwIkShg0wuishdfE9nDRpaejX/3FoGIVE/gJBMFgrpmuPg==",
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/megalodon/-/megalodon-0.9.0.tgz",
"integrity": "sha512-/Ks2QxHhVQeK+Lzlb3x4+kEouXHC+q+1+JL0M2HSalkXE8FEnyBVm77QE5r71qSowkK/0pzjzf2xdUfsb4c9sA==",
"requires": {
"@types/oauth": "^0.9.0",
"@types/request": "^2.47.0",
@ -23069,9 +23069,9 @@
}
},
"ws": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.1.0.tgz",
"integrity": "sha512-Swie2C4fs7CkwlHu1glMePLYJJsWjzhl1vm3ZaLplD0h7OMkZyZ6kLTB/OagiU923bZrPFXuDTeEqaEN4NWG4g==",
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.1.2.tgz",
"integrity": "sha512-gftXq3XI81cJCgkUiAVixA0raD9IVmXqsylCrjRygw4+UOOGzPoxnQ6r/CnVL9i+mDncJo94tSkyrtuuQVBmrg==",
"requires": {
"async-limiter": "^1.0.0"
}

View File

@ -171,7 +171,7 @@
"i18next": "^12.1.0",
"i18next-sync-fs-backend": "^1.1.1",
"lodash": "^4.17.11",
"megalodon": "0.8.2",
"megalodon": "0.9.0",
"moment": "^2.21.0",
"mousetrap": "^1.6.2",
"nedb": "^1.8.0",

View File

@ -27,6 +27,7 @@ export type StatusState = {
openSuggest: boolean
startIndex: number | null
matchWord: string | null
client: Mastodon | null
}
const state = (): StatusState => ({
@ -36,7 +37,8 @@ const state = (): StatusState => ({
filteredEmojis: [],
openSuggest: false,
startIndex: null,
matchWord: null
matchWord: null,
client: null
})
export const MUTATION_TYPES = {
@ -52,7 +54,9 @@ export const MUTATION_TYPES = {
FILTERED_SUGGESTION_FROM_HASHTAGS: 'filteredSuggestionFromHashtags',
FILTERED_SUGGESTION_FROM_ACCOUNTS: 'filteredSuggestionFromAccounts',
FILTERED_SUGGESTION_FROM_EMOJIS: 'filteredSuggestionFromEmojis',
CLEAR_FILTERED_SUGGESTION: 'clearFilteredSuggestion'
CLEAR_FILTERED_SUGGESTION: 'clearFilteredSuggestion',
SET_CLIENT: 'setClient',
CLEAR_CLIENT: 'clearClient'
}
const mutations: MutationTree<StatusState> = {
@ -124,6 +128,12 @@ const mutations: MutationTree<StatusState> = {
},
[MUTATION_TYPES.CLEAR_FILTERED_SUGGESTION]: state => {
state.filteredSuggestion = []
},
[MUTATION_TYPES.SET_CLIENT]: (state, client: Mastodon) => {
state.client = client
},
[MUTATION_TYPES.CLEAR_CLIENT]: state => {
state.client = null
}
}
@ -133,7 +143,8 @@ type WordStart = {
}
const actions: ActionTree<StatusState, RootState> = {
suggestAccount: async ({ commit, rootState }, wordStart: WordStart) => {
suggestAccount: async ({ commit, rootState, dispatch }, wordStart: WordStart) => {
dispatch('cancelRequest')
commit(MUTATION_TYPES.CLEAR_FILTERED_ACCOUNTS)
commit(MUTATION_TYPES.FILTERED_SUGGESTION_FROM_ACCOUNTS)
const { word, start } = wordStart
@ -155,6 +166,7 @@ const actions: ActionTree<StatusState, RootState> = {
}
const searchAPI = async () => {
const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
commit(MUTATION_TYPES.SET_CLIENT, client)
const res: Response<Array<Account>> = await client.get<Array<Account>>('/accounts/search', { q: word, resolve: false })
if (res.data.length === 0) throw new Error('Empty')
commit(MUTATION_TYPES.APPEND_FILTERED_ACCOUNTS, res.data.map(account => account.acct))
@ -170,7 +182,8 @@ const actions: ActionTree<StatusState, RootState> = {
}
await Promise.all([searchCache(), searchAPI()])
},
suggestHashtag: async ({ commit, rootState }, wordStart: WordStart) => {
suggestHashtag: async ({ commit, rootState, dispatch }, wordStart: WordStart) => {
dispatch('cancelRequest')
commit(MUTATION_TYPES.CLEAR_FILTERED_HASHTAGS)
commit(MUTATION_TYPES.FILTERED_SUGGESTION_FROM_HASHTAGS)
const { word, start } = wordStart
@ -192,6 +205,7 @@ const actions: ActionTree<StatusState, RootState> = {
}
const searchAPI = async () => {
const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v2')
commit(MUTATION_TYPES.SET_CLIENT, client)
const res: Response<Results> = await client.get<Results>('/search', { q: word })
if (res.data.hashtags.length === 0) throw new Error('Empty')
commit(MUTATION_TYPES.APPEND_FILTERED_HASHTAGS, res.data.hashtags.map(tag => tag.name))
@ -237,13 +251,20 @@ const actions: ActionTree<StatusState, RootState> = {
commit(MUTATION_TYPES.FILTERED_SUGGESTION_FROM_EMOJIS)
return filtered
},
closeSuggest: ({ commit }) => {
cancelRequest: ({ state }) => {
if (state.client) {
state.client.cancel()
}
},
closeSuggest: ({ commit, dispatch }) => {
dispatch('cancelRequest')
commit(MUTATION_TYPES.CHANGE_OPEN_SUGGEST, false)
commit(MUTATION_TYPES.CHANGE_START_INDEX, null)
commit(MUTATION_TYPES.CHANGE_MATCH_WORD, null)
commit(MUTATION_TYPES.CLEAR_FILTERED_SUGGESTION)
commit(MUTATION_TYPES.CLEAR_FILTERED_ACCOUNTS)
commit(MUTATION_TYPES.CLEAR_FILTERED_HASHTAGS)
commit(MUTATION_TYPES.CLEAR_CLIENT)
}
}