add WASM blazor-like thing
This commit is contained in:
31
go/cmd/examples/go-wasm-web/wasm/main.go
Normal file
31
go/cmd/examples/go-wasm-web/wasm/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
//go:build js && wasm
|
||||
|
||||
// Command wasm is the client entry point: it wires client capabilities into the
|
||||
// (generated) routes, then hydrates the server-rendered DOM or renders fresh.
|
||||
package main
|
||||
|
||||
import (
|
||||
"gowasmweb/app"
|
||||
"kjol/vdom"
|
||||
"kjol/wasmruntime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Collect the signals created during setup so their values can be preserved
|
||||
// across an in-place hot swap. On a normal load RestoreState is nil (fresh
|
||||
// state); after a dev hot-reload it carries the previous instance's values,
|
||||
// which NewSignal restores by creation order.
|
||||
collector := vdom.BeginCollect(wasmruntime.RestoreState())
|
||||
router := wasmruntime.NewRouter()
|
||||
deps := app.Deps{Path: router.Path, Navigate: router.Navigate}
|
||||
routes := app.Routes(deps)
|
||||
vdom.EndCollect() // signals created later (during renders) aren't preserved
|
||||
wasmruntime.PreserveState(collector)
|
||||
|
||||
render := func() *vdom.VNode { return app.Shell(deps, routes) }
|
||||
if wasmruntime.HasServerContent() {
|
||||
wasmruntime.Hydrate(render) // static route: adopt the server-rendered DOM
|
||||
} else {
|
||||
wasmruntime.Run(render) // client-rendered route
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user