feat: add memo utility#420
Open
MathiasWP wants to merge 2 commits into
Open
Conversation
`memo` is to `$derived` what `watch` is to `$effect`: it accepts an explicit list of source getters and only recomputes when one of them changes. Reads inside the compute body are not tracked. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 2ea24d1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The first argument now exists solely to register dependencies; the compute callback takes no arguments and just produces the value. This matches the shape of `watch` more directly: arg 1 = "what triggers reactivity", arg 2 = "what to do". Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Very useful! I often end up inlining such with |
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
Adds a new
memoutility — the$derivedanalog ofwatch.$derivedautomatically tracks every signal read inside its expression. That's usually what you want, but sometimes the compute body needs to read state that shouldn't trigger recomputation. Today the only escape hatch is wrapping every unrelated read inuntrack, which is verbose and easy to get wrong.memoflips the model and mirrorswatch's API split:Implementation
packages/runed/src/lib/utilities/memo/memo.svelte.ts—memofunction. Touches the source getter(s) to register them as dependencies, then runs the compute body insideuntrackso nothing inside it gets tracked. Internally constructs a small class that holds a$derived.byand exposes the value via.current(the standard runed pattern, since runes are compiler macros and can't be returned as bare reactive values from a function).packages/runed/src/lib/utilities/memo/memo.test.svelte.ts— 4 tests:packages/runed/src/lib/utilities/memo/index.ts+packages/runed/src/lib/utilities/index.ts— re-exports.sites/docs/src/content/utilities/memo.md— docs page modeled directly onwatch.mdfor consistency (same framing paragraph structure, same example progression: single source → array → untracked-read example)..changeset/add-memo-utility.md—minorbump.Test plan
pnpm exec vitest --run src/lib/utilities/memo— 4/4 passingpnpm check— 0 errors, 0 warningsmemovs alternatives) and on the choice to expose via.currentrather than a different accessor🤖 Generated with Claude Code