Skip to content

Overload cached for non-class use - #21537

Draft
NullVoxPopuli-ai-agent wants to merge 3 commits into
emberjs:mainfrom
NullVoxPopuli-ai-agent:overload-cached-for-non-class-use
Draft

Overload cached for non-class use#21537
NullVoxPopuli-ai-agent wants to merge 3 commits into
emberjs:mainfrom
NullVoxPopuli-ai-agent:overload-cached-for-non-class-use

Conversation

@NullVoxPopuli-ai-agent

@NullVoxPopuli-ai-agent NullVoxPopuli-ai-agent commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Implements the standalone form of cached proposed in Overload cached for non-class use — the caching companion to RFC 1071's overloaded tracked, and mirrors that implementation (#21471) throughout.

import { tracked, cached } from '@glimmer/tracking';

const count = tracked(0);
const doubled = cached(() => count.value * 2);

doubled.value; // read the cached value, entangling with any tracking context
doubled.get(); // function shorthand for reading
  • New cachedValue(fn, options) in @glimmer/validator, returning RFC 1071's ReadOnlyReactive — no set/update/freeze, it has no storage of its own. Backed by createCache/getValue. Per RFC review there is no new public type: get() joins the ReadOnlyReactive interface (an RFC 1071 oversight) and the implementation class stays internal.
  • options.description labels the cache for debugging. (An earlier revision proposed options.equals; removed per RFC review — calling fn is the expensive part, so re-running it only to discard the result has unclear benefit.)
  • @ember/-internals/metal cached dispatches between the two forms: a legacy decorator invocation always receives three arguments, so cached(fn) / cached(fn, options) can never collide with @cached on a getter (including static getters). Decorator behavior is unchanged.
  • The docs blocks gain a "Standalone usage" section; the existing Reactive/ReadOnlyReactive/TrackedValue type re-exports from @glimmer/tracking are unchanged.
  • Tests at every layer, mirroring Implement RFC #1071: overloaded tracked #21471: @glimmer/validator unit tests, glimmer integration-tests rendering tests, metal standalone + assertion tests, an ember rendering test (Component Standalone Cached Values), and type-tests covering overload resolution, read-only value, and the options object.

Draft until the RFC is proposed/accepted upstream.

Test results: filter=cached 33/33 pass, filter=tracked 273 pass / 3 skip, type-check clean, lint:docs pass, prettier/eslint clean.

🤖 Generated with Claude Code

Implements the standalone form proposed in
NullVoxPopuli/rfcs#19 -- the memoization
companion to RFC 1071's overloaded tracked (emberjs#21471).

cached(fn, options?) returns a read-only CachedValue backed by
createCache/getValue. options.equals (default Object.is) retains the
previous value's identity when a re-computation produces an equivalent
result; options.description labels the cache for debugging. The
decorator form is unchanged: a legacy decorator invocation always
receives three arguments, so the two forms cannot collide.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@NullVoxPopuli-ai-agent
NullVoxPopuli-ai-agent force-pushed the overload-cached-for-non-class-use branch from ac806d1 to e137661 Compare July 31, 2026 02:12
NullVoxPopuli-ai-agent and others added 2 commits July 30, 2026 22:48
Calling fn is the expensive part; re-running it only to discard the
result has unclear benefit, so the standalone form takes only
{ description }. Also say cached, not memoized, throughout.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
No new public CachedValue type: get() joins the ReadOnlyReactive
interface (an RFC 1071 oversight) and both cachedValue() and the
cached() overload return ReadOnlyReactive<Value>. The CachedValue
class stays as an internal implementation detail.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant