Skip to content

Commit 45f94c3

Browse files
committed
useState and useAtom both now return 3 values....
1 parent 7bdad4b commit 45f94c3

2 files changed

Lines changed: 12 additions & 25 deletions

File tree

tsunami/demo/todo/main-todo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ var TodoList = app.DefineComponent(AppClient, "TodoList",
112112
var App = app.DefineComponent(AppClient, "App",
113113
func(ctx context.Context, _ any) any {
114114
// Multiple state hooks example
115-
todos, setTodos := vdom.UseState(ctx, []Todo{
115+
todos, setTodos, _ := vdom.UseState(ctx, []Todo{
116116
{Id: 1, Text: "Learn VDOM", Completed: false},
117117
{Id: 2, Text: "Build a todo app", Completed: false},
118118
})
119-
nextId, setNextId := vdom.UseState(ctx, 3)
120-
inputText, setInputText := vdom.UseState(ctx, "")
119+
nextId, setNextId, _ := vdom.UseState(ctx, 3)
120+
inputText, setInputText, _ := vdom.UseState(ctx, "")
121121

122122
// Event handlers modifying multiple pieces of state
123123
addTodo := func() {

tsunami/vdom/vdom.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -287,26 +287,7 @@ func P(propName string, propVal any) any {
287287
return map[string]any{propName: propVal}
288288
}
289289

290-
func UseState[T any](ctx context.Context, initialVal T) (T, func(T)) {
291-
vc := GetRenderContext(ctx)
292-
hookVal := vc.GetOrderedHook()
293-
if !hookVal.Init {
294-
hookVal.Init = true
295-
hookVal.Val = initialVal
296-
}
297-
var rtnVal T
298-
rtnVal, ok := hookVal.Val.(T)
299-
if !ok {
300-
panic("UseState hook value is not a state (possible out of order or conditional hooks)")
301-
}
302-
setVal := func(newVal T) {
303-
hookVal.Val = newVal
304-
vc.AddRenderWork(vc.GetCompWaveId())
305-
}
306-
return rtnVal, setVal
307-
}
308-
309-
func UseStateWithFn[T any](ctx context.Context, initialVal T) (T, func(T), func(func(T) T)) {
290+
func UseState[T any](ctx context.Context, initialVal T) (T, func(T), func(func(T) T)) {
310291
vc := GetRenderContext(ctx)
311292
hookVal := vc.GetOrderedHook()
312293
if !hookVal.Init {
@@ -332,7 +313,7 @@ func UseStateWithFn[T any](ctx context.Context, initialVal T) (T, func(T), func(
332313
return rtnVal, setVal, setFuncVal
333314
}
334315

335-
func UseAtom[T any](ctx context.Context, atomName string) (T, func(T)) {
316+
func UseSharedAtom[T any](ctx context.Context, atomName string) (T, func(T), func(func(T) T)) {
336317
vc := GetRenderContext(ctx)
337318
hookVal := vc.GetOrderedHook()
338319
if !hookVal.Init {
@@ -355,7 +336,13 @@ func UseAtom[T any](ctx context.Context, atomName string) (T, func(T)) {
355336
vc.AddRenderWork(waveId)
356337
}
357338
}
358-
return atomVal, setVal
339+
setFuncVal := func(updateFunc func(T) T) {
340+
atom.Val = updateFunc(atom.Val.(T))
341+
for waveId := range atom.UsedBy {
342+
vc.AddRenderWork(waveId)
343+
}
344+
}
345+
return atomVal, setVal, setFuncVal
359346
}
360347

361348
func UseVDomRef(ctx context.Context) *VDomRef {

0 commit comments

Comments
 (0)