server

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildInit

func BuildInit(ci *island.Info) *gproto.ServerMessage

BuildInit builds the initial VDomMessage for a client connection. On first call (no tree yet), it builds from scratch. On subsequent calls (reconnect), it re-resolves from the live struct and merges into the existing tree to preserve node IDs for other connections.

func BuildIslandInfo

func BuildIslandInfo(comp interface{}, entryHTML string, layers []template.FSLayer, registry map[string]string) *island.Info

BuildIslandInfo takes pre-read entry HTML, expands custom-element partials via the provided layers and registry, validates directives, and parses the VDOM templates. Returns a ready-to-use Info (caller still needs to wire the godom.Island embed and add it to the slice).

entryHTML is the raw HTML (already read from disk, or supplied inline). layers are consulted in order for sibling-file partial lookup. registry is the engine-wide named-partial map (may be nil).

func BuildIslandInfoFromFS

func BuildIslandInfoFromFS(comp interface{}, fsys fs.FS, entryPath string) *island.Info

BuildIslandInfoFromFS is a convenience for the common case where an island's entry HTML lives in a single filesystem with siblings as partials. Reads the entry file and sets up a single FSLayer at the entry's directory.

func BuildUpdate

func BuildUpdate(ci *island.Info) (*gproto.ServerMessage, []string)

BuildUpdate rebuilds the tree from templates, diffs against Tree, and returns a patch message. Returns nil if no changes.

func Cleanup

func Cleanup(comps []*island.Info)

Cleanup calls Cleanup() on any component that implements it, then closes event channels so processor goroutines exit cleanly.

func Run

func Run(cfg EngineConfig) error

Run starts the HTTP server, opens the browser, and blocks forever.

Types

type Client

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

Client is an addressable handle to one connected replica — a single browser tab / WebSocket. It is the foundation that per-connection features build on: connection-scoped environment (Env), targeted JS, and roster/governance.

A Client is per-socket: a reconnecting tab is a *new* Client, not a revived one. Its ID is stable for the life of the connection and unique within the process. Application code obtains Clients from the engine roster; it never constructs them.

func ClientsWith

func ClientsWith(clients []*Client, capability string) []*Client

ClientsWith returns the clients from the given set that have advertised the named capability. Engine.ClientsWith is a thin public wrapper over it.

func (*Client) Call

func (c *Client) Call(method string, args any, out any) error

Call invokes a registered client module function and blocks until the reply, unmarshaling it into out (which may be nil) and surfacing a JS-side throw as a Go error. Because it blocks on a browser round-trip, it MUST be called off the island event loop — e.g. inside a Task closure. If the client disconnects mid-call it returns a "client disconnected" error rather than hanging.

func (*Client) CallAsync

func (c *Client) CallAsync(method string, args any, cb func(result []byte, errMsg string))

CallAsync invokes a registered client module function on this client with JSON-marshaled args and delivers the JSON-encoded reply (or a JS error) to cb. Non-blocking — safe from a handler.

func (*Client) Env

func (c *Client) Env() Env

Env returns a snapshot of the connection's browser environment (timezone, locale, viewport). It is zero until the bridge delivers it just after connect; safe to read from any goroutine.

func (*Client) Eval

func (c *Client) Eval(expr string, cb func(result []byte, errMsg string))

Eval runs a JavaScript expression on this one client (unlike ExecJS, which broadcasts to every tab) and invokes cb once with the JSON-encoded result and an error string. cb may be nil for fire-and-forget. Safe to call from a handler — it does not block.

func (*Client) Has

func (c *Client) Has(capability string) bool

Has reports whether this client has advertised the named module capability. Safe to call from any goroutine.

func (*Client) ID

func (c *Client) ID() string

ID returns the connection's stable, process-unique identifier.

type ClientSource

type ClientSource interface {
	Clients() []*Client
}

ClientSource exposes the live connection roster. The server's connPool implements it, and the public Engine reads through it for Engine.Clients().

type EngineConfig

type EngineConfig interface {
	Islands() []*island.Info
	PluginScripts() map[string][]string
	EmbeddedJS() (bridge, protobufMin, protocol string)
	Mux() *http.ServeMux
	WebSocketPath() string
	GodomScriptPath() string
	Auth() middleware.AuthFunc
	ExecJSDisabled() bool
	GetDisconnectHTML() string
	GetDisconnectBadgeHTML() string
	GetFaviconSVG() string

	// BindClients hands the live connection roster back to the engine at
	// startup so Engine.Clients() can read it. Called once from Run.
	BindClients(ClientSource)

	// ClientModules returns registered client-side JS modules (name → script)
	// to ship in the bundle for targeted client.Call. May be nil/empty.
	ClientModules() map[string]string
}

EngineConfig is the interface that the root Engine satisfies. It replaces the old Config struct, eliminating field duplication between the public Engine and the internal server package.

type Env

type Env struct {
	TimeZone string // IANA timezone, e.g. "Europe/Berlin"
	Locale   string // BCP-47 locale, e.g. "en-GB"
	Viewport Viewport
}

Env is the connection-derived browser environment — the data Go cannot derive on its own. It is captured once, just after the socket connects.

type EnvAware

type EnvAware interface {
	OnConnect(c *Client)
}

EnvAware is implemented by an island that wants to seed state from a new connection's environment. OnConnect runs as an ordinary event on the island's loop (serialized with handlers and refreshes), once per connecting client.

Note: an island has one shared VDOM replicated to every client, so seeding a shared field from c.Env() is last-writer-wins across clients — correct for the single-environment case (one local user, possibly multiple same-machine windows). For divergent per-client environments, scope by page or engine.

type Viewport

type Viewport struct {
	W int
	H int
}

Viewport is the browser viewport size in CSS pixels.

Jump to

Keyboard shortcuts

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