refactor: simplify signals; small optimizations
This commit is contained in:
102
signal.go
102
signal.go
@@ -2,7 +2,6 @@ package via
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -16,8 +15,7 @@ import (
|
||||
// reactively on an html element.
|
||||
type signal struct {
|
||||
id string
|
||||
v reflect.Value
|
||||
t reflect.Type
|
||||
val any
|
||||
changed bool
|
||||
err error
|
||||
}
|
||||
@@ -45,7 +43,7 @@ func (s *signal) Bind() h.H {
|
||||
return h.Data("bind", s.id)
|
||||
}
|
||||
|
||||
// Text binds the signal value to an html element as text.
|
||||
// Text binds the signal value to an html span element as text.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
@@ -57,130 +55,52 @@ func (s *signal) Text() h.H {
|
||||
// SetValue updates the signal’s 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) {
|
||||
val := reflect.ValueOf(v)
|
||||
typ := reflect.TypeOf(v)
|
||||
if typ != s.t {
|
||||
s.err = fmt.Errorf("expected type '%s', got '%s'", s.t.String(), typ.String())
|
||||
return
|
||||
}
|
||||
s.v = val
|
||||
s.val = v
|
||||
s.changed = true
|
||||
s.err = nil
|
||||
}
|
||||
|
||||
// String return the signal value as a string.
|
||||
func (s *signal) String() string {
|
||||
return fmt.Sprintf("%v", s.v)
|
||||
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 {
|
||||
switch s.v.Kind() {
|
||||
case reflect.Bool:
|
||||
return s.v.Bool()
|
||||
case reflect.String:
|
||||
val := strings.ToLower(s.v.String())
|
||||
return val == "true" || val == "1" || val == "yes" || val == "on"
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return s.v.Int() != 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return s.v.Float() != 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
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 {
|
||||
if n, err := strconv.Atoi(s.v.String()); err == nil {
|
||||
if n, err := strconv.Atoi(s.String()); err == nil {
|
||||
return n
|
||||
}
|
||||
if s.v.CanInt() {
|
||||
return int(s.v.Int())
|
||||
}
|
||||
if s.v.CanFloat() {
|
||||
return int(s.v.Float())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Int64 tries to read the signal value as an int64.
|
||||
// Returns the value or 0 on failure.
|
||||
func (s *signal) Int64() int64 {
|
||||
if n, err := strconv.ParseInt(s.v.String(), 10, 64); err == nil {
|
||||
if n, err := strconv.ParseInt(s.String(), 10, 64); err == nil {
|
||||
return n
|
||||
}
|
||||
if s.v.CanInt() {
|
||||
return s.v.Int()
|
||||
}
|
||||
if s.v.CanFloat() {
|
||||
return int64(s.v.Float())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Uint64 tries to read the signal value as an uint64.
|
||||
// Returns the value or 0 on failure.
|
||||
func (s *signal) Uint64() uint64 {
|
||||
if n, err := strconv.ParseUint(s.v.String(), 10, 64); err == nil {
|
||||
return n
|
||||
}
|
||||
if s.v.CanUint() {
|
||||
return s.v.Uint()
|
||||
}
|
||||
if s.v.CanFloat() {
|
||||
return uint64(s.v.Float())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Float64 tries to read the signal value as a float64.
|
||||
// Returns the value or 0.0 on failure.
|
||||
func (s *signal) Float64() float64 {
|
||||
if n, err := strconv.ParseFloat(s.v.String(), 64); err == nil {
|
||||
func (s *signal) Float() float64 {
|
||||
if n, err := strconv.ParseFloat(s.String(), 64); err == nil {
|
||||
return n
|
||||
}
|
||||
if s.v.CanFloat() {
|
||||
return s.v.Float()
|
||||
}
|
||||
if s.v.CanInt() {
|
||||
return float64(s.v.Int())
|
||||
}
|
||||
return 0.0
|
||||
}
|
||||
|
||||
// Complex128 tries to read the signal value as a complex128.
|
||||
// Returns the value or 0 on failure.
|
||||
func (s *signal) Complex128() complex128 {
|
||||
if s.v.Kind() == reflect.Complex128 {
|
||||
return s.v.Complex()
|
||||
}
|
||||
if s.v.Kind() == reflect.String {
|
||||
if n, err := strconv.ParseComplex(s.v.String(), 128); err == nil {
|
||||
return n
|
||||
}
|
||||
}
|
||||
if s.v.CanFloat() {
|
||||
return complex(s.v.Float(), 0)
|
||||
}
|
||||
if s.v.CanInt() {
|
||||
return complex(float64(s.v.Int()), 0)
|
||||
}
|
||||
return complex(0, 0)
|
||||
}
|
||||
|
||||
// Bytes tries to read the signal value as a []byte
|
||||
// Returns the value or an empty []byte on failure.
|
||||
func (s *signal) Bytes() []byte {
|
||||
switch s.v.Kind() {
|
||||
case reflect.Slice:
|
||||
if s.v.Type().Elem().Kind() == reflect.Uint8 {
|
||||
return s.v.Bytes()
|
||||
}
|
||||
case reflect.String:
|
||||
return []byte(s.v.String())
|
||||
}
|
||||
return make([]byte, 0)
|
||||
return []byte(s.String())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user