feat: tea.Batch() returns nil if all Cmds were nil #200
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
hi,
as far as i can tell,
tea.Batch()
semantically should returnnil
if there is notea.Cmd
. this is not documented, but i base this assumption on the length check in its implementation:bubbletea/tea.go
Lines 112 to 128 in 608fde5
however, if the
[]tea.Cmd
passed to it contains onlynil
item(s), a batch cmd containing onlynil
is returned, instead of justnil
.a
[]tea.Cmd
that just contains onlynil
can easily happen when you expect multipletea.Cmd
in yourUpdate()
function and start by initializing the slice, but then don't check every cmd returned by a subcall fornil
first before appending to this slice.this happens for example in
bubbles.List
:https://github.com/charmbracelet/bubbles/blob/7ecce3fb97420ba2a44e3453c9291a98d5894d9d/list/list.go#L682-L683
this bit me a little bit, as i pass the
tea.Msg
to a submodel, and wanted to check if it "consumed" the message by checking if the returnedtea.Cmd
isnil
. if it is not consumed, i consume it in the main model (think pressing:
in vim insert mode vs normal mode). i used the pattern from theList
bubble in my own code, but even when i checked everywhere fornil
before appending, i realized that theList
bubble itself returns a batch cmd with just a nil inside it and i'd have to check for that too.for now i just worked around this by explicitly telling my main model if the message was consumed, which is fine too. but i think it would be more comfortable and true to the intention of the
tea.Batch()
function to e.g. check if the slice given to it contains onlynil
s. not only can i just still check whether thetea.Cmd
a model returns is simplynil
or not without having to know that it is a batch cmd, but i also don't have to check every returned cmd before appending it to such a slice to make this work.the reasons i can think of against this are:
nil
what do you think?