Repository navigation
Add debounce modifier - #216
Merged
Merged
Conversation
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>
There was a problem hiding this comment.
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
DebounceModifierand a publicAtom.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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.Behavior
duration, the latest value is delivered.Design notes
Equatableconstraint; compose with.changes(atom.debounce(for: 0.3).changes) when dedup is desired.PreviousModifier): the modified atom watches the base atom and a private storage atom. The pending delivery is aTaskcancelled viacontext.onTerminationwhenever the atom is recomputed or released, so rapid updates restart the timer and never deliver stale values.enum { case empty; case settled(Base) }rather than an optional, so it stays correct whenBaseis itself optional (a settlednilis 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