fix: maplibre reactive signal bugs and stale signal re-push (#3)
Some checks failed
CI / Build and Test (push) Failing after 36s

This commit was merged in pull request #3.
This commit is contained in:
2026-02-20 18:31:27 +00:00
parent 47dcab8fea
commit c0f4782f2b
12 changed files with 732 additions and 146 deletions

View File

@@ -9,27 +9,27 @@ import (
)
// Signal represents a value that is reactive in the browser. Signals
// are synct with the server right before an action triggers.
// are synced with the server right before an action triggers.
//
// Use Bind() to connect a signal to an input and Text() to display it
// reactively on an html element.
type signal struct {
type Signal struct {
id string
val any
changed bool
err error
}
// ID returns the signal ID
func (s *signal) ID() string {
// ID returns the signal ID.
func (s *Signal) ID() string {
return s.id
}
// Err returns a signal error or nil if it contains no error.
//
// It is useful to check for errors after updating signals with
// dinamic values.
func (s *signal) Err() error {
// dynamic values.
func (s *Signal) Err() error {
return s.err
}
@@ -39,7 +39,7 @@ func (s *signal) Err() error {
// Example:
//
// h.Input(h.Type("number"), mysignal.Bind())
func (s *signal) Bind() h.H {
func (s *Signal) Bind() h.H {
return h.Data("bind", s.id)
}
@@ -48,33 +48,33 @@ func (s *signal) Bind() h.H {
// Example:
//
// h.Div(mysignal.Text())
func (s *signal) Text() h.H {
func (s *Signal) Text() h.H {
return h.Span(h.Data("text", "$"+s.id))
}
// SetValue updates the signals value and marks it for synchronization with the browser.
// The change will be propagated to the browser using *Context.Sync() or *Context.SyncSignals().
func (s *signal) SetValue(v any) {
func (s *Signal) SetValue(v any) {
s.val = v
s.changed = true
s.err = nil
}
// String return the signal value as a string.
func (s *signal) String() string {
// String returns the signal value as a string.
func (s *Signal) String() string {
return fmt.Sprintf("%v", s.val)
}
// Bool tries to read the signal value as a bool.
// Returns the value or false on failure.
func (s *signal) Bool() bool {
func (s *Signal) Bool() bool {
val := strings.ToLower(s.String())
return val == "true" || val == "1" || val == "yes" || val == "on"
}
// Int tries to read the signal value as an int.
// Returns the value or 0 on failure.
func (s *signal) Int() int {
func (s *Signal) Int() int {
if n, err := strconv.Atoi(s.String()); err == nil {
return n
}