bugfix: debounce save quote API call

This commit is contained in:
Settel 2022-10-05 09:38:42 +02:00
parent fa7806e276
commit 62b0f429d7
6 changed files with 30 additions and 14 deletions

View File

@ -25,7 +25,7 @@ const newQuote = ref({} as Quote)
const createNewQuote = () => { const createNewQuote = () => {
showNewQuoteCard.value = false showNewQuoteCard.value = false
newQuote.value = { newQuote.value = {
id: '', id: ':new:' + Date.now(),
quote: '', quote: '',
} }
} }

View File

@ -82,7 +82,7 @@ const editModeEnd = async () => {
const saveQuote = async () => { const saveQuote = async () => {
if (props.quote.quote) { if (props.quote.quote) {
useEngine().saveQuote(props.quote.id || ':new:', props.quote.quote) await useEngine().saveQuote(props.quote.id, props.quote.quote)
} }
await editModeEnd() await editModeEnd()

View File

@ -1,10 +1,11 @@
package application package application
import ( import (
"time"
"sirlab.de/go/knowyt/applicationConfig" "sirlab.de/go/knowyt/applicationConfig"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
"time"
) )
func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) {
@ -13,6 +14,7 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e
users: make(map[string]*user.User), users: make(map[string]*user.User),
games: make(map[string]*game.Game), games: make(map[string]*game.Game),
playerATime: make(map[string]time.Time), playerATime: make(map[string]time.Time),
debounceMap: make(map[string]bool),
} }
if err := app.loadUsers(); err != nil { if err := app.loadUsers(); err != nil {

View File

@ -2,9 +2,10 @@ package application
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"net/http" "net/http"
"path" "path"
"github.com/google/uuid"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -26,7 +27,8 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
quoteText := r.URL.Query().Get("quote") quoteText := r.URL.Query().Get("quote")
quoteId := r.URL.Query().Get("id") quoteId := r.URL.Query().Get("id")
if quoteId == ":new:" { if quoteId[:5] == ":new:" {
if app.debounceQuote(usr.GetId() + ":" + quoteId) {
quoteNewUuid := uuid.NewString() quoteNewUuid := uuid.NewString()
err = gm.CreateQuote( err = gm.CreateQuote(
app.getQuoteFileNameFromId(gm, quoteNewUuid), app.getQuoteFileNameFromId(gm, quoteNewUuid),
@ -34,6 +36,7 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
quoteNewUuid, quoteNewUuid,
quoteText, quoteText,
) )
}
} else { } else {
err = gm.SaveQuote( err = gm.SaveQuote(
app.getQuoteFileNameFromId(gm, quoteId), app.getQuoteFileNameFromId(gm, quoteId),
@ -58,3 +61,11 @@ func (app *Application) getQuoteFileNameFromId(gm *game.Game, id string) string
quoteFileNameShort := id + ".json" quoteFileNameShort := id + ".json"
return path.Join(gameDirName, "quotes", quoteFileNameShort) return path.Join(gameDirName, "quotes", quoteFileNameShort)
} }
func (app *Application) debounceQuote(id string) bool {
if app.debounceMap[id] {
return false
}
app.debounceMap[id] = true
return true
}

View File

@ -1,11 +1,12 @@
package application package application
import ( import (
"sync"
"time"
"sirlab.de/go/knowyt/applicationConfig" "sirlab.de/go/knowyt/applicationConfig"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
"sync"
"time"
) )
type Application struct { type Application struct {
@ -14,4 +15,5 @@ type Application struct {
users map[string]*user.User users map[string]*user.User
games map[string]*game.Game games map[string]*game.Game
playerATime map[string]time.Time playerATime map[string]time.Time
debounceMap map[string]bool
} }

View File

@ -2,6 +2,7 @@ package game
import ( import (
"fmt" "fmt"
"sirlab.de/go/knowyt/quote" "sirlab.de/go/knowyt/quote"
) )