Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: try avoid request api at same time #337

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ import jsYaml from "js-yaml";
import {ref} from "vue";
import Hotkey from "@/components/hotkey.vue";
import ExtraNetworksPopup from "@/components/extraNetworksPopup.vue";
import waitTick from "@/utils/waitTick";

export default {
name: 'App',
Expand Down Expand Up @@ -370,7 +371,7 @@ export default {
this.gradioAPI.setData('languageCode', val).then(data => {
}).catch(err => {
})
this.loadGroupTags()
waitTick.addWaitTick(() => this.loadGroupTags())
},
immediate: false,
},
Expand Down Expand Up @@ -782,7 +783,7 @@ export default {
}
if (data.tagCompleteFile !== null) {
this.tagCompleteFile = data.tagCompleteFile
this.$nextTick(() => {
waitTick.addWaitTick(() => {
this.$refs.translateSetting.getCSV(this.tagCompleteFile)
})
} else {
Expand Down Expand Up @@ -870,7 +871,8 @@ export default {
})

this.handlePaste()
this.loadGroupTags()

waitTick.addWaitTick(() => this.loadGroupTags())

/*this.gradioAPI.getVersion().then(res => {
this.version = res.version
Expand Down Expand Up @@ -905,7 +907,7 @@ export default {
})
},
loadGroupTags() {
this.gradioAPI.getGroupTags(this.languageCode).then(data => {
return this.gradioAPI.getGroupTags(this.languageCode).then(data => {
if (!data || data === '') {
this.groupTags = []
} else {
Expand Down
7 changes: 4 additions & 3 deletions src/src/components/favorite.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import common from "@/utils/common";

import LanguageMixin from "@/mixins/languageMixin";
import IconSvg from "@/components/iconSvg.vue";
import waitTick from '@/utils/waitTick';

export default {
components: {IconSvg},
Expand Down Expand Up @@ -122,7 +123,7 @@ export default {
emits: ['use'],
mounted() {
this.favorites.forEach(item => {
this.getFavorites(item.key)
waitTick.addWaitTick(() => this.getFavorites(item.key))
})
},
methods: {
Expand All @@ -134,7 +135,7 @@ export default {
let favoriteItem = this.favorites.find(item => item.key === favoriteKey)
if (!favoriteItem) return
this.loading = true
this.gradioAPI.getFavorites(favoriteKey).then(res => {
return this.gradioAPI.getFavorites(favoriteKey).then(res => {
if(res && res.length > 0){
// 倒序
res.reverse()
Expand Down Expand Up @@ -301,4 +302,4 @@ export default {
},
}
}
</script>
</script>
4 changes: 2 additions & 2 deletions src/src/components/history.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default {
let historyItem = this.histories.find(item => item.key === historyKey)
if (!historyItem) return
this.loading = true
this.gradioAPI.getHistories(historyKey).then(res => {
return this.gradioAPI.getHistories(historyKey).then(res => {
if (res && res.length > 0) {
// 倒序
res.reverse()
Expand Down Expand Up @@ -282,4 +282,4 @@ export default {
},
}
}
</script>
</script>
4 changes: 2 additions & 2 deletions src/src/components/translateSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ Github: {{name}}`
if (this.tagCompleteFilesLoading) return
this.tagCompleteFilesLoading = true
this.tagCompleteFiles = []
this.gradioAPI.getCSVs().then(res => {
return this.gradioAPI.getCSVs().then(res => {
this.tagCompleteFilesLoading = false
if (!res || res.length <= 0) return
this.tagCompleteFiles.push({
Expand Down Expand Up @@ -375,4 +375,4 @@ Github: {{name}}`
},
},
}
</script>
</script>
41 changes: 41 additions & 0 deletions src/src/utils/waitTick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { nextTick } from 'vue'

export default {
/**
* @types Function[]
*/
waitTickList: [],
startingTick: false,
/**
*
* @param cb Function
*/
addWaitTick(cb) {
this.waitTickList.push(cb)
return this.startWaitTick()
},

async execatueWaitTick()
{
if (this.startingTick) return

this.startingTick = true

while (this.waitTickList.length) {
const cb = this.waitTickList.shift()
await nextTick().then(cb)
}

this.startingTick = false
},

async startWaitTick() {
if (!this.startingTick)
{
return nextTick(() => this.execatueWaitTick().catch(e => {
this.startingTick = false
return this.startWaitTick()
}))
}
}
}