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 ¶
- func ApplySnapshot(parsePayload string) error
- func Configure(parseConfig Config)
- func Disable()
- func Enable()
- func Enabled() bool
- func GetSnapshot() (string, error)
- func IsEnabled() booldeprecated
- func Prepare()
- func SchemaChanged(parsePersisted, parseCurrent map[string]any) bool
- func SchemaFingerprint(parseSnapshot map[string]any) string
- type Config
- type SnapshotMigration
- type SnapshotMigrationContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplySnapshot ¶
ApplySnapshot is a no-op on non-browser builds.
func GetSnapshot ¶
GetSnapshot returns an empty payload on non-browser builds.
func SchemaChanged ¶
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 ¶
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.