Skip to content

core: document lazy-init edge case in derive and select getters #68

@nerdalytics

Description

@nerdalytics

Finding

Both createDerive (lines 247-254) and createSelect (lines 286-294) in src/index.ts have a !initialized branch in their getter functions. This branch duplicates computation logic from the effect and appears unreachable under normal usage — createEffect always runs the effect synchronously.

The branch is reachable when derive() or select() is called inside batch(), because batch defers effect creation. Without the lazy-init fallback, reading a derived value inside batch would return stale data.

The code is correct. The reason it exists is not documented.

Proposed fix

Add a brief comment above each !initialized branch explaining the batch-deferred edge case. Something like:

// When derive() is called inside batch(), effect creation is deferred.
// This fallback ensures the first read still returns the computed value.

No behavioral change needed.

Source: out/report-simplify.txt, Findings 3 + 4

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions