Skip to content

Commit 6185464

Browse files
committed
refactor: Use declarative component composition for webgpu-cube
Replace manual component wrapper (app_custom.go) with declarative Guix component composition pattern. The app.gx now properly includes the Controls component using the same pattern as the counter example. Changes: - Update app.gx to declaratively include Controls(WithCommands(commands)) - Create command channel using make() directly (hoisted by codegen) - Remove app_custom.go wrapper entirely - Add minimal app_helpers.go with StartCommandProcessor() method - Update main.go to use generated NewApp() directly - Regenerate app_gen.go with proper component composition The generated code now: - Hoists the commands channel to a struct field - Initializes Controls component in NewApp() - Calls BindApp() on the Controls instance - Renders Controls inline via c.controlsInstance.Render() This follows the Guix design principle that component composition should be handled by the code generator, not manual Go wrappers.
1 parent 92939e8 commit 6185464

5 files changed

Lines changed: 41 additions & 86 deletions

File tree

examples/webgpu-cube/app.gx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package main
22

33
func App() (Component) {
4+
commands := make(chan ControlCommand, 10)
5+
46
Div(
57
Class("webgpu-container"),
6-
TabIndex(0)
8+
TabIndex(0),
9+
OnKeyDown(makeKeyboardHandler(commands))
710
) {
811
Canvas(
912
ID("webgpu-canvas"),
@@ -12,6 +15,7 @@ func App() (Component) {
1215
) {
1316
GPUScene(NewCubeScene(0, 0))
1417
}
18+
Controls(WithCommands(commands))
1519
Div(Class("loading")) {
1620
"Loading WebGPU..."
1721
}

examples/webgpu-cube/app_custom.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

examples/webgpu-cube/app_gen.go

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build js && wasm
2+
3+
package main
4+
5+
// StartCommandProcessor starts the goroutine that processes control commands
6+
func (a *App) StartCommandProcessor() {
7+
go func() {
8+
for cmd := range a.commands {
9+
processControlCommand(cmd)
10+
}
11+
}()
12+
}

examples/webgpu-cube/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,34 @@ func main() {
2222
runtime.WaitForDOMReady(func() {
2323
log("[Go] DOM is ready, initializing app")
2424

25-
// Create and mount app component with controls
26-
log("[Go] Creating AppWithControls...")
27-
appComponent := NewAppWithControls()
28-
log("[Go] AppWithControls created")
25+
// Create app using generated component
26+
log("[Go] Creating App...")
27+
appComponent := NewApp()
28+
log("[Go] App created")
2929

30+
// Start command processor goroutine
31+
log("[Go] Starting command processor...")
32+
appComponent.StartCommandProcessor()
33+
log("[Go] Command processor started")
34+
35+
// Create runtime app
3036
log("[Go] Creating runtime app...")
3137
runtimeApp := runtime.NewApp(appComponent)
3238
log("[Go] Runtime app created")
3339

40+
// Bind app
3441
log("[Go] Binding app...")
3542
appComponent.BindApp(runtimeApp)
3643
log("[Go] App bound")
3744

45+
// Mount
3846
log("[Go] Mounting to #app...")
3947
if err := runtimeApp.Mount("#app"); err != nil {
4048
log("[Go] Mount error:", err)
4149
panic(err)
4250
}
4351

44-
log("[Go] App mounted successfully with integrated controls")
52+
log("[Go] App mounted successfully")
4553
})
4654

4755
// Keep the program running

0 commit comments

Comments
 (0)