Documentation
¶
Overview ¶
Package goscript is the Go side of Tern's experimental <script lang="go">: component logic written in Go, compiled to WebAssembly for the browser, with Svelte-style runes.
<script lang="go">
import "github.com/Unfield/Tern/goscript"
func Setup(c *goscript.Component) {
count := goscript.State(c, "count", 0)
goscript.Derived(c, "doubled", func() int { return count.Get() * 2 })
c.Func("increment", func() { count.Set(count.Get() + 1) })
}
</script>
<button onclick={increment}>{count} × 2 = {doubled}</button>
Names given to State, Derived and Func become variables in the component's markup. Setup also runs natively at build time, so server rendering and the first paint show the initial state before the WebAssembly module loads. Enable it with "experimental": {"goScript": true} in tern.json. See docs/experimental-go-script.md.
Index ¶
- func Effect(c *Component, fn func())
- func InitialState(id string, props []byte, locals map[string]any) (map[string]json.RawMessage, error)
- func Register(id string, setup func(*Component))
- func Registered(id string) bool
- func Run()
- type Component
- type Event
- type GlobalSignal
- type Instance
- type Signal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Effect ¶
func Effect(c *Component, fn func())
Effect runs fn now and again whenever a value it reads changes.
func InitialState ¶
func InitialState(id string, props []byte, locals map[string]any) (map[string]json.RawMessage, error)
InitialState runs a component's Setup with props and returns what it published, for server rendering.
func Registered ¶
Registered reports whether a component ID is registered.
Types ¶
type Component ¶
type Component struct {
// contains filtered or unexported fields
}
Component is one mounted instance of a component.
func (*Component) Func ¶
Func makes fn callable from the markup under name, such as onclick={increment}. Its arguments are decoded from the JavaScript values passed to it; DOM events arrive as an Event.
func (*Component) Locals ¶
Locals returns request values a live component was mounted with, such as the signed-in user from ctx.Locals; nil in the browser.
func (*Component) OnDestroy ¶
func (c *Component) OnDestroy(fn func())
OnDestroy runs fn when the component goes away: when its page closes or navigates, or its live connection ends.
type Event ¶
type Event struct {
Type string `json:"type"`
Value string `json:"value"` // the target's value, for inputs
Checked bool `json:"checked"` // for checkboxes
Key string `json:"key"` // for keyboard events
}
Event is a DOM event passed to a Func.
type GlobalSignal ¶
type GlobalSignal[T any] struct { // contains filtered or unexported fields }
GlobalSignal is a value shared by every instance of every component, such as a counter all visitors see. Shown in a component's markup with Show. It is meant for live components, which run on the server and so share memory; in the browser each page has its own.
func (*GlobalSignal[T]) Get ¶
func (g *GlobalSignal[T]) Get() T
Get returns the value, subscribing the running Derived or Effect to it.
func (*GlobalSignal[T]) Set ¶
func (g *GlobalSignal[T]) Set(v T)
Set changes the value for everyone.
func (*GlobalSignal[T]) Show ¶
func (g *GlobalSignal[T]) Show(c *Component, name string)
Show publishes the shared value in c's markup under name, and keeps it up to date there.
func (*GlobalSignal[T]) Update ¶
func (g *GlobalSignal[T]) Update(fn func(T) T)
Update sets the value to fn of the current one.
type Instance ¶
type Instance struct {
// contains filtered or unexported fields
}
Instance is a mounted live component.
func MountLive ¶
func MountLive(id string, props []byte, locals map[string]any, emit func(name string, value []byte)) (*Instance, error)
MountLive mounts a component on the server for a live connection; emit receives every value it publishes. Tern's live endpoint calls it.
type Signal ¶
type Signal[T any] struct { // contains filtered or unexported fields }
Signal is a reactive value; see State.
func Derived ¶
Derived declares a value computed from others, recomputed when they change. With a name, it is shown in the markup too.
func State ¶
State declares a reactive value, shown in the markup under name. Reading it with Get inside a Derived or Effect subscribes to it.
func (*Signal[T]) Peek ¶
func (s *Signal[T]) Peek() T
Peek returns the value without subscribing to it.