Skip to content

Commit 2baf32e

Browse files
committed
docs: add godoc comments
1 parent 6cf3b8a commit 2baf32e

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

v1/dom/dom.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//go:build js && wasm
22

3+
// Package dom provides utilities for updating the browser DOM and binding
4+
// event listeners for WebAssembly components.
35
package dom
46

57
import (
@@ -12,8 +14,12 @@ import (
1214
"github.com/rfwlab/rfw/v1/state"
1315
)
1416

17+
// TemplateHook is an optional callback invoked after a DOM update to allow
18+
// custom processing of the rendered HTML.
1519
var TemplateHook func(componentID, html string)
1620

21+
// UpdateDOM patches the DOM of the specified component with the provided
22+
// HTML string.
1723
func UpdateDOM(componentID string, html string) {
1824
document := js.Document()
1925
var element jst.Value

v1/dom/vdom.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010
"golang.org/x/net/html"
1111
)
1212

13+
// VDOMNode represents a node in a virtual DOM tree derived from an HTML
14+
// template.
1315
type VDOMNode struct {
1416
Tag string
1517
Attributes map[string]string
1618
Children []*VDOMNode
1719
Text string
1820
}
1921

22+
// NewVDOM parses an HTML template into a virtual DOM tree.
2023
func NewVDOM(htmlTemplate string) (*VDOMNode, error) {
2124
reader := strings.NewReader(htmlTemplate)
2225
doc, err := html.Parse(reader)
@@ -111,6 +114,8 @@ func nodeByPath(root jst.Value, path []int) jst.Value {
111114
return node
112115
}
113116

117+
// BindEventListeners attaches registered handlers to nodes within the component
118+
// identified by componentID.
114119
func BindEventListeners(componentID string, root jst.Value) {
115120
if bs, ok := compiledBindings[componentID]; ok {
116121
for _, b := range bs {
@@ -186,6 +191,8 @@ func BindEventListeners(componentID string, root jst.Value) {
186191
}
187192
}
188193

194+
// RemoveEventListeners detaches all event listeners associated with the
195+
// specified component.
189196
func RemoveEventListeners(componentID string) {
190197
if ls, ok := listeners[componentID]; ok {
191198
for _, l := range ls {

v1/router/router.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//go:build js && wasm
22

3+
// Package router implements a simple client-side router for WebAssembly
4+
// applications built with RFW.
35
package router
46

57
import (
@@ -13,8 +15,12 @@ import (
1315
"github.com/rfwlab/rfw/v1/js"
1416
)
1517

18+
// Guard is a function that determines whether navigation to a route is
19+
// permitted based on the provided parameters.
1620
type Guard func(map[string]string) bool
1721

22+
// Route describes a routing rule that maps a path to a component and optional
23+
// guards or child routes.
1824
type Route struct {
1925
Path string
2026
Component func() core.Component
@@ -35,6 +41,7 @@ type route struct {
3541
var routes []route
3642
var currentComponent core.Component
3743

44+
// RegisterRoute adds a new Route to the router's configuration.
3845
func RegisterRoute(r Route) {
3946
routes = append(routes, buildRoute(r))
4047
}
@@ -100,6 +107,8 @@ func matchRoute(routes []route, path string) (*route, []Guard, map[string]string
100107
return nil, nil, nil
101108
}
102109

110+
// Navigate renders the component associated with the specified path if all
111+
// route guards allow it.
103112
func Navigate(path string) {
104113
r, guards, params := matchRoute(routes, path)
105114
if r == nil {
@@ -135,6 +144,7 @@ func Navigate(path string) {
135144
js.History().Call("pushState", nil, "", path)
136145
}
137146

147+
// ExposeNavigate makes the Navigate function accessible from JavaScript.
138148
func ExposeNavigate() {
139149
js.ExposeFunc("goNavigate", func(this jst.Value, args []jst.Value) any {
140150
path := args[0].String()
@@ -143,6 +153,8 @@ func ExposeNavigate() {
143153
})
144154
}
145155

156+
// InitRouter initializes the router and begins listening for navigation
157+
// events.
146158
func InitRouter() {
147159
ch := events.Listen("popstate", js.Window())
148160
go func() {

0 commit comments

Comments
 (0)