-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
70 lines (55 loc) · 1.23 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package reactea
import (
tea "github.com/charmbracelet/bubbletea"
)
// Useful for constraining some actions to update-stage only
var isUpdate bool
type model struct {
program *tea.Program
root Component
width, height int
}
func (m model) Init() tea.Cmd {
return m.root.Init()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
wasRouteChanged = false
switch msg := msg.(type) {
// We want component to know at what size should it render
// and unify size handling across all Reactea components
// We pass WindowSizeMsg to root component just for
// sake of utility.
case tea.WindowSizeMsg:
m.width, m.height = msg.Width, msg.Height
}
isUpdate = true
m.execute(m.root.Update(msg))
isUpdate = false
// Guarantee rerender if route was changed
if wasRouteChanged {
return m, updatedRoute(lastRoute)
}
return m, nil
}
func (m model) View() string {
return m.root.Render(m.width, m.height)
}
func (m model) execute(cmd tea.Cmd) {
if cmd == nil {
return
}
go func() {
msg := cmd()
switch msg := msg.(type) {
case destroyAppMsg:
m.root.Destroy()
m.program.Send(tea.QuitMsg{})
case tea.BatchMsg:
for _, cmd := range msg {
m.execute(cmd)
}
default:
m.program.Send(msg)
}
}()
}