Skip to content

Commit 86cbf14

Browse files
committed
Fix channel range statement in generated code
The RangeStmt was using Value for the loop variable instead of Key, causing the Go formatter to omit the variable declaration. For channels, `for val := range ch` requires val to be in the Key position, not Value. This fixes the generated code from: for range c.CounterChannel { c.currentCounterChannel = val // val undefined } To: for val := range c.CounterChannel { c.currentCounterChannel = val // val correctly defined }
1 parent 4f344e0 commit 86cbf14

2 files changed

Lines changed: 32 additions & 102 deletions

File tree

examples/counter/counter_gen.go

Lines changed: 30 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,61 @@
1-
// Code generated by guix. DO NOT EDIT.
2-
package main
1+
package // Code generated by guix. DO NOT EDIT.
2+
main
33

44
import (
55
"fmt"
6-
"strconv"
7-
"syscall/js"
8-
96
"github.com/gaarutyunov/guix/pkg/runtime"
7+
"syscall/js"
108
)
119

12-
// Counter component that displays a counter value
13-
type Counter struct {
14-
app *runtime.App
15-
currentValue int
16-
}
17-
18-
func NewCounter() *Counter {
19-
return &Counter{
20-
currentValue: 0,
21-
}
22-
}
23-
24-
func (c *Counter) BindApp(app *runtime.App) {
25-
c.app = app
26-
}
27-
28-
func (c *Counter) Render() *runtime.VNode {
29-
return runtime.Div(
30-
runtime.Class("counter-display"),
31-
runtime.Span(
32-
runtime.Class("counter-value"),
33-
runtime.Text(fmt.Sprintf("Counter: %d", c.currentValue)),
34-
),
35-
)
36-
}
37-
38-
func (c *Counter) Mount(parent js.Value) {
39-
runtime.Mount(c.Render(), parent)
40-
}
41-
42-
func (c *Counter) Unmount() {
10+
type CounterProps struct {
11+
CounterChannel chan int
4312
}
13+
type CounterOption func(*Counter)
4414

45-
func (c *Counter) Update() {
46-
if c.app != nil {
47-
c.app.Update()
15+
func WithCounterChannel(v chan int) CounterOption {
16+
return func(c *Counter) {
17+
c.CounterChannel = v
4818
}
4919
}
5020

51-
// App component
52-
type App struct {
53-
app *runtime.App
54-
counter chan int
55-
counterComp *Counter
56-
currentValue int
21+
type Counter struct {
22+
app *runtime.App
23+
CounterChannel chan int
24+
currentCounterChannel int
5725
}
5826

59-
func NewApp() *App {
60-
app := &App{
61-
counter: make(chan int, 10), // Buffered channel
62-
currentValue: 0,
27+
func NewCounter(opts ...CounterOption) *Counter {
28+
c := &Counter{}
29+
for _, opt := range opts {
30+
opt(c)
6331
}
64-
// Create Counter component once
65-
app.counterComp = NewCounter()
66-
return app
32+
return c
6733
}
68-
69-
func (c *App) BindApp(app *runtime.App) {
34+
func (c *Counter) BindApp(app *runtime.App) {
7035
c.app = app
71-
c.counterComp.BindApp(app)
72-
// Start listening to the counter channel
73-
c.startChannelListener()
36+
if c.CounterChannel != nil {
37+
c.startCounterChannelListener()
38+
}
7439
}
75-
76-
// startChannelListener listens for values on the channel and triggers re-renders
77-
func (c *App) startChannelListener() {
40+
func (c *Counter) startCounterChannelListener() {
7841
go func() {
79-
for val := range c.counter {
80-
c.currentValue = val
81-
c.counterComp.currentValue = val
42+
for val := range c.CounterChannel {
43+
c.currentCounterChannel = val
8244
if c.app != nil {
8345
c.app.Update()
8446
}
8547
}
8648
}()
8749
}
88-
89-
func (c *App) Render() *runtime.VNode {
90-
return runtime.Div(
91-
runtime.Class("app-container"),
92-
runtime.H1(runtime.Text("Counter Example")),
93-
runtime.Div(
94-
runtime.Class("input-group"),
95-
runtime.Input(
96-
runtime.Type("number"),
97-
runtime.Placeholder("Enter a number"),
98-
runtime.Value(fmt.Sprint(c.currentValue)),
99-
runtime.ID("counter-input"),
100-
runtime.OnInput(func(e runtime.Event) {
101-
// Parse the input value and send to channel
102-
value := e.Target.Value
103-
if n, err := strconv.Atoi(value); err == nil {
104-
// Non-blocking send
105-
select {
106-
case c.counter <- n:
107-
default:
108-
// Channel full, skip
109-
}
110-
}
111-
}),
112-
),
113-
),
114-
c.counterComp.Render(),
115-
)
50+
func (c *Counter) Render() *runtime.VNode {
51+
return runtime.Div(runtime.Class("counter-display"), runtime.Span(runtime.Class("counter-value"), runtime.Text("Counter: "+fmt.Sprint(c.currentCounterChannel))))
11652
}
117-
118-
func (c *App) Mount(parent js.Value) {
53+
func (c *Counter) Mount(parent js.Value) {
11954
runtime.Mount(c.Render(), parent)
12055
}
121-
122-
func (c *App) Unmount() {
123-
if c.counter != nil {
124-
close(c.counter)
125-
}
56+
func (c *Counter) Unmount() {
12657
}
127-
128-
func (c *App) Update() {
58+
func (c *Counter) Update() {
12959
if c.app != nil {
13060
c.app.Update()
13161
}

pkg/codegen/codegen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,8 @@ func (g *Generator) generateChannelListenerMethods(comp *guixast.Component) []as
11271127
List: []ast.Stmt{
11281128
// for val := range c.ChannelName { ... }
11291129
&ast.RangeStmt{
1130-
Key: nil,
1131-
Value: ast.NewIdent("val"),
1130+
Key: ast.NewIdent("val"),
1131+
Value: nil,
11321132
Tok: token.DEFINE,
11331133
X: &ast.SelectorExpr{
11341134
X: ast.NewIdent("c"),

0 commit comments

Comments
 (0)