types
packageAPI reference for the types
package.
Imports
(5)Int
Int is an integer signal.
type Int state.Signal[int]
String
String is a string signal.
type String state.Signal[string]
Bool
Bool is a boolean signal.
type Bool state.Signal[bool]
Float
Float is a float64 signal.
type Float state.Signal[float64]
Any
Any is an untyped signal.
type Any state.Signal[any]
Store
Store aliases the state store.
type Store state.Store
View
View aliases an HTML component.
type View core.HTMLComponent
Comp
Comp aliases the component interface.
type Comp core.Component
Slice
Slice is a slice signal.
type Slice struct
NewSlice
NewSlice creates a slice signal.
Parameters
Returns
func NewSlice[T any](v ...[]T) *Slice[T]
{
var initial []T
if len(v) > 0 {
initial = v[0]
}
return &Slice[T]{Signal: state.NewSignal(initial)}
}
Map
Map is a map signal.
type Map struct
NewMap
NewMap creates a map signal.
Parameters
Returns
func NewMap[K comparable, V any](v ...map[K]V) *Map[K, V]
{
var initial map[K]V
if len(v) > 0 {
initial = v[0]
}
return &Map[K, V]{Signal: state.NewSignal(initial)}
}
HInt
HInt is a host-backed integer signal.
type HInt struct
HString
HString is a host-backed string signal.
type HString struct
HBool
HBool is a host-backed boolean signal.
type HBool struct
HFloat
HFloat is a host-backed float signal.
type HFloat struct
HAny
HAny is a host-backed untyped signal.
type HAny struct
HSlice
HSlice is a host-backed slice signal.
type HSlice struct
HMap
HMap is a host-backed map signal.
type HMap struct
Ref
Ref stores a DOM reference.
type Ref struct
Methods
Set updates the referenced node.
Parameters
func (*Ref) Set(v js.Value)
{ r.node = v }
IsNil reports whether no node is referenced.
Returns
func (*Ref) IsNil() bool
{ return r.node.IsNull() || r.node.IsUndefined() }
Fields
| Name | Type | Description |
|---|---|---|
| node | js.Value |
NewRef
NewRef creates an empty DOM reference.
Returns
func NewRef() *Ref
{
return &Ref{node: js.Null()}
}
Prop
Prop stores a component property.
type Prop struct
Fields
| Name | Type | Description |
|---|---|---|
| value | T |
NewProp
NewProp creates a component property.
Parameters
Returns
func NewProp[T any](v T) *Prop[T]
{
return &Prop[T]{value: v}
}
NewInt
NewInt creates an integer signal.
Parameters
Returns
func NewInt(v int) *Int
{ return state.NewSignal(v) }
NewString
NewString creates a string signal.
Parameters
Returns
func NewString(v string) *String
{ return state.NewSignal(v) }
NewBool
NewBool creates a boolean signal.
Parameters
Returns
func NewBool(v bool) *Bool
{ return state.NewSignal(v) }
NewFloat
NewFloat creates a float signal.
Parameters
Returns
func NewFloat(v float64) *Float
{ return state.NewSignal(v) }
NewAny
NewAny creates an untyped signal.
Parameters
Returns
func NewAny(v any) *Any
{ return state.NewSignal(v) }
Inject
Inject marks a dependency injection field.
type Inject struct
Fields
| Name | Type | Description |
|---|---|---|
| Value | T |
History
History records store snapshots for undo and redo.
type History struct
Methods
Bind attaches a store to the history.
Parameters
func (*History) Bind(s *state.Store)
{
h.store = s
}
Undo restores the previous snapshot.
func (*History) Undo()
{
if h.store == nil || h.cursor <= 0 {
return
}
h.cursor--
snap := h.entries[h.cursor]
for k, v := range snap {
h.store.Set(k, v)
}
}
Redo restores the next snapshot.
func (*History) Redo()
{
if h.store == nil || h.cursor >= len(h.entries)-1 {
return
}
h.cursor++
snap := h.entries[h.cursor]
for k, v := range snap {
h.store.Set(k, v)
}
}
Snapshot records the current store state.
func (*History) Snapshot()
{
if h.store == nil {
return
}
snap := h.store.Snapshot()
if snap == nil {
snap = map[string]any{}
}
if h.cursor < len(h.entries)-1 {
h.entries = h.entries[:h.cursor+1]
}
h.entries = append(h.entries, snap)
if len(h.entries) > h.max {
h.entries = h.entries[len(h.entries)-h.max:]
}
h.cursor = len(h.entries) - 1
}
Fields
| Name | Type | Description |
|---|---|---|
| store | *state.Store | |
| max | int | |
| cursor | int | |
| entries | []map[string]any |
NewHistory
NewHistory creates a bounded store history.
Parameters
Returns
func NewHistory(limit int) *History
{
return &History{max: limit, entries: make([]map[string]any, 0)}
}
signalStub
type signalStub struct
Fields
| Name | Type | Description |
|---|---|---|
| value | T | |
| onChangeMu | sync.Mutex | |
| onChange | []func(T) | |
| ch | chan T | |
| chCreated | bool |
Int
Int is an integer signal.
type Int signalStub[int]
String
String is a string signal.
type String signalStub[string]
Bool
Bool is a boolean signal.
type Bool signalStub[bool]
Float
Float is a float64 signal.
type Float signalStub[float64]
Any
Any is an untyped signal.
type Any signalStub[any]
Store
Store aliases the non-WASM store placeholder.
type Store core.Component
View
View aliases the non-WASM component view.
type View core.HTMLComponent
Comp
Comp aliases the non-WASM component interface.
type Comp core.Component
Slice
Slice is a slice signal.
type Slice struct
Map
Map is a map signal.
type Map struct
HInt
HInt is a host-backed integer signal placeholder.
type HInt struct
HString
HString is a host-backed string signal placeholder.
type HString struct
HBool
HBool is a host-backed boolean signal placeholder.
type HBool struct
HFloat
HFloat is a host-backed float signal placeholder.
type HFloat struct
HAny
HAny is a host-backed untyped signal placeholder.
type HAny struct
HSlice
HSlice is a host-backed slice signal placeholder.
type HSlice struct
HMap
HMap is a host-backed map signal placeholder.
type HMap struct
Ref
Ref is a DOM reference placeholder.
type Ref struct
Methods
Set updates the referenced node.
Parameters
func (*Ref) Set(v js.Value)
{ r.node = v }
IsNil reports whether no node is referenced.
Returns
func (*Ref) IsNil() bool
{ return r.node.IsNull() || r.node.IsUndefined() }
Prop
Prop stores a component property.
type Prop struct
Fields
| Name | Type | Description |
|---|---|---|
| value | T |
Inject
Inject marks a dependency injection field.
type Inject struct
Fields
| Name | Type | Description |
|---|---|---|
| Value | T |
History
History is a non-WASM history placeholder.
type History struct
Methods
Bind attaches a store to the history.
Parameters
func (*History) Bind(s *state.Store)
{
h.store = s
}
Undo restores the previous snapshot.
func (*History) Undo()
{
if h.store == nil || h.cursor <= 0 {
return
}
h.cursor--
snap := h.entries[h.cursor]
for k, v := range snap {
h.store.Set(k, v)
}
}
Redo restores the next snapshot.
func (*History) Redo()
{
if h.store == nil || h.cursor >= len(h.entries)-1 {
return
}
h.cursor++
snap := h.entries[h.cursor]
for k, v := range snap {
h.store.Set(k, v)
}
}
Snapshot records the current store state.
func (*History) Snapshot()
{
if h.store == nil {
return
}
snap := h.store.Snapshot()
if snap == nil {
snap = map[string]any{}
}
if h.cursor < len(h.entries)-1 {
h.entries = h.entries[:h.cursor+1]
}
h.entries = append(h.entries, snap)
if len(h.entries) > h.max {
h.entries = h.entries[len(h.entries)-h.max:]
}
h.cursor = len(h.entries) - 1
}
Fields
| Name | Type | Description |
|---|---|---|
| max | int |
NewHistory
NewHistory creates a history placeholder.
Parameters
Returns
func NewHistory(limit int) *History
{ return &History{max: limit} }
NewInt
NewInt creates an integer signal.
Parameters
Returns
func NewInt(v int) *Int
{ return &signalStub[int]{value: v} }
NewString
NewString creates a string signal.
Parameters
Returns
func NewString(v string) *String
{ return &signalStub[string]{value: v} }
NewBool
NewBool creates a boolean signal.
Parameters
Returns
func NewBool(v bool) *Bool
{ return &signalStub[bool]{value: v} }
NewFloat
NewFloat creates a float signal.
Parameters
Returns
func NewFloat(v float64) *Float
{ return &signalStub[float64]{value: v} }
NewAny
NewAny creates an untyped signal.
Parameters
Returns
func NewAny(v any) *Any
{ return &signalStub[any]{value: v} }
NewSlice
NewSlice creates a slice signal.
Parameters
Returns
func NewSlice[T any](v ...[]T) *Slice[T]
{
var initial []T
if len(v) > 0 {
initial = v[0]
}
return &Slice[T]{signalStub: &signalStub[[]T]{value: initial}}
}
NewMap
NewMap creates a map signal.
Parameters
Returns
func NewMap[K comparable, V any](v ...map[K]V) *Map[K, V]
{
var initial map[K]V
if len(v) > 0 {
initial = v[0]
}
return &Map[K, V]{signalStub: &signalStub[map[K]V]{value: initial}}
}
NewRef
NewRef creates a DOM reference placeholder.
Returns
func NewRef() *Ref
{ return &Ref{} }
NewProp
NewProp creates a component property.
Parameters
Returns
func NewProp[T any](v T) *Prop[T]
{ return &Prop[T]{value: v} }
TestTypedSignals
Parameters
func TestTypedSignals(t *testing.T)
{
count := NewInt(1)
if count.Get() != 1 || count.Read() != 1 {
t.Fatalf("unexpected initial int signal value")
}
var seen int
sub := count.OnChange(func(v int) { seen = v })
count.Set(2)
if seen != 2 {
t.Fatalf("expected OnChange value 2, got %d", seen)
}
sub.Stop()
count.Set(3)
if seen != 2 {
t.Fatalf("expected stopped subscription to remain 2, got %d", seen)
}
}
TestSignalChannelAndHostConversion
Parameters
func TestSignalChannelAndHostConversion(t *testing.T)
{
count := NewInt(0)
ch := count.Channel()
count.SetFromHost(float64(7))
if count.Get() != 7 {
t.Fatalf("expected host float64 converted to int 7, got %d", count.Get())
}
select {
case got := <-ch:
if got != 7 {
t.Fatalf("expected channel value 7, got %d", got)
}
default:
t.Fatalf("expected channel update")
}
}
TestCollectionsPropsAndInject
Parameters
func TestCollectionsPropsAndInject(t *testing.T)
{
items := NewSlice([]string{"a"})
items.Set(append(items.Get(), "b"))
if got := items.Get(); len(got) != 2 || got[1] != "b" {
t.Fatalf("unexpected slice value: %v", got)
}
m := NewMap(map[string]int{"a": 1})
if m.Get()["a"] != 1 {
t.Fatalf("unexpected map value: %v", m.Get())
}
prop := NewProp("old")
prop.Set("new")
if prop.Get() != "new" {
t.Fatalf("unexpected prop value: %s", prop.Get())
}
inject := Inject[int]{Value: 42}
if inject.Value != 42 {
t.Fatalf("unexpected inject value: %d", inject.Value)
}
}