goscript

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 6 Imported by: 0

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

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 Register

func Register(id string, setup func(*Component))

Register makes a component's Setup available; generated code calls it.

func Registered

func Registered(id string) bool

Registered reports whether a component ID is registered.

func Run

func Run()

Run mounts every component with empty props, outside the browser, and prints each one's initial state as JSON. tern uses it at build time so server rendering and the first paint show real values.

Types

type Component

type Component struct {
	// contains filtered or unexported fields
}

Component is one mounted instance of a component.

func (*Component) Func

func (c *Component) Func(name string, fn any)

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

func (c *Component) Locals() map[string]any

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.

func (*Component) Props

func (c *Component) Props(dst any) error

Props decodes the component's props into dst. Props are read when the component mounts.

func (*Component) Rendering

func (c *Component) Rendering() bool

Rendering reports whether Setup runs only to compute the initial state, for server rendering or the build. Skip side effects then, such as counting a visitor.

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 Global

func Global[T any](initial T) *GlobalSignal[T]

Global declares a shared value.

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.

func (*Instance) Call

func (i *Instance) Call(name string, args []byte) ([]byte, error)

Call runs one of the component's Funcs with JSON arguments.

func (*Instance) Destroy

func (i *Instance) Destroy()

Destroy stops the component.

type Signal

type Signal[T any] struct {
	// contains filtered or unexported fields
}

Signal is a reactive value; see State.

func Derived

func Derived[T any](c *Component, name string, fn func() T) *Signal[T]

Derived declares a value computed from others, recomputed when they change. With a name, it is shown in the markup too.

func State

func State[T any](c *Component, name string, initial T) *Signal[T]

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]) Get

func (s *Signal[T]) Get() T

Get returns the value.

func (*Signal[T]) Peek

func (s *Signal[T]) Peek() T

Peek returns the value without subscribing to it.

func (*Signal[T]) Set

func (s *Signal[T]) Set(v T)

Set changes the value, updating the markup and everything derived from it.

func (*Signal[T]) Update

func (s *Signal[T]) Update(fn func(T) T)

Update sets the value to fn of the current one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL