Skip to content

Commit

Permalink
update render with fifo, fix inlining in fifo
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Feb 24, 2025
1 parent 758809b commit edd3665
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
36 changes: 20 additions & 16 deletions examples/gno.land/p/moul/fifo/fifo.gno
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,27 @@ func (l *List) Prepend(entry interface{}) {

if l.size < l.maxSize {
l.size++
} else {
// Remove last element by traversing to second-to-last
if l.size == 1 {
// Special case: if size is 1, just update both pointers
l.head = newNode
l.tail = newNode
newNode.next = nil
} else {
// Find second-to-last node
current := l.head
for current.next != l.tail {
current = current.next
}
current.next = nil
l.tail = current
}
return
}

// Remove last element by traversing to second-to-last
if l.size == 1 {
// Special case: if size is 1, just update both pointers
l.head = newNode
l.tail = newNode
newNode.next = nil
return

}

// Find second-to-last node
current := l.head
for current.next != l.tail {
current = current.next
}
current.next = nil
l.tail = current

}

// Append adds a new entry at the end of the list. If the list exceeds maxSize,
Expand Down
35 changes: 10 additions & 25 deletions examples/gno.land/r/gnoland/users/v1/render.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package users

import (
"std"
"strconv"

"gno.land/p/demo/avl/pager"
"gno.land/p/demo/ufmt"
"gno.land/p/moul/md"
"gno.land/p/moul/realmpath"
Expand All @@ -18,14 +16,14 @@ func Render(path string) string {
req := realmpath.Parse(path)

if req.Path == "" {
return renderHomePage(path)
return renderHomePage()
}

// Otherwise, render the user page
return renderUserPage(req.Path)
}

func renderHomePage(path string) string {
func renderHomePage() string {
var out string

out += "# gno.land user registry\n"
Expand All @@ -39,29 +37,16 @@ func renderHomePage(path string) string {

out += renderIntroParagraph()

out += md.H2("User list")

p := pager.NewPager(susers.GetReadOnlyNameStore(), 20, false)
page := p.MustGetPageByPath(path)
for _, item := range page.Items {
if item.Value == nil {
continue
}

data := item.Value.(*susers.UserData)

// Skip previous names
if item.Key != data.Name() {
continue
}

out += ufmt.Sprintf("- User [%s](/r/gnoland/users/v1:%s)\n", md.Bold(item.Key), item.Key)
out += md.H2("Latest registrations")
entries := latestUsers.Entries()
if len(entries) == 0 {
out += "No registered users."
}

out += "\n"
out += page.Picker()
out += "\n\n"
out += "Page " + strconv.Itoa(page.PageNumber) + " of " + strconv.Itoa(page.TotalPages) + "\n\n"
for i := len(entries) - 1; i >= 0; i-- {
user := entries[i].(string)
out += ufmt.Sprintf("- User [%s](/r/gnoland/users/v1:%s)\n", md.Bold(user), user)
}

return out
}
Expand Down
9 changes: 8 additions & 1 deletion examples/gno.land/r/gnoland/users/v1/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"regexp"
"std"

"gno.land/p/moul/fifo"
susers "gno.land/r/sys/users"
)

const (
reValidUsername = "^[a-z]{3}[_a-z0-9]{0,14}[0-9]{3}$"
)

var reUsername = regexp.MustCompile(reValidUsername)
var (
latestUsers = fifo.New(10) // Save the latest 10 users for rendering purposes
reUsername = regexp.MustCompile(reValidUsername)
)

// Register registers a new username for the caller
// A valid username must start with a minimum of 3 letters,
Expand All @@ -34,5 +38,8 @@ func Register(username string) error {
return err
}

latestUsers.Append(username)
std.Emit("Registeration", "address", registrant.String(), "name", username)

return nil
}

0 comments on commit edd3665

Please sign in to comment.