Skip to content
Myr

Subs shouldn't take a model as an argument

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This also nullifies the need for SubMap

authored by Christian Rocha · committed by Christian Rocha · parent dada718 · Browse files · 4 files+6 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+6 added−16 removed

  • other:+6 added−16 removed

4 files+6 added−16 removed

modified:examples/fullscreen/main.go+1 added-1 removed
53 unmodified lines
54}5556func subscriptions(_ tea.Model) tea.Subs {57 return tea.Subs{58 "tick": func(_ tea.Model) tea.Msg {58 "tick": func() tea.Msg {59 time.Sleep(time.Second)60 return tickMsg{}61 },62 }
6 unmodified lines
modified:examples/simple/main.go+1 added-1 removed
57 unmodified lines
58// This is a subscription which we setup in NewProgram(). It waits for one59// second, sends a tick, and then restarts.60func subscriptions(_ tea.Model) tea.Subs {61 return tea.Subs{62 "tick": func(_ tea.Model) tea.Msg {62 "tick": func() tea.Msg {63 time.Sleep(time.Second)64 return TickMsg{}65 },66 }
1 unmodified line
modified:examples/views/main.go+2 added-2 removed
56 unmodified lines
57 "frame": frame,58 }59}6061func tick(model tea.Model) tea.Msg {61func tick() tea.Msg {62 time.Sleep(time.Second)63 return tickMsg{}64}6566func frame(model tea.Model) tea.Msg {66func frame() tea.Msg {67 time.Sleep(time.Second / 60)68 return frameMsg{}69}70
158 unmodified lines
modified:tea.go+2 added-12 removed
30 unmodified lines
3132// Sub is an event subscription; generally a recurring IO operation. If it33// returns nil it's considered a no-op, but there's really no reason to have34// a nil subscription.35type Sub func(Model) Msg35type Sub func() Msg3637// SubMap applies a given model as an argument to a given subscription.38func SubMap(sub Sub, model Model) Sub {39 if sub == nil {40 return nil41 }42 return func(_ Model) Msg {43 return sub(model)44 }45}4637// Subs is a keyed set of subscriptions. The key should be a unique38// identifier: two different subscriptions should not have the same key or39// weird behavior will occur.40type Subs map[string]Sub
228 unmodified lines
269 for {270 select {271 case <-done:272 return283 case msgs <- s(model):273 case msgs <- s():274 continue275 }276 }277 }(activeSubs[key].done, activeSubs[key].sub)
19 unmodified lines