Skip to content

Commit 9a29464

Browse files
committed
Use cache in the fixture
1 parent 89d4fe1 commit 9a29464

File tree

13 files changed

+167
-22
lines changed

13 files changed

+167
-22
lines changed

fixtures/flight/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "flight",
3-
"type": "module",
43
"version": "0.1.0",
54
"private": true,
65
"dependencies": {

fixtures/flight/server/handler.server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server';
44
import * as React from 'react';
5+
import {CacheContext, createCache} from 'react/unstable-cache.js';
56

67
import url from 'url';
78

@@ -10,10 +11,13 @@ function resolve(path) {
1011
}
1112

1213
module.exports = async function(req, res) {
14+
CacheContext._currentValue = createCache();
15+
1316
res.setHeader('Access-Control-Allow-Origin', '*');
14-
const m = await import('../src/App.server.js');
15-
// const m = require('../src/App.server.js');
17+
// const m = await import('../src/App.server.js');
18+
const m = require('../src/App.server.js');
1619
const App = m.default.default || m.default;
20+
1721
pipeToNodeWritable(<App />, res, {
1822
// TODO: Read from a map on the disk.
1923
[resolve('../src/Counter.client.js')]: {

fixtures/flight/server/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ app.get('/', function(req, res) {
2424
require('./handler.server.js')(req, res);
2525
});
2626

27+
app.get('/todos', function(req, res) {
28+
res.setHeader('Access-Control-Allow-Origin', '*');
29+
res.json([
30+
{
31+
id: 1,
32+
text: 'Shave yaks',
33+
},
34+
{
35+
id: 2,
36+
text: 'Eat kale',
37+
},
38+
]);
39+
});
40+
2741
app.listen(3001, () => {
2842
console.log('Flight Server listening on port 3001...');
2943
});

fixtures/flight/src/App.server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import {fetch} from 'react-fetch';
23

34
import Container from './Container.js';
45

@@ -7,10 +8,16 @@ import Counter from './Counter.client.js';
78
import ShowMore from './ShowMore.client.js';
89

910
export default function App() {
11+
const todos = fetch('http://localhost:3001/todos').json();
1012
return (
1113
<Container>
1214
<h1>Hello, world</h1>
1315
<Counter />
16+
<ul>
17+
{todos.map(todo => (
18+
<li key={todo.id}>{todo.text}</li>
19+
))}
20+
</ul>
1421
<ShowMore>
1522
<p>Lorem ipsum</p>
1623
</ShowMore>

fixtures/flight/src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import {Suspense} from 'react';
33
import ReactDOM from 'react-dom';
44
import ReactTransportDOMClient from 'react-transport-dom-webpack';
5+
import {CacheContext, createCache} from 'react/unstable-cache';
56

67
let data = ReactTransportDOMClient.createFromFetch(
78
fetch('http://localhost:3001')
@@ -11,10 +12,14 @@ function Content() {
1112
return data.readRoot();
1213
}
1314

15+
let cache = createCache();
16+
1417
ReactDOM.render(
15-
<Suspense fallback={<h1>Loading...</h1>}>
16-
<Content />
17-
</Suspense>,
18+
<CacheContext.Provider value={cache}>
19+
<Suspense fallback={<h1>Loading...</h1>}>
20+
<Content />
21+
</Suspense>
22+
</CacheContext.Provider>,
1823
document.getElementById('root')
1924
);
2025

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ function useOpaqueIdentifier(): OpaqueIDType | void {
298298
}
299299

300300
const Dispatcher: DispatcherType = {
301+
readCache: readContext, // TODO
301302
readContext,
302303
useCallback,
303304
useContext,

packages/react-dom/src/server/ReactPartialRendererHooks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ export function setCurrentPartialRenderer(renderer: PartialRenderer) {
492492
}
493493

494494
export const Dispatcher: DispatcherType = {
495+
readCache: readContext, // TODO
495496
readContext,
496497
useContext,
497498
useMemo,

packages/react-reconciler/src/ReactFiberHooks.new.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,7 @@ function dispatchAction<S, A>(
18161816
}
18171817

18181818
export const ContextOnlyDispatcher: Dispatcher = {
1819+
readCache: readContext, // TODO
18191820
readContext,
18201821

18211822
useCallback: throwInvalidHookError,
@@ -1837,6 +1838,7 @@ export const ContextOnlyDispatcher: Dispatcher = {
18371838
};
18381839

18391840
const HooksDispatcherOnMount: Dispatcher = {
1841+
readCache: readContext, // TODO
18401842
readContext,
18411843

18421844
useCallback: mountCallback,
@@ -1858,6 +1860,7 @@ const HooksDispatcherOnMount: Dispatcher = {
18581860
};
18591861

18601862
const HooksDispatcherOnUpdate: Dispatcher = {
1863+
readCache: readContext, // TODO
18611864
readContext,
18621865

18631866
useCallback: updateCallback,
@@ -1879,6 +1882,7 @@ const HooksDispatcherOnUpdate: Dispatcher = {
18791882
};
18801883

18811884
const HooksDispatcherOnRerender: Dispatcher = {
1885+
readCache: readContext, // TODO
18821886
readContext,
18831887

18841888
useCallback: updateCallback,
@@ -1927,6 +1931,14 @@ if (__DEV__) {
19271931
};
19281932

19291933
HooksDispatcherOnMountInDEV = {
1934+
// TODO: remove the arguments and change the implementation.
1935+
readCache<T>(
1936+
context: ReactContext<T>,
1937+
observedBits: void | number | boolean,
1938+
): T {
1939+
warnInvalidContextAccess();
1940+
return readContext(context, observedBits);
1941+
},
19301942
readContext<T>(
19311943
context: ReactContext<T>,
19321944
observedBits: void | number | boolean,
@@ -2054,6 +2066,14 @@ if (__DEV__) {
20542066
};
20552067

20562068
HooksDispatcherOnMountWithHookTypesInDEV = {
2069+
// TODO: remove the arguments and change the implementation.
2070+
readCache<T>(
2071+
context: ReactContext<T>,
2072+
observedBits: void | number | boolean,
2073+
): T {
2074+
warnInvalidContextAccess();
2075+
return readContext(context, observedBits);
2076+
},
20572077
readContext<T>(
20582078
context: ReactContext<T>,
20592079
observedBits: void | number | boolean,
@@ -2176,6 +2196,14 @@ if (__DEV__) {
21762196
};
21772197

21782198
HooksDispatcherOnUpdateInDEV = {
2199+
// TODO: remove the arguments and change the implementation.
2200+
readCache<T>(
2201+
context: ReactContext<T>,
2202+
observedBits: void | number | boolean,
2203+
): T {
2204+
warnInvalidContextAccess();
2205+
return readContext(context, observedBits);
2206+
},
21792207
readContext<T>(
21802208
context: ReactContext<T>,
21812209
observedBits: void | number | boolean,
@@ -2298,6 +2326,14 @@ if (__DEV__) {
22982326
};
22992327

23002328
HooksDispatcherOnRerenderInDEV = {
2329+
// TODO: remove the arguments and change the implementation.
2330+
readCache<T>(
2331+
context: ReactContext<T>,
2332+
observedBits: void | number | boolean,
2333+
): T {
2334+
warnInvalidContextAccess();
2335+
return readContext(context, observedBits);
2336+
},
23012337
readContext<T>(
23022338
context: ReactContext<T>,
23032339
observedBits: void | number | boolean,
@@ -2421,6 +2457,13 @@ if (__DEV__) {
24212457
};
24222458

24232459
InvalidNestedHooksDispatcherOnMountInDEV = {
2460+
readCache<T>(
2461+
// TODO: remove this argument.
2462+
context: ReactContext<T>,
2463+
observedBits: void | number | boolean,
2464+
): T {
2465+
return readContext(context, observedBits);
2466+
},
24242467
readContext<T>(
24252468
context: ReactContext<T>,
24262469
observedBits: void | number | boolean,
@@ -2558,6 +2601,14 @@ if (__DEV__) {
25582601
};
25592602

25602603
InvalidNestedHooksDispatcherOnUpdateInDEV = {
2604+
// TODO: remove the arguments and change the implementation.
2605+
readCache<T>(
2606+
context: ReactContext<T>,
2607+
observedBits: void | number | boolean,
2608+
): T {
2609+
warnInvalidContextAccess();
2610+
return readContext(context, observedBits);
2611+
},
25612612
readContext<T>(
25622613
context: ReactContext<T>,
25632614
observedBits: void | number | boolean,
@@ -2695,6 +2746,14 @@ if (__DEV__) {
26952746
};
26962747

26972748
InvalidNestedHooksDispatcherOnRerenderInDEV = {
2749+
// TODO: remove the arguments and change the implementation.
2750+
readCache<T>(
2751+
context: ReactContext<T>,
2752+
observedBits: void | number | boolean,
2753+
): T {
2754+
warnInvalidContextAccess();
2755+
return readContext(context, observedBits);
2756+
},
26982757
readContext<T>(
26992758
context: ReactContext<T>,
27002759
observedBits: void | number | boolean,

packages/react-reconciler/src/ReactFiberHooks.old.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,7 @@ function dispatchAction<S, A>(
18161816
}
18171817

18181818
export const ContextOnlyDispatcher: Dispatcher = {
1819+
readCache: readContext, // TODO
18191820
readContext,
18201821

18211822
useCallback: throwInvalidHookError,
@@ -1837,6 +1838,7 @@ export const ContextOnlyDispatcher: Dispatcher = {
18371838
};
18381839

18391840
const HooksDispatcherOnMount: Dispatcher = {
1841+
readCache: readContext, // TODO
18401842
readContext,
18411843

18421844
useCallback: mountCallback,
@@ -1858,6 +1860,7 @@ const HooksDispatcherOnMount: Dispatcher = {
18581860
};
18591861

18601862
const HooksDispatcherOnUpdate: Dispatcher = {
1863+
readCache: readContext, // TODO
18611864
readContext,
18621865

18631866
useCallback: updateCallback,
@@ -1879,6 +1882,7 @@ const HooksDispatcherOnUpdate: Dispatcher = {
18791882
};
18801883

18811884
const HooksDispatcherOnRerender: Dispatcher = {
1885+
readCache: readContext, // TODO
18821886
readContext,
18831887

18841888
useCallback: updateCallback,
@@ -1927,6 +1931,13 @@ if (__DEV__) {
19271931
};
19281932

19291933
HooksDispatcherOnMountInDEV = {
1934+
// TODO: remove the arguments and change the implementation.
1935+
readCache<T>(
1936+
context: ReactContext<T>,
1937+
observedBits: void | number | boolean,
1938+
): T {
1939+
return readContext(context, observedBits);
1940+
},
19301941
readContext<T>(
19311942
context: ReactContext<T>,
19321943
observedBits: void | number | boolean,
@@ -2054,6 +2065,13 @@ if (__DEV__) {
20542065
};
20552066

20562067
HooksDispatcherOnMountWithHookTypesInDEV = {
2068+
// TODO: remove the arguments and change the implementation.
2069+
readCache<T>(
2070+
context: ReactContext<T>,
2071+
observedBits: void | number | boolean,
2072+
): T {
2073+
return readContext(context, observedBits);
2074+
},
20572075
readContext<T>(
20582076
context: ReactContext<T>,
20592077
observedBits: void | number | boolean,
@@ -2176,6 +2194,13 @@ if (__DEV__) {
21762194
};
21772195

21782196
HooksDispatcherOnUpdateInDEV = {
2197+
// TODO: remove the arguments and change the implementation.
2198+
readCache<T>(
2199+
context: ReactContext<T>,
2200+
observedBits: void | number | boolean,
2201+
): T {
2202+
return readContext(context, observedBits);
2203+
},
21792204
readContext<T>(
21802205
context: ReactContext<T>,
21812206
observedBits: void | number | boolean,
@@ -2298,6 +2323,13 @@ if (__DEV__) {
22982323
};
22992324

23002325
HooksDispatcherOnRerenderInDEV = {
2326+
// TODO: remove the arguments and change the implementation.
2327+
readCache<T>(
2328+
context: ReactContext<T>,
2329+
observedBits: void | number | boolean,
2330+
): T {
2331+
return readContext(context, observedBits);
2332+
},
23012333
readContext<T>(
23022334
context: ReactContext<T>,
23032335
observedBits: void | number | boolean,
@@ -2421,6 +2453,13 @@ if (__DEV__) {
24212453
};
24222454

24232455
InvalidNestedHooksDispatcherOnMountInDEV = {
2456+
// TODO: remove the arguments and change the implementation.
2457+
readCache<T>(
2458+
context: ReactContext<T>,
2459+
observedBits: void | number | boolean,
2460+
): T {
2461+
return readContext(context, observedBits);
2462+
},
24242463
readContext<T>(
24252464
context: ReactContext<T>,
24262465
observedBits: void | number | boolean,
@@ -2558,6 +2597,13 @@ if (__DEV__) {
25582597
};
25592598

25602599
InvalidNestedHooksDispatcherOnUpdateInDEV = {
2600+
// TODO: remove the arguments and change the implementation.
2601+
readCache<T>(
2602+
context: ReactContext<T>,
2603+
observedBits: void | number | boolean,
2604+
): T {
2605+
return readContext(context, observedBits);
2606+
},
25612607
readContext<T>(
25622608
context: ReactContext<T>,
25632609
observedBits: void | number | boolean,
@@ -2695,6 +2741,13 @@ if (__DEV__) {
26952741
};
26962742

26972743
InvalidNestedHooksDispatcherOnRerenderInDEV = {
2744+
// TODO: remove the arguments and change the implementation.
2745+
readCache<T>(
2746+
context: ReactContext<T>,
2747+
observedBits: void | number | boolean,
2748+
): T {
2749+
return readContext(context, observedBits);
2750+
},
26982751
readContext<T>(
26992752
context: ReactContext<T>,
27002753
observedBits: void | number | boolean,

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ type BasicStateAction<S> = (S => S) | S;
274274
type Dispatch<A> = A => void;
275275

276276
export type Dispatcher = {|
277+
readCache<T>(
278+
// TODO: remove these arguments.
279+
context: ReactContext<T>,
280+
observedBits: void | number | boolean,
281+
): T,
277282
readContext<T>(
278283
context: ReactContext<T>,
279284
observedBits: void | number | boolean,

0 commit comments

Comments
 (0)