hotreload

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 5 Imported by: 0

README

GWC | Hotreload Library

GoWebComponents (GWC)

High-Level Overview

The hotreload library provides development-time hot reload plumbing and production gating behavior for GWC runtimes.

Public APIs

github.com/monstercameron/GoWebComponents/hotreload (package hotreload)
  • Functions: ApplySnapshot, Configure, Disable, Enable, Enabled, GetSnapshot, IsEnabled, Prepare
  • Types: Config, SnapshotMigration, SnapshotMigrationContext
  • Variables: none
  • Constants: none

Subfiles And Purpose

  • doc.go - Package-level Go documentation
  • hotreload.go - Core implementation for hotreload
  • hotreload_native.go - Native (non-WASM) implementation for hotreload
  • hotreload_native_test.go - Tests for hotreload_native behavior
  • hotreload_production.go - Production-mode implementation or helpers
  • hotreload_production_mode.go - Production-mode implementation or helpers
  • hotreload_production_wasm_test.go - Tests for hotreload_production_wasm behavior
  • hotreload_wasm.go - WebAssembly-specific implementation for hotreload
  • hotreload_wasm_test.go - Tests for hotreload_wasm behavior
  • production_mode_production_wasm_test.go - Tests for production_mode_production_wasm behavior
  • production_mode_wasm_test.go - Tests for production_mode_wasm behavior

File Map

hotreload/
|-- doc.go
|-- hotreload.go
|-- hotreload_native.go
|-- hotreload_native_test.go
|-- hotreload_production.go
|-- hotreload_production_mode.go
|-- hotreload_production_wasm_test.go
|-- hotreload_wasm.go
|-- hotreload_wasm_test.go
|-- production_mode_production_wasm_test.go
\-- production_mode_wasm_test.go

Documentation

Overview

Package hotreload provides the public state-preserving hot reload API for GoWebComponents development builds.

Typical app usage is:

hotreload.Enable()

Or, when only selected atom ids should be exported:

hotreload.Configure(hotreload.Config{AtomIDs: []string{"session", "draft"}})

To force an intentional state reset after an edit, change ResetKey:

hotreload.Configure(hotreload.Config{ResetKey: "layout-v2"})

To preserve compatible state across an app refactor, raise SnapshotVersion and provide SnapshotMigrations for old browser snapshots:

hotreload.Configure(hotreload.Config{
	SnapshotVersion: 2,
	SnapshotMigrations: []hotreload.SnapshotMigration{{
		FromVersion: 1,
		ToVersion:   2,
		ComponentPathAliases: map[string]string{
			"old/path": "new/path",
		},
	}},
})

The standard tools/dev.ps1 and tools/dev.sh dev server wrappers use this bridge automatically when the application enables it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplySnapshot

func ApplySnapshot(parsePayload string) error

ApplySnapshot is a no-op on non-browser builds.

func Configure

func Configure(parseConfig Config)

Configure is unavailable on non-browser builds.

func Disable

func Disable()

Disable is unavailable on non-browser builds.

func Enable

func Enable()

Enable installs the default hot reload bridge for development builds.

func Enabled

func Enabled() bool

Enabled always reports false on non-browser builds.

func GetSnapshot

func GetSnapshot() (string, error)

GetSnapshot returns an empty payload on non-browser builds.

func IsEnabled deprecated

func IsEnabled() bool

IsEnabled reports whether hot reload is enabled.

Deprecated: use Enabled.

func Prepare

func Prepare()

Prepare is a no-op on non-browser builds.

func SchemaChanged

func SchemaChanged(parsePersisted, parseCurrent map[string]any) bool

SchemaChanged reports whether a persisted snapshot's shape differs from the current code's shape. This is the C1 ghost-bug guard: when it is true, a state-preserving hot reload must surface a visible "state reset" rather than silently restoring the persisted snapshot into a mismatched type (which produces stale-state ghost bugs). When false, the snapshot is shape-compatible and safe to restore.

func SchemaFingerprint

func SchemaFingerprint(parseSnapshot map[string]any) string

SchemaFingerprint returns a stable fingerprint of a state snapshot's SHAPE — its sorted keys and each value's concrete type — independent of the actual values. Two snapshots with the same keys and per-key types produce the same fingerprint; adding/removing a key or changing a field's type changes it.

Types

type Config

type Config struct {
	// AtomIDs limits exported atom state to the provided ids.
	// When empty, all exported atom state is included.
	AtomIDs []string

	// ResetKey opts into explicit snapshot invalidation. When the reset key
	// changes between builds, previously exported local and shared state is
	// discarded instead of being restored into the new module.
	ResetKey string

	// SnapshotVersion names the app-owned hot reload snapshot schema.
	//
	// It defaults to 1. Increment it when refactors require atom-state or
	// component identity migrations before an old browser snapshot can be
	// restored into the new module.
	SnapshotVersion int

	// SnapshotMigrations migrates old app-owned snapshot schemas into
	// SnapshotVersion before restore. Migrations are applied in version order
	// and may rewrite shared atom state plus component path or identity aliases.
	SnapshotMigrations []SnapshotMigration
}

Config controls how the hot reload bridge captures state.

type SnapshotMigration

type SnapshotMigration struct {
	FromVersion int
	ToVersion   int

	// MigrateState rewrites the shared atom snapshot for this version step.
	MigrateState func(SnapshotMigrationContext) (state.Snapshot, error)

	// ComponentPathAliases rewrites stable component paths captured by the
	// runtime. Use it when a component moved but should keep compatible local
	// hook state.
	ComponentPathAliases map[string]string

	// ComponentIdentityAliases rewrites component identity strings captured in
	// signatures and identity trails. Use it when a component was renamed or
	// moved but its hook shape remains compatible.
	ComponentIdentityAliases map[string]string
}

SnapshotMigration rewrites one app-owned hot reload snapshot schema version.

type SnapshotMigrationContext

type SnapshotMigrationContext struct {
	FromVersion int
	ToVersion   int
	State       state.Snapshot
}

SnapshotMigrationContext is passed to SnapshotMigration.MigrateState.

Jump to

Keyboard shortcuts

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