Skip to content

Add debounce modifier - #216

Merged
ra1028 merged 1 commit into
mainfrom
add-debounce-modifier
Jun 18, 2026
Merged

ra1028 merged 1 commit into
mainfrom
add-debounce-modifier

Conversation

@ra1028

@ra1028 ra1028 commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Summary

Add a .debounce(for:) modifier that delays delivering the atom's value until it stops changing for the specified duration — useful for deferring reaction to rapidly changing values, such as a search query while typing.

@Watch(QueryAtom().debounce(for: 0.3))
var query

Behavior

  • The initial value is delivered immediately (not debounced).
  • While the value keeps changing, the previously delivered value is kept; once no further change occurs for duration, the latest value is delivered.
  • Only the latest value is ever delivered — superseded pending deliveries are cancelled.

Design notes

  • Its sole responsibility is to delay value delivery. It does not deduplicate equivalent values, so there is no Equatable constraint; compose with .changes (atom.debounce(for: 0.3).changes) when dedup is desired.
  • Implemented like other stateful modifiers (PreviousModifier): the modified atom watches the base atom and a private storage atom. The pending delivery is a Task cancelled via context.onTermination whenever the atom is recomputed or released, so rapid updates restart the timer and never deliver stale values.
  • Storage holds the settled value through an enum { case empty; case settled(Base) } rather than an optional, so it stays correct when Base is itself optional (a settled nil is not mistaken for "uninitialized").

Tests

Nine tests cover: initial immediate delivery, value lag then settle, delivering only the latest value (superseded values never delivered), propagation to a dependent atom, cancellation on unwatch and on reset, optional base values (including settling to nil), composition with .changes, zero duration, and key identity.

🤖 Generated with Claude Code

Add a `.debounce(for:)` modifier that delays delivering the atom's value
until it stops changing for the specified duration. The initial value is
delivered immediately, and the latest value is delivered once it settles.

Its sole responsibility is to delay value delivery; it does not deduplicate
equivalent values (compose with `.changes` for that), so there is no
`Equatable` constraint. A pending delivery is cancelled when the atom is
recomputed or released, so only the latest value is ever delivered.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 18, 2026 07:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new .debounce(for:) atom modifier to delay value delivery until the upstream atom stops changing for a specified duration, making it easier to defer reactions to rapidly changing state (e.g., typing).

Changes:

  • Introduces DebounceModifier and a public Atom.debounce(for:) API.
  • Adds a dedicated test suite covering debounce behavior, cancellation, composition with .changes, optionals, zero duration, and key identity.
  • Updates the DocC index to include the new modifier and API entry.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
Sources/Atoms/Modifier/DebounceModifier.swift Implements the new debounce modifier and its internal storage atom.
Tests/AtomsTests/Modifier/DebounceModifierTests.swift Adds comprehensive tests for debounce semantics and lifecycle/cancellation behavior.
Sources/Atoms/Atoms.docc/Atoms.md Registers the new API/modifier in the documentation index.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ra1028
ra1028 merged commit 3ff9fa8 into main Jun 18, 2026
5 checks passed
@ra1028
ra1028 deleted the add-debounce-modifier branch June 18, 2026 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants