dillon1000/react

Commit

Initial implementation of cache cleanup (#22510)

This is an initial, partial implementation of a cleanup mechanism for the experimental Cache API. The idea is that consumers of the Cache API can register to be informed when a given Cache instance is no longer needed so that they can perform associated cleanup tasks to free resources stored in the cache. A canonical example would be cancelling pending network requests.

An overview of the high-level changes:

* Changes the `Cache` type from a Map of cache instances to be an object with the original Map of instances, a reference count (to count roughly "active references" to the cache instances - more below), and an AbortController.
* Adds a new public API, `unstable_getCacheSignal(): AbortSignal`, which is callable during render. It returns an AbortSignal tied to the lifetime of the cache - developers can listen for the 'abort' event on the signal, which React now triggers when a given cache instance is no longer referenced. 
  * Note that `AbortSignal` is a web standard that is supported by other platform APIs; for example a signal can be passed to `fetch()` to trigger cancellation of an HTTP request.
* Implements the above - triggering the 'abort' event - by handling passive mount/unmount for HostRoot and CacheComponent fiber nodes.

Cases handled:
* Aborted transitions: we clean up a new cache created for an aborted transition
* Suspense: we retain a fresh cache instance until a suspended tree resolves

For follow-ups:
* When a subsequent cache refresh is issued before a previous refresh completes, the refreshes are queued. Fresh cache instances for previous refreshes in the queue should be cleared, retaining only the most recent cache. I plan to address this in a follow-up PR.
* If a refresh is cancelled, the fresh cache should be cleaned up.
Browse files
Changed paths29 files
First-parent comparison
M packages/react-devtools-shared/src/__tests__/__snapshots__/profilingCache-test.js.snap ModifiedM packages/react-dom/src/server/ReactPartialRendererHooks.js ModifiedM packages/react-reconciler/src/ReactFiberCacheComponent.new.js ModifiedM packages/react-reconciler/src/ReactFiberCacheComponent.old.js ModifiedM packages/react-reconciler/src/ReactFiberCommitWork.new.js ModifiedM packages/react-reconciler/src/ReactFiberCommitWork.old.js ModifiedM packages/react-reconciler/src/ReactFiberCompleteWork.new.js ModifiedM packages/react-reconciler/src/ReactFiberCompleteWork.old.js ModifiedM packages/react-reconciler/src/ReactFiberHooks.new.js ModifiedM packages/react-reconciler/src/ReactFiberHooks.old.js ModifiedM packages/react-reconciler/src/ReactFiberLane.new.js ModifiedM packages/react-reconciler/src/ReactFiberLane.old.js ModifiedM packages/react-reconciler/src/ReactFiberRoot.new.js ModifiedM packages/react-reconciler/src/ReactFiberRoot.old.js ModifiedM packages/react-reconciler/src/ReactFiberWorkLoop.new.js ModifiedM packages/react-reconciler/src/ReactFiberWorkLoop.old.js ModifiedM packages/react-reconciler/src/ReactInternalTypes.js ModifiedM packages/react-reconciler/src/__tests__/ReactCache-test.js ModifiedM packages/react-reconciler/src/__tests__/SchedulingProfiler-test.internal.js ModifiedM packages/react-reconciler/src/__tests__/SchedulingProfilerLabels-test.internal.js ModifiedM packages/react-reconciler/src/__tests__/StrictEffectsModeDefaults-test.internal.js ModifiedM packages/react/index.classic.fb.js ModifiedM packages/react/index.experimental.js ModifiedM packages/react/index.js ModifiedM packages/react/index.modern.fb.js ModifiedM packages/react/src/React.js ModifiedM packages/react/src/ReactHooks.js ModifiedM packages/react/unstable-shared-subset.experimental.js ModifiedM scripts/jest/setupEnvironment.js Modified
Patch

Files changed

Rendering syntax-highlighted changes…