GeezerVison is showing github.com — [Flight] Add getCacheForType() to the dispatcher (#20315) · react/react@e23673b
File treeExpand file treeCollapse file tree Open diff view settings react-suspense-test-utils
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/server/cli.server.js +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,13 +17,28 @@ const app = express(); | 17 | 17 | // Application | 18 | 18 | app.get('/', function(req, res) { | 19 | 19 | if (process.env.NODE_ENV === 'development') { | 20 | | - for (var key in require.cache) { | 21 | | - delete require.cache[key]; | 22 | | - } | | 20 | + // This doesn't work in ESM mode. | | 21 | + // for (var key in require.cache) { | | 22 | + // delete require.cache[key]; | | 23 | + // } | 23 | 24 | } | 24 | 25 | require('./handler.server.js')(req, res); | 25 | 26 | }); | 26 | 27 |
| | 28 | +app.get('/todos', function(req, res) { | | 29 | + res.setHeader('Access-Control-Allow-Origin', '*'); | | 30 | + res.json([ | | 31 | + { | | 32 | + id: 1, | | 33 | + text: 'Shave yaks', | | 34 | + }, | | 35 | + { | | 36 | + id: 2, | | 37 | + text: 'Eat kale', | | 38 | + }, | | 39 | + ]); | | 40 | +}); | | 41 | + | 27 | 42 | app.listen(3001, () => { | 28 | 43 | console.log('Flight Server listening on port 3001...'); | 29 | 44 | }); | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/src/App.server.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import * as React from 'react'; | | 2 | +import {fetch} from 'react-fetch'; | 2 | 3 |
| 3 | 4 | import Container from './Container.js'; | 4 | 5 |
| @@ -8,11 +9,17 @@ import {Counter as Counter2} from './Counter2.client.js'; | 8 | 9 | import ShowMore from './ShowMore.client.js'; | 9 | 10 |
| 10 | 11 | export default function App() { | | 12 | + const todos = fetch('http://localhost:3001/todos').json(); | 11 | 13 | return ( | 12 | 14 | <Container> | 13 | 15 | <h1>Hello, world</h1> | 14 | 16 | <Counter /> | 15 | 17 | <Counter2 /> | | 18 | + <ul> | | 19 | + {todos.map(todo => ( | | 20 | + <li key={todo.id}>{todo.text}</li> | | 21 | + ))} | | 22 | + </ul> | 16 | 23 | <ShowMore> | 17 | 24 | <p>Lorem ipsum</p> | 18 | 25 | </ShowMore> | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/ReactDebugHooks.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,7 @@ import type {OpaqueIDType} from 'react-reconciler/src/ReactFiberHostConfig'; | 23 | 23 | import {NoMode} from 'react-reconciler/src/ReactTypeOfMode'; | 24 | 24 |
| 25 | 25 | import ErrorStackParser from 'error-stack-parser'; | | 26 | +import invariant from 'shared/invariant'; | 26 | 27 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | 27 | 28 | import {REACT_OPAQUE_ID_TYPE} from 'shared/ReactSymbols'; | 28 | 29 | import { | @@ -100,6 +101,10 @@ function nextHook(): null | Hook { | 100 | 101 | return hook; | 101 | 102 | } | 102 | 103 |
| | 104 | +function getCacheForType<T>(resourceType: () => T): T { | | 105 | + invariant(false, 'Not implemented.'); | | 106 | +} | | 107 | + | 103 | 108 | function readContext<T>( | 104 | 109 | context: ReactContext<T>, | 105 | 110 | observedBits: void | number | boolean, | @@ -298,6 +303,7 @@ function useOpaqueIdentifier(): OpaqueIDType | void { | 298 | 303 | } | 299 | 304 |
| 300 | 305 | const Dispatcher: DispatcherType = { | | 306 | + getCacheForType, | 301 | 307 | readContext, | 302 | 308 | useCallback, | 303 | 309 | useContext, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactPartialRendererHooks.js +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ import type PartialRenderer from './ReactPartialRenderer'; | 20 | 20 | import {validateContextBounds} from './ReactPartialRendererContext'; | 21 | 21 |
| 22 | 22 | import invariant from 'shared/invariant'; | | 23 | +import {enableCache} from 'shared/ReactFeatureFlags'; | 23 | 24 | import is from 'shared/objectIs'; | 24 | 25 |
| 25 | 26 | type BasicStateAction<S> = (S => S) | S; | @@ -214,6 +215,10 @@ export function resetHooksState(): void { | 214 | 215 | workInProgressHook = null; | 215 | 216 | } | 216 | 217 |
| | 218 | +function getCacheForType<T>(resourceType: () => T): T { | | 219 | + invariant(false, 'Not implemented.'); | | 220 | +} | | 221 | + | 217 | 222 | function readContext<T>( | 218 | 223 | context: ReactContext<T>, | 219 | 224 | observedBits: void | number | boolean, | @@ -512,3 +517,7 @@ export const Dispatcher: DispatcherType = { | 512 | 517 | // Subscriptions are not setup in a server environment. | 513 | 518 | useMutableSource, | 514 | 519 | }; | | 520 | + | | 521 | +if (enableCache) { | | 522 | + Dispatcher.getCacheForType = getCacheForType; | | 523 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-fetch/src/ReactFetchBrowser.js +9-12Lines changed: 9 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | import type {Wakeable} from 'shared/ReactTypes'; | 11 | 11 |
| 12 | | -import {readCache} from 'react/unstable-cache'; | | 12 | +import {unstable_getCacheForType} from 'react'; | 13 | 13 |
| 14 | 14 | const Pending = 0; | 15 | 15 | const Resolved = 1; | @@ -34,16 +34,13 @@ type Result = PendingResult | ResolvedResult | RejectedResult; | 34 | 34 |
| 35 | 35 | // TODO: this is a browser-only version. Add a separate Node entry point. | 36 | 36 | const nativeFetch = window.fetch; | 37 | | -const fetchKey = {}; | 38 | | - | 39 | | -function readResultMap(): Map<string, Result> { | 40 | | - const resources = readCache().resources; | 41 | | - let map = resources.get(fetchKey); | 42 | | - if (map === undefined) { | 43 | | - map = new Map(); | 44 | | - resources.set(fetchKey, map); | 45 | | - } | 46 | | - return map; | | 37 | + | | 38 | +function getResultMap(): Map<string, Result> { | | 39 | + return unstable_getCacheForType(createResultMap); | | 40 | +} | | 41 | + | | 42 | +function createResultMap(): Map<string, Result> { | | 43 | + return new Map(); | 47 | 44 | } | 48 | 45 |
| 49 | 46 | function toResult(thenable): Result { | @@ -120,7 +117,7 @@ Response.prototype = { | 120 | 117 | }; | 121 | 118 |
| 122 | 119 | function preloadResult(url: string, options: mixed): Result { | 123 | | - const map = readResultMap(); | | 120 | + const map = getResultMap(); | 124 | 121 | let entry = map.get(url); | 125 | 122 | if (!entry) { | 126 | 123 | if (options) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-fetch/src/ReactFetchNode.js +7-12Lines changed: 7 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,8 +11,7 @@ import type {Wakeable} from 'shared/ReactTypes'; | 11 | 11 |
| 12 | 12 | import * as http from 'http'; | 13 | 13 | import * as https from 'https'; | 14 | | - | 15 | | -import {readCache} from 'react/unstable-cache'; | | 14 | +import {unstable_getCacheForType} from 'react'; | 16 | 15 |
| 17 | 16 | type FetchResponse = {| | 18 | 17 | // Properties | @@ -75,16 +74,12 @@ type RejectedResult = {| | 75 | 74 |
| 76 | 75 | type Result<V> = PendingResult | ResolvedResult<V> | RejectedResult; | 77 | 76 |
| 78 | | -const fetchKey = {}; | | 77 | +function getResultMap(): Map<string, Result<FetchResponse>> { | | 78 | + return unstable_getCacheForType(createResultMap); | | 79 | +} | 79 | 80 |
| 80 | | -function readResultMap(): Map<string, Result<FetchResponse>> { | 81 | | - const resources = readCache().resources; | 82 | | - let map = resources.get(fetchKey); | 83 | | - if (map === undefined) { | 84 | | - map = new Map(); | 85 | | - resources.set(fetchKey, map); | 86 | | - } | 87 | | - return map; | | 81 | +function createResultMap(): Map<string, Result<FetchResponse>> { | | 82 | + return new Map(); | 88 | 83 | } | 89 | 84 |
| 90 | 85 | function readResult<T>(result: Result<T>): T { | @@ -166,7 +161,7 @@ Response.prototype = { | 166 | 161 | }; | 167 | 162 |
| 168 | 163 | function preloadResult(url: string, options: mixed): Result<FetchResponse> { | 169 | | - const map = readResultMap(); | | 164 | + const map = getResultMap(); | 170 | 165 | let entry = map.get(url); | 171 | 166 | if (!entry) { | 172 | 167 | if (options) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-fetch/src/__tests__/ReactFetchNode-test.js +73-47Lines changed: 73 additions & 47 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | 'use strict'; | 11 | 11 |
| 12 | 12 | describe('ReactFetchNode', () => { | 13 | | - let ReactCache; | 14 | | - let ReactFetchNode; | 15 | 13 | let http; | 16 | 14 | let fetch; | | 15 | + let waitForSuspense; | 17 | 16 | let server; | 18 | 17 | let serverEndpoint; | 19 | 18 | let serverImpl; | 20 | 19 |
| 21 | 20 | beforeEach(done => { | 22 | 21 | jest.resetModules(); | 23 | | - if (__EXPERIMENTAL__) { | 24 | | - ReactCache = require('react/unstable-cache'); | 25 | | - // TODO: A way to pass load context. | 26 | | - ReactCache.CacheProvider._context._currentValue = ReactCache.createCache(); | 27 | | - ReactFetchNode = require('react-fetch'); | 28 | | - fetch = ReactFetchNode.fetch; | 29 | | - } | | 22 | + | | 23 | + fetch = require('react-fetch').fetch; | 30 | 24 | http = require('http'); | | 25 | + waitForSuspense = require('react-suspense-test-utils').waitForSuspense; | 31 | 26 |
| 32 | 27 | server = http.createServer((req, res) => { | 33 | 28 | serverImpl(req, res); | 34 | 29 | }); | 35 | | - server.listen(done); | 36 | | - serverEndpoint = `http://localhost:${server.address().port}/`; | | 30 | + serverEndpoint = null; | | 31 | + server.listen(() => { | | 32 | + serverEndpoint = `http://localhost:${server.address().port}/`; | | 33 | + done(); | | 34 | + }); | 37 | 35 | }); | 38 | 36 |
| 39 | 37 | afterEach(done => { | 40 | 38 | server.close(done); | 41 | 39 | server = null; | 42 | 40 | }); | 43 | 41 |
| 44 | | - async function waitForSuspense(fn) { | 45 | | - while (true) { | 46 | | - try { | 47 | | - return fn(); | 48 | | - } catch (promise) { | 49 | | - if (typeof promise.then === 'function') { | 50 | | - await promise; | 51 | | - } else { | 52 | | - throw promise; | 53 | | - } | 54 | | - } | 55 | | - } | 56 | | - } | | 42 | + // @gate experimental | | 43 | + it('can fetch text from a server component', async () => { | | 44 | + serverImpl = (req, res) => { | | 45 | + res.write('mango'); | | 46 | + res.end(); | | 47 | + }; | | 48 | + const text = await waitForSuspense(() => { | | 49 | + return fetch(serverEndpoint).text(); | | 50 | + }); | | 51 | + expect(text).toEqual('mango'); | | 52 | + }); | 57 | 53 |
| 58 | 54 | // @gate experimental | 59 | | - it('can read text', async () => { | | 55 | + it('can fetch json from a server component', async () => { | 60 | 56 | serverImpl = (req, res) => { | 61 | | - res.write('ok'); | | 57 | + res.write(JSON.stringify({name: 'Sema'})); | 62 | 58 | res.end(); | 63 | 59 | }; | 64 | | - await waitForSuspense(() => { | 65 | | - const response = fetch(serverEndpoint); | 66 | | - expect(response.status).toBe(200); | 67 | | - expect(response.statusText).toBe('OK'); | 68 | | - expect(response.ok).toBe(true); | 69 | | - expect(response.text()).toEqual('ok'); | 70 | | - // Can read again: | 71 | | - expect(response.text()).toEqual('ok'); | | 60 | + const json = await waitForSuspense(() => { | | 61 | + return fetch(serverEndpoint).json(); | 72 | 62 | }); | | 63 | + expect(json).toEqual({name: 'Sema'}); | 73 | 64 | }); | 74 | 65 |
| 75 | 66 | // @gate experimental | 76 | | - it('can read json', async () => { | | 67 | + it('provides response status', async () => { | 77 | 68 | serverImpl = (req, res) => { | 78 | 69 | res.write(JSON.stringify({name: 'Sema'})); | 79 | 70 | res.end(); | 80 | 71 | }; | 81 | | - await waitForSuspense(() => { | 82 | | - const response = fetch(serverEndpoint); | 83 | | - expect(response.status).toBe(200); | 84 | | - expect(response.statusText).toBe('OK'); | 85 | | - expect(response.ok).toBe(true); | 86 | | - expect(response.json()).toEqual({ | 87 | | - name: 'Sema', | 88 | | - }); | 89 | | - // Can read again: | 90 | | - expect(response.json()).toEqual({ | 91 | | - name: 'Sema', | 92 | | - }); | | 72 | + const response = await waitForSuspense(() => { | | 73 | + return fetch(serverEndpoint); | | 74 | + }); | | 75 | + expect(response).toMatchObject({ | | 76 | + status: 200, | | 77 | + statusText: 'OK', | | 78 | + ok: true, | 93 | 79 | }); | 94 | 80 | }); | | 81 | + | | 82 | + // @gate experimental | | 83 | + it('handles different paths', async () => { | | 84 | + serverImpl = (req, res) => { | | 85 | + switch (req.url) { | | 86 | + case '/banana': | | 87 | + res.write('banana'); | | 88 | + break; | | 89 | + case '/mango': | | 90 | + res.write('mango'); | | 91 | + break; | | 92 | + case '/orange': | | 93 | + res.write('orange'); | | 94 | + break; | | 95 | + } | | 96 | + res.end(); | | 97 | + }; | | 98 | + const outputs = await waitForSuspense(() => { | | 99 | + return [ | | 100 | + fetch(serverEndpoint + 'banana').text(), | | 101 | + fetch(serverEndpoint + 'mango').text(), | | 102 | + fetch(serverEndpoint + 'orange').text(), | | 103 | + ]; | | 104 | + }); | | 105 | + expect(outputs).toMatchObject(['banana', 'mango', 'orange']); | | 106 | + }); | | 107 | + | | 108 | + // @gate experimental | | 109 | + it('can produce an error', async () => { | | 110 | + serverImpl = (req, res) => {}; | | 111 | + | | 112 | + expect.assertions(1); | | 113 | + try { | | 114 | + await waitForSuspense(() => { | | 115 | + return fetch('BOOM'); | | 116 | + }); | | 117 | + } catch (err) { | | 118 | + expect(err.message).toEqual('Invalid URL: BOOM'); | | 119 | + } | | 120 | + }); | 95 | 121 | }); |
|