Skip to content

fix(native): re-run an effect a batch notifies twice, and release a style entry by identity - #436

Merged
danstepanov merged 1 commit into
nativewind:mainfrom
YevheniiKotyrlo:fix/bounded-styles-cache
Nov 23, 2026
Merged

danstepanov merged 1 commit into
nativewind:mainfrom
YevheniiKotyrlo:fix/bounded-styles-cache

Conversation

@YevheniiKotyrlo

@YevheniiKotyrlo YevheniiKotyrlo commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

#451 releases a cached style entry once its last element unmounts. Two defects remain on main:

  1. A batch skips an effect notified twice. A batch collects notified effects in a Set and iterates it, and that iteration skips a member added again after it ran. Two elements read a :root variable that a width media query switches, and one of them reads a viewport unit before the variable. After a rotation, that element keeps the previous orientation's value: at 700 wide it renders the narrow colour, at 300 the wide one. The same rule with the variable before the unit is correct.
  2. A replayed effect drops a live entry from the cache. An entry deletes its hash from the cache when its last observer leaves, whatever the hash maps to by then. React replays effects under StrictMode and when an <Activity> is shown again. The replay acquires a fresh entry for the hash, then releases the old one, which deletes the fresh one. As a result:
    • under StrictMode the cache holds no entries while elements are mounted, so nothing is shared;
    • an element that Activity showed again deletes another element's live entry when it unmounts.

Solution

  • batch(update) runs the update with notifications deferred, then drains them as a work list: each effect is deleted before it runs, so a second notification queues it again. The Dimensions listener and StyleCollection.inject both open their batch through it.
  • family.deleteIf(key, value) deletes a key only while it maps to that value, and an entry releases itself through it.

Tests

  • batch.test.tsx:
    • the rotation, in both declaration orders
    • an effect notified again while the batch drains
    • a batch defers its notifications and runs each effect once
  • style-cache.test.tsx:
    • the StrictMode and Activity replays
    • coverage of the release: an entry goes when its last element unmounts, survives while shared, stays reactive while mounted and is released when superseded
    • the cache returns to empty after repeated mounts
    • re-rendering keeps the value nativeStyleMapping took from the shared entry
  • reactivity-dependencies.test.tsx: an observed computed value runs once per change, and deleteIf.
  • On main with fix(native): one resolved-style cache key per distinct input set #434, 6 of the 23 added cases fail: the rotation, both replays, and three that need batch or deleteIf. Each of 8 mutations fails its own cases.

Verification

On Windows with Node 26:

  • yarn lint clean
  • yarn typecheck clean
  • yarn test --coverage: every failure also fails on main on this machine (the four babel cases)
  • yarn build clean
  • yarn example expo export --platform web exported

This PR's cases, run on main and here: #436 (comment)

Merge order

@YevheniiKotyrlo

YevheniiKotyrlo commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor Author

No device frame

The tests drive both defects directly: the rotation in both declaration orders, and the StrictMode and Activity replays.

This PR's cases — main against this PR

batch.test.tsx, style-cache.test.tsx, config-identity.test.ts, reactivity-dependencies.test.tsx and state-hash.test.ts from this PR, run on main (eda2b286) and here, merged into main: 26 of their 53 cases fail on main and none here.

Run through the repo's jest on Windows with Node 26.

@sammoore sammoore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fixes a regression from #451 / 3.1.0-rc.0 (which exposed an underlying assumption that's been around since 3.0.0).

Approved!

…tyle entry by identity

A batch collected notified effects in a Set and iterated it, which skips a
member added again after it ran. On a rotation, an entry that reads a
viewport unit before a :root variable another entry shares runs before the
variable recomputes, and the variable's notification then lands on a member
already passed, so the element keeps the previous orientation's value.
batch() deletes each effect before running it, so a second notification
queues it again; the Dimensions listener and StyleCollection.inject both
open their batch through it.

A style entry deleted its hash from the cache once its last observer left,
whatever entry the hash mapped to by then. React replays effects under
StrictMode and when an Activity is shown again: the replay acquires a fresh
entry for the hash and then releases the old one, which deleted the fresh
entry. family.deleteIf deletes a key only while it maps to the given value,
and an entry releases through it.

The tests also cover the release itself: an entry goes when its last element
unmounts, survives while shared, stays reactive while mounted, and the cache
returns to empty after repeated mounts.
@danstepanov
danstepanov force-pushed the fix/bounded-styles-cache branch from 0a41370 to 6999d3e Compare November 23, 2026 19:57
@danstepanov
danstepanov merged commit 2cc08ed into nativewind:main Nov 23, 2026
6 checks passed
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.

3 participants