task

package
v0.14.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package task is the generation-tracked async work manager for a Bubble Tea app: named scopes group work, every start bumps the scope's monotonic generation, and a finished result is accepted only while its generation is still the latest — so a superseded fetch (the user refreshed again, changed filters, moved on) is dropped instead of overwriting newer state. All bookkeeping happens on the single Update goroutine, so there is no locking; the run closure executes off-loop in the returned command and never touches the manager. There is deliberately no "task started" message: the caller sets its own loading state synchronously when it starts work, which avoids any ordering race against the finished message for fast tasks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FinishedMsg

type FinishedMsg struct {
	Scope  Scope
	Gen    uint64
	Result any
	Err    error
}

FinishedMsg carries the result of a task. The app drops it unless its generation is still the latest for the scope (see Manager.Accept), then routes it to whichever component owns the scope — even if the user has since switched to a different view.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager hands out monotonic generations per scope so stale results can be discarded. All of its methods are called from the single Bubble Tea update goroutine, so the generation map needs no locking: Start runs during Update, Accept runs during Update, and the async Run closure never touches it.

func New

func New() *Manager

New returns an empty manager.

func (*Manager) Accept

func (m *Manager) Accept(scope Scope, gen uint64) bool

Accept reports whether a finished task is still the latest for its scope. A false result means the user moved on and the work should be discarded.

func (*Manager) Generation

func (m *Manager) Generation(scope Scope) uint64

Generation returns the current generation for a scope (0 if never started).

func (*Manager) Start

func (m *Manager) Start(spec Spec) tea.Cmd

Start bumps the scope's generation and returns a command that runs Run off the UI loop and reports the outcome as a FinishedMsg tagged with the captured generation. A later Start in the same scope makes this task's eventual result stale. The map is lazily initialized so a zero-value manager is usable.

type Scope

type Scope string

Scope groups async work so that a newer task in the same scope supersedes an older one. For example all issue fetches share one scope; if the user refreshes twice quickly, only the latest result is accepted.

type Spec

type Spec struct {
	Scope Scope
	Run   func() (any, error)
}

Spec describes a unit of async work. Run executes off the UI loop (Bubble Tea runs the returned command in its own goroutine) and must not touch any model state — it returns a result that is delivered back as a FinishedMsg and applied on the UI loop.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL