Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] Add getCacheForType() to the dispatcher #20315

Merged
merged 13 commits into from
Dec 3, 2020
21 changes: 18 additions & 3 deletions fixtures/flight/server/cli.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@ const app = express();
// Application
app.get('/', function(req, res) {
if (process.env.NODE_ENV === 'development') {
for (var key in require.cache) {
delete require.cache[key];
}
// This doesn't work in ESM mode.
// for (var key in require.cache) {
// delete require.cache[key];
// }
}
require('./handler.server.js')(req, res);
});

app.get('/todos', function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.json([
{
id: 1,
text: 'Shave yaks',
},
{
id: 2,
text: 'Eat kale',
},
]);
});

app.listen(3001, () => {
console.log('Flight Server listening on port 3001...');
});
Expand Down
7 changes: 7 additions & 0 deletions fixtures/flight/src/App.server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import {fetch} from 'react-fetch';

import Container from './Container.js';

Expand All @@ -8,11 +9,17 @@ import {Counter as Counter2} from './Counter2.client.js';
import ShowMore from './ShowMore.client.js';

export default function App() {
const todos = fetch('http://localhost:3001/todos').json();
return (
<Container>
<h1>Hello, world</h1>
<Counter />
<Counter2 />
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.text}</li>
))}
</ul>
<ShowMore>
<p>Lorem ipsum</p>
</ShowMore>
Expand Down
7 changes: 7 additions & 0 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
MutableSourceSubscribeFn,
ReactContext,
ReactProviderType,
ReactCache,
} from 'shared/ReactTypes';
import type {
Fiber,
Expand All @@ -23,6 +24,7 @@ import type {OpaqueIDType} from 'react-reconciler/src/ReactFiberHostConfig';
import {NoMode} from 'react-reconciler/src/ReactTypeOfMode';

import ErrorStackParser from 'error-stack-parser';
import invariant from 'shared/invariant';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {REACT_OPAQUE_ID_TYPE} from 'shared/ReactSymbols';
import {
Expand Down Expand Up @@ -100,6 +102,10 @@ function nextHook(): null | Hook {
return hook;
}

function readCache(): ReactCache {
invariant(false, 'Not implemented.');
}

function readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -298,6 +304,7 @@ function useOpaqueIdentifier(): OpaqueIDType | void {
}

const Dispatcher: DispatcherType = {
readCache,
readContext,
useCallback,
useContext,
Expand Down
6 changes: 6 additions & 0 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
MutableSourceGetSnapshotFn,
MutableSourceSubscribeFn,
ReactContext,
ReactCache,
} from 'shared/ReactTypes';
import type PartialRenderer from './ReactPartialRenderer';

Expand Down Expand Up @@ -214,6 +215,10 @@ export function resetHooksState(): void {
workInProgressHook = null;
}

function readCache(): ReactCache {
invariant(false, 'Not implemented.');
}

function readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -492,6 +497,7 @@ export function setCurrentPartialRenderer(renderer: PartialRenderer) {
}

export const Dispatcher: DispatcherType = {
readCache,
readContext,
useContext,
useMemo,
Expand Down
12 changes: 10 additions & 2 deletions packages/react-fetch/src/ReactFetchBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* @flow
*/

import type {Wakeable} from 'shared/ReactTypes';
import type {Wakeable, ReactCache} from 'shared/ReactTypes';

import {readCache} from 'react/unstable-cache';
import * as React from 'react';

const Pending = 0;
const Resolved = 1;
Expand All @@ -36,6 +36,14 @@ type Result = PendingResult | ResolvedResult | RejectedResult;
const nativeFetch = window.fetch;
const fetchKey = {};

const ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
gaearon marked this conversation as resolved.
Show resolved Hide resolved
.ReactCurrentDispatcher;

function readCache(): ReactCache {
return ReactCurrentDispatcher.current.readCache();
}

function readResultMap(): Map<string, Result> {
const resources = readCache().resources;
let map = resources.get(fetchKey);
Expand Down
13 changes: 10 additions & 3 deletions packages/react-fetch/src/ReactFetchNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
* @flow
*/

import type {Wakeable} from 'shared/ReactTypes';
import type {Wakeable, ReactCache} from 'shared/ReactTypes';

import * as http from 'http';
import * as https from 'https';

import {readCache} from 'react/unstable-cache';
import * as React from 'react';

type FetchResponse = {|
// Properties
Expand Down Expand Up @@ -75,6 +74,14 @@ type RejectedResult = {|

type Result<V> = PendingResult | ResolvedResult<V> | RejectedResult;

const ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher;

function readCache(): ReactCache {
return ReactCurrentDispatcher.current.readCache();
}

const fetchKey = {};

function readResultMap(): Map<string, Result<FetchResponse>> {
Expand Down
Loading