Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ APIs to simplify data loading and caching. Primarily intended for use with [Reac

#### ⚠️ Considerations
1. Suspense is an experimental, pre-release feature; **these APIs will change** along with React.
1. This package depends on `react@experimental` and `react-dom@experimental` versions.
1. This package depends on `react@^19.0.0` and `react-dom@^19.0.0` versions.


#### Example
Expand Down
3 changes: 0 additions & 3 deletions definitions/react.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import "react";

declare module "react" {
// Unstable APIs used by this package ...

export function unstable_getCacheForType<T>(resourceType: () => T): T;

export function unstable_useCacheRefresh(): (
resourceType?: () => any,
seed?: any
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"@preconstruct/cli": "^2.8.1",
"@types/jest": "^29.4.0",
"@types/node": "^18.14.6",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"@types/react": "^19.0.1",
"@types/react-dom": "^19.0.1",
"@types/react-virtualized-auto-sizer": "^1.0.1",
"parcel": "^2.9.3",
"path-browserify": "^1.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/suspense/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
"point-utilities": "^0.0.2"
},
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
"react": "^19.0.0",
"react-dom": "^19.0.0"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm not sure what the best way is to support both, so for now I'm only focused on getting it to work with 19.

},
"browserslist": [
"Chrome 79"
]
}
}
35 changes: 11 additions & 24 deletions packages/suspense/src/cache/createCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isDevelopment } from "#is-development";
import { unstable_getCacheForType as getCacheForTypeMutable } from "react";
import { cache as cacheMutable } from "react";
import {
STATUS_NOT_FOUND,
STATUS_PENDING,
Expand Down Expand Up @@ -38,12 +38,11 @@ import {
isResolvedRecord,
} from "../utils/isRecordStatus";

if (getCacheForTypeMutable == null) {
if (cacheMutable == null) {
throw new Error(
"unstable_getCacheForType is not a function.\n\n" +
"cache is not a function.\n\n" +
"This probably means that the wrong version of React has been specified as a dependency. " +
'The "suspense" package requires the @experimental release of "react" and "react-dom".\n\n' +
"For more information, see https://react.dev/community/versioning-policy#experimental-channel"
'The "suspense" package requires release 19 of "react" and "react-dom".'
);
}

Expand Down Expand Up @@ -141,13 +140,11 @@ export function createCache<Params extends Array<any>, Value>(
// Immutable caches should read from backing cache directly.
// Only mutable caches should use React-managed cache
// in order to reduce re-renders when caches are refreshed for mutations.
const getCacheForType = immutable ? () => recordMap : getCacheForTypeMutable;
const getCacheForType = immutable ? () => recordMap : cacheMutable(createPendingMutationRecordMap);

function abort(...params: Params): boolean {
const cacheKey = getKey(params);
const pendingMutationRecordMap = getCacheForType(
createPendingMutationRecordMap
);
const pendingMutationRecordMap = getCacheForType();

// In-progress mutations aren't guaranteed to be in the recordMap.
// So we check the mutationAbortControllerMap to infer this.
Expand Down Expand Up @@ -189,9 +186,7 @@ export function createCache<Params extends Array<any>, Value>(
debugLog("cache()", params);

const cacheKey = getKey(params);
const pendingMutationRecordMap = getCacheForType(
createPendingMutationRecordMap
);
const pendingMutationRecordMap = getCacheForType();

let record: Record<Value> | undefined = getRecord(...params);
if (record != null) {
Expand Down Expand Up @@ -237,9 +232,7 @@ export function createCache<Params extends Array<any>, Value>(

function evict(...params: Params): boolean {
const cacheKey = getKey(params);
const pendingMutationRecordMap = getCacheForType(
createPendingMutationRecordMap
);
const pendingMutationRecordMap = getCacheForType();

debugLog("evict()", params);

Expand All @@ -252,9 +245,7 @@ export function createCache<Params extends Array<any>, Value>(
}

function evictAll(): void {
const pendingMutationRecordMap = getCacheForType(
createPendingMutationRecordMap
);
const pendingMutationRecordMap = getCacheForType();

debugLog("evictAll()", undefined);

Expand All @@ -273,18 +264,14 @@ export function createCache<Params extends Array<any>, Value>(

function getRecord(...params: Params): Record<Value> | undefined {
const cacheKey = getKey(params);
const pendingMutationRecordMap = getCacheForType(
createPendingMutationRecordMap
);
const pendingMutationRecordMap = getCacheForType();

return pendingMutationRecordMap.get(cacheKey) ?? recordMap.get(cacheKey);
}

function getOrCreateRecord(...params: Params): Record<Value> {
const cacheKey = getKey(params);
const pendingMutationRecordMap = getCacheForType(
createPendingMutationRecordMap
);
const pendingMutationRecordMap = getCacheForType();

let record = getRecord(...params);
if (record == null) {
Expand Down
Loading