Introducing a new way to review your PRs.

Read the announcement

crmne / spotifast

Spotify, native and fast. One lightweight Rust app for your whole library, local playback, and Spotify Connect on Linux, macOS, and Windows.

View on GitHub

19 documents · Updated Nov 18, 2026

docs / application-state-orchestration

Application state orchestration

Spotifast coordinates user-facing state in one application model. The app joins screen state, account data, playback state, saved preferences, background work, and desktop conditions so that each window shows a current and usable view of the same session.

Coordination model

Application state

The src/app.rs module is the main state owner for a running session. It keeps the current account, page, library data, playback view, pending user choices, settings, and window state together. UI code turns input into actions. The app applies those actions and sends work to shared services when the change needs data, playback, or the operating system.

References: src/app.rs• L365-430 src/app.rs• L483-590 src/app.rs• L1124-1154

State domains

The state model separates durable preferences from live data and short-lived work. This lets the app restore useful context after a restart without treating an old network answer as current.

  • Preferences control choices such as theme, language, audio output, library layout, and background playback.
  • Session state restores the last page, playback position, queue context, and other workspace choices.
  • Content state represents Home shelves, library lists, search results, and detail pages.
  • Playback state combines local player reports with remote device state.
  • Pending state records requests and optimistic changes until a service confirms them.

References: src/model.rs• L108-195 src/model.rs• L223-268 src/app.rs• L513-590 src/app.rs• L749-876

User-facing state flow

Frame cycle

Each logic pass accepts outside events, reconciles the app state, runs scheduled work, applies queued actions, and then updates the window and desktop services. The same cycle runs when the main window is hidden, which keeps playback and background services alive.

This diagram shows how user input and service work become the state that the UI and desktop services expose.

flowchart LR
    Input[User or desktop input] --> Actions[Queued actions]
    Backend[Async backend events] --> App[Application state]
    Actions --> App
    Power[Power and session conditions] --> App
    App --> UI[Window and panels]
    App --> Media[Media controls and tray]
    App --> Persist[Settings and session writes]
    App --> Backend

References: src/app.rs• L14544-14582 src/app.rs• L12503-12520 src/app.rs• L14584-14593

Asynchronous work

The backend is the boundary between the responsive UI thread and work that can wait on the network, playback engine, disk, or browser. The app sends commands and receives typed events through channels. Each event carries context when needed so the app can accept a current result and ignore a result from an older request, account, or playback session.

The boundary covers Spotify reads and writes, local playback, sign-in, artwork, cache work, audio output, proxy changes, and update work. A wake signal asks the current window or headless loop to run again instead of making the UI wait. During proxy restoration, work that needs the network waits until the usable connection policy is ready.

References: src/backend.rs• L1-7 src/backend.rs• L705-883 src/backend.rs• L984-1185 src/backend.rs• L1232-1290 src/backend.rs• L2184-2205

User actions

UI surfaces emit a shared Action vocabulary for navigation, playback, library edits, settings, and window changes. The app applies actions in order and may update local state before a service reply arrives. Later replies confirm the change or restore a safe state when the service rejects it.

References: src/model.rs• L1126-1197 src/model.rs• L1290-1415 src/app.rs• L12503-12520 src/app.rs• L12540-12589

Content and account state

Screen data

The models describe the pages that a person can open and the loading state behind them. Lists support incremental loading. Refreshes can keep the last good value while new data arrives. Generation and revision values keep late replies from replacing a newer page or account view.

This gives navigation a stable surface while the app reads more data. It also lets the app keep useful cached content visible during a short network failure.

References: src/model.rs• L108-195 src/model.rs• L223-268 src/model.rs• L270-505 src/model.rs• L565-736 src/model.rs• L840-915

Account transitions

Sign-in establishes the account that owns the content state. A connected session clears data that belongs to a former account, restores account caches, and starts the current page and playback reads. Sign-out removes account-specific data and stops account-bound playback services so the next user does not see the old session.

Local playback has its own readiness state. When it becomes ready, the app can retry content that needs the streaming session and replay a play request that waited for the player.

References: src/app.rs• L3414-3479 src/app.rs• L3481-3565 src/backend.rs• L1164-1180

Session persistence

Settings and session data have different roles. Settings hold long-lived user choices. Session data holds the last working context, such as the page, playback resume point, queue, layout state, and recent contexts. The app writes them through the file service and flushes them before shutdown or a system suspend.

References: src/settings.rs• L261-480 src/settings.rs• L688-721 src/app.rs• L15068-15160

Runtime adaptation

User preferences

Settings is the durable preference model for the app. It provides defaults for new users, accepts older saved forms, and resolves choices into values used by the app and backend. Some preferences are drafts until the user applies them, so active services continue to use the last confirmed value.

Theme, language, library presentation, playback output, proxy policy, and background behavior all flow through this model. A preference change therefore updates the visible state and may also restart or reconfigure a service.

References: src/settings.rs• L261-480 src/settings.rs• L496-591 src/settings.rs• L688-781 src/app.rs• L5394-5504

Proxy policy

Proxy settings have two forms: the values currently shown in the settings view and the policy active in network workers. The app validates a new policy, sends it to the backend, and persists it only after the backend accepts it. Password data uses protected storage rather than the normal settings snapshot.

This separation prevents an unfinished form edit from changing network requests or restarting local playback. It also lets startup restore the saved policy before queued network work begins.

References: src/settings.rs• L179-229 src/settings.rs• L633-676 src/settings.rs• L748-781 src/app.rs• L5404-5483 src/backend.rs• L2168-2181

Appearance state

The appearance service supplies the desktop's light or dark preference and its reduced-motion choice when the platform exposes them. The app uses those values with the saved theme choice, then applies one palette and motion policy to the current UI. A desktop change wakes the app so the visible state changes without a restart.

References: src/appearance.rs• L1-10 src/appearance.rs• L29-78 src/app.rs• L14594-14607 src/app.rs• L5581-5626

Power and reachability

The power service turns system conditions into a budget for drawing and polling. A visible and focused window can use the normal budget. An unfocused window or an energy-saving system uses a lower budget. A hidden, locked, or display-off session enters the background budget.

The app also tells the backend when the system reports that the network is unavailable. The engine waits for reachability instead of retrying in a loop. When the network returns, the app refreshes incomplete account data. Local playback can keep the computer awake while it plays or loads. Remote playback does not prevent sleep.

References: src/power.rs• L1-8 src/power.rs• L14-80 src/app.rs• L14544-14562 src/app.rs• L3585-3614 src/backend.rs• L1648-1657

Shared runtime services

Shared services remain active across window changes. The backend keeps asynchronous work running. Media controls and the tray provide outside control. The app republishes current playback and window intent to those services, then recreates or hides the window without losing the session.

This coordination supports background playback, tray control, system media keys, and a window that can close while the process remains alive. The runtime and entrypoint responsibilities are described in Application runtime and lifecycle and Executable entrypoints and startup diagnostics.

References: src/app.rs• L955-1015 src/app.rs• L1081-1102 src/app.rs• L14544-14582 src/backend.rs• L1182-1186 src/backend.rs• L1688-1694

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep action names, backend commands, backend events, and app handlers aligned. A missing route can leave a control changed locally with no service result.
  • When adding account-scoped data, clear it on sign-out and reject replies from an older account or generation.
  • When changing a saved preference, check its default, load behavior, persistence form, and the service that consumes the applied value.
  • When changing proxy behavior, keep draft values separate from the active policy and preserve the protected password migration path.
  • When changing power or reachability handling, check both visible-window polling and headless playback behavior.
  • When changing shutdown or suspend persistence, keep settings, session, history, and file flush ordering safe for a short system hold.

Important failure modes

TriggerConsequenceGuard
A late backend reply arrives after navigation, sign-out, or a newer refreshOld content can replace the current page or account stateRequest generations, serials, and account checks gate replies before they update models
A preference draft is treated as active before ApplyNetwork or playback services use a value the user did not confirmThe app separates pending preferences from applied settings and updates the backend after confirmation
Proxy restoration finishes after network work is queuedRequests can use an incomplete or invalid connection policyThe backend holds network commands until proxy restoration completes
The network becomes unavailable while the engine is reconnectingReconnect attempts can fail repeatedly and playback can flapReachability is sent to the backend, which waits for the network before retrying
The window closes during background playbackThe process may quit or lose the controls needed to returnTray and resident-window state distinguish hide, reopen, switch, and quit
Suspend or session end occurs with pending writesResume state or preferences may be lostThe app saves state and flushes the file writer before acknowledging the system event

Key files

FileWhy to read it
src/app.rsMain coordinator for actions, backend events, persistence, window state, playback state, and runtime services
src/model.rsShared vocabulary for pages, loading state, lists, Home data, and user actions
src/backend.rsCommand and event boundary for asynchronous API, playback, disk, proxy, and desktop work
src/settings.rsDurable preference model, defaults, migration, appearance choices, and applied proxy policy
src/power.rsRules that map system conditions to drawing, polling, playback activity, and sleep behavior
src/appearance.rsDesktop appearance and motion signals used by the app's theme and interaction state