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 = () => {
showNewQuoteCard.value = false
newQuote.value = {
id: '',
id: ':new:' + Date.now(),
quote: '',
}
}

View File

@ -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()

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}

View File

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