Compare commits

...

2 Commits

Author SHA1 Message Date
70c9f027b8
Added efficient comment loading
All checks were successful
continuous-integration/drone/push Build is passing
2022-03-27 14:06:21 +02:00
75dab086ff
Guestbook loading still broken 2022-03-25 14:02:14 +01:00
2 changed files with 89 additions and 4 deletions

View File

@ -1,6 +1,8 @@
package main
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
@ -21,6 +23,7 @@ type guestbook struct {
website string
message string
lastHash [32]byte
gbModalOpen bool
OnSubmit func(
ctx app.Context,
@ -33,7 +36,8 @@ type guestbook struct {
// TODO: The comments are loaded like 2 or 3 times every time the page is loaded...
func (g *guestbook) OnMount(ctx app.Context) {
g.LoadComments(ctx)
ctx.Handle("guestbook-loadcomments", g.onHandleLoadComments)
ctx.NewAction("guestbook-loadcomments")
}
/*
@ -138,7 +142,8 @@ func (g guestbook) Render() app.UI {
ctx.Dispatch(func(ctx app.Context) {
g.clear()
})
g.LoadComments(ctx)
ctx.NewAction("guestbook-loadcomments")
//g.LoadComments(ctx)
}),
app.If(
g.gbModalOpen,
@ -160,8 +165,66 @@ func (g guestbook) Render() app.UI {
)
}
func (g *guestbook) SmartLoadComments(ctx app.Context) {
var lasthash []byte
err := ctx.LocalStorage().Get("lasthash", &lasthash)
if err != nil {
app.Log(err)
return
}
if lasthash == nil {
fmt.Printf("Program thinks lasthash is empty\n")
g.LoadComments(ctx)
return
}
url := ApiURL + "/commenthash"
ctx.Async(func() {
res, err := http.Get(url)
if err != nil {
app.Log(err)
return
}
defer res.Body.Close()
hash, err := io.ReadAll(res.Body)
if err != nil {
app.Log(err)
return
}
fmt.Printf("hash: %v\n", hash)
fmt.Printf("lasthash: %v\n", lasthash)
// If the hash is different, aka there was an update in the comments
if !bytes.Equal(hash, lasthash) {
fmt.Printf("Hash calculation is different\n")
g.LoadComments(ctx)
return
}
// if the hash is not different, then there is no need to load new comments
jsondata := make([]byte, 0)
err = ctx.LocalStorage().Get("comments", &jsondata)
if err != nil {
app.Log(err)
return
}
fmt.Printf("jsondata: %v\n", jsondata)
ctx.Dispatch(func(ctx app.Context) {
err = json.Unmarshal(jsondata, &g.comments)
if err != nil {
app.Log(err)
return
}
})
return
})
}
func (g *guestbook) LoadComments(ctx app.Context) {
// TODO: maybe you can put this in a localbrowser storage?
fmt.Printf("Called LoadComments()\n")
url := ApiURL + "/comment"
ctx.Async(func() {
res, err := http.Get(url)
@ -183,6 +246,18 @@ func (g *guestbook) LoadComments(ctx app.Context) {
return
}
})
ctx.LocalStorage().Set("comments", jsondata)
// Calculating the hash
fmt.Printf("Calculating the hash from LoadComments\n")
hash := sha256.Sum256(jsondata)
fmt.Printf("hash fresh from calculation: %v\n", hash)
//g.lastHash = hash
err = ctx.LocalStorage().Set("lasthash", []byte(fmt.Sprintf("%x\n", hash)))
if err != nil {
app.Log(err)
return
}
})
}
@ -191,6 +266,13 @@ func (g *guestbook) clear() {
g.message = ""
}
func (g *guestbook) onHandleLoadComments(ctx app.Context, a app.Action) {
g.SmartLoadComments(ctx)
ctx.Dispatch(func(ctx app.Context) {
g.Update()
})
}
type guestbookAlertModal struct {
app.Compo

View File

@ -55,7 +55,10 @@ func (p *Homepage) Render() app.UI {
}
url := ApiURL + "/comment"
ctx.Async(func() {
// This is not Async'ed, because otherwise you run into a race
// condition where you reload the comments before the server had time
// to process the request!
{
req, err := http.Post(url, "application/json", bytes.NewBuffer(jsondata))
if err != nil {
fmt.Printf("err: %v\n", err)
@ -65,7 +68,7 @@ func (p *Homepage) Render() app.UI {
p.Update()
}
defer req.Body.Close()
})
}
},
},
),