From 62b0f429d7be8038b24ac1bcceefb7b35bbeb13d Mon Sep 17 00:00:00 2001 From: Settel Date: Wed, 5 Oct 2022 09:38:42 +0200 Subject: [PATCH] bugfix: debounce save quote API call --- client/src/components/CollectQuotes.vue | 2 +- client/src/components/QuoteCard.vue | 2 +- server/src/application/application.go | 4 +++- server/src/application/saveQuote.go | 29 +++++++++++++++++-------- server/src/application/struct.go | 6 +++-- server/src/game/saveQuote.go | 1 + 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/client/src/components/CollectQuotes.vue b/client/src/components/CollectQuotes.vue index 63a4fca..a447c95 100644 --- a/client/src/components/CollectQuotes.vue +++ b/client/src/components/CollectQuotes.vue @@ -25,7 +25,7 @@ const newQuote = ref({} as Quote) const createNewQuote = () => { showNewQuoteCard.value = false newQuote.value = { - id: '', + id: ':new:' + Date.now(), quote: '', } } diff --git a/client/src/components/QuoteCard.vue b/client/src/components/QuoteCard.vue index d688709..5ac1912 100644 --- a/client/src/components/QuoteCard.vue +++ b/client/src/components/QuoteCard.vue @@ -82,7 +82,7 @@ const editModeEnd = async () => { const saveQuote = async () => { if (props.quote.quote) { - useEngine().saveQuote(props.quote.id || ':new:', props.quote.quote) + await useEngine().saveQuote(props.quote.id, props.quote.quote) } await editModeEnd() diff --git a/server/src/application/application.go b/server/src/application/application.go index ce19c7f..c7c26bf 100644 --- a/server/src/application/application.go +++ b/server/src/application/application.go @@ -1,10 +1,11 @@ package application import ( + "time" + "sirlab.de/go/knowyt/applicationConfig" "sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/user" - "time" ) func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { @@ -13,6 +14,7 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e users: make(map[string]*user.User), games: make(map[string]*game.Game), playerATime: make(map[string]time.Time), + debounceMap: make(map[string]bool), } if err := app.loadUsers(); err != nil { diff --git a/server/src/application/saveQuote.go b/server/src/application/saveQuote.go index 5415ffc..ab3f49c 100644 --- a/server/src/application/saveQuote.go +++ b/server/src/application/saveQuote.go @@ -2,9 +2,10 @@ package application import ( "fmt" - "github.com/google/uuid" "net/http" "path" + + "github.com/google/uuid" "sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/user" ) @@ -26,14 +27,16 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http quoteText := r.URL.Query().Get("quote") quoteId := r.URL.Query().Get("id") - if quoteId == ":new:" { - quoteNewUuid := uuid.NewString() - err = gm.CreateQuote( - app.getQuoteFileNameFromId(gm, quoteNewUuid), - usr.GetId(), - quoteNewUuid, - quoteText, - ) + if quoteId[:5] == ":new:" { + if app.debounceQuote(usr.GetId() + ":" + quoteId) { + quoteNewUuid := uuid.NewString() + err = gm.CreateQuote( + app.getQuoteFileNameFromId(gm, quoteNewUuid), + usr.GetId(), + quoteNewUuid, + quoteText, + ) + } } else { err = gm.SaveQuote( app.getQuoteFileNameFromId(gm, quoteId), @@ -58,3 +61,11 @@ func (app *Application) getQuoteFileNameFromId(gm *game.Game, id string) string quoteFileNameShort := id + ".json" 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 +} diff --git a/server/src/application/struct.go b/server/src/application/struct.go index 2d22639..9fd1e79 100644 --- a/server/src/application/struct.go +++ b/server/src/application/struct.go @@ -1,11 +1,12 @@ package application import ( + "sync" + "time" + "sirlab.de/go/knowyt/applicationConfig" "sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/user" - "sync" - "time" ) type Application struct { @@ -14,4 +15,5 @@ type Application struct { users map[string]*user.User games map[string]*game.Game playerATime map[string]time.Time + debounceMap map[string]bool } diff --git a/server/src/game/saveQuote.go b/server/src/game/saveQuote.go index 89b8ce5..3adb2fb 100644 --- a/server/src/game/saveQuote.go +++ b/server/src/game/saveQuote.go @@ -2,6 +2,7 @@ package game import ( "fmt" + "sirlab.de/go/knowyt/quote" )