Skip to content

Commit

Permalink
Use AsyncLocalStorage to extend the scope of the cache to micro tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Oct 23, 2022
1 parent db586ec commit e4de611
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,6 @@ module.exports = {
gate: 'readonly',
trustedTypes: 'readonly',
IS_REACT_ACT_ENVIRONMENT: 'readonly',
AsyncLocalStorage: 'readonly',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export const supportsRequestStorage = false;
export const requestStorage: AsyncLocalStorage<any> = (null: any);

export function beginWriting(destination: Destination) {}

export function writeChunk(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export const supportsRequestStorage = false;
export const requestStorage: AsyncLocalStorage<
Map<Function, mixed>,
> = (null: any);

export function beginWriting(destination: Destination) {}

export function writeChunk(destination: Destination, chunk: Chunk): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export const supportsRequestStorage = false;
export const requestStorage: AsyncLocalStorage<
Map<Function, mixed>,
> = (null: any);

export function beginWriting(destination: Destination) {}

export function writeChunk(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export const supportsRequestStorage = false;
export const requestStorage: AsyncLocalStorage<
Map<Function, mixed>,
> = (null: any);

export function beginWriting(destination: Destination) {}

export function writeChunk(destination: Destination, chunk: Chunk): void {
Expand Down
28 changes: 19 additions & 9 deletions packages/react-server/src/ReactFlightCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,30 @@

import type {CacheDispatcher} from 'react-reconciler/src/ReactInternalTypes';

import {
supportsRequestStorage,
requestStorage,
} from './ReactFlightServerConfig';

function createSignal(): AbortSignal {
return new AbortController().signal;
}

function resolveCache(): Map<Function, mixed> {
if (currentCache) return currentCache;
if (supportsRequestStorage) {
const cache = requestStorage.getStore();
if (cache) return cache;
}
// Since we override the dispatcher all the time, we're effectively always
// active and so to support cache() and fetch() outside of render, we yield
// an empty Map.
return new Map();
}

export const DefaultCacheDispatcher: CacheDispatcher = {
getCacheSignal(): AbortSignal {
if (!currentCache) {
throw new Error('Reading the cache is only supported while rendering.');
}
let entry: AbortSignal | void = (currentCache.get(createSignal): any);
let entry: AbortSignal | void = (resolveCache().get(createSignal): any);
if (entry === undefined) {
entry = createSignal();
// $FlowFixMe[incompatible-use] found when upgrading Flow
Expand All @@ -27,11 +41,7 @@ export const DefaultCacheDispatcher: CacheDispatcher = {
return entry;
},
getCacheForType<T>(resourceType: () => T): T {
if (!currentCache) {
throw new Error('Reading the cache is only supported while rendering.');
}

let entry: T | void = (currentCache.get(resourceType): any);
let entry: T | void = (resolveCache().get(resourceType): any);
if (entry === undefined) {
entry = resourceType();
// TODO: Warn if undefined?
Expand Down
6 changes: 5 additions & 1 deletion packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,11 @@ function flushCompletedChunks(
}

export function startWork(request: Request): void {
scheduleWork(() => performWork(request));
if (supportsRequestStorage) {
scheduleWork(() => requestStorage.run(request.cache, performWork, request));
} else {
scheduleWork(() => performWork(request));
}
}

export function startFlowing(request: Request, destination: Destination): void {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-server/src/ReactServerStreamConfigBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function flushBuffered(destination: Destination) {
// For now we support AsyncLocalStorage as a global for the "browser" builds
// TODO: Move this to some special WinterCG build.
export const supportsRequestStorage = typeof AsyncLocalStorage === 'function';
export const requestStorage = new AsyncLocalStorage();
export const requestStorage: AsyncLocalStorage<
Map<Function, mixed>,
> = supportsRequestStorage ? new AsyncLocalStorage() : (null: any);

const VIEW_SIZE = 512;
let currentView = null;
Expand Down
4 changes: 3 additions & 1 deletion packages/react-server/src/ReactServerStreamConfigNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export function flushBuffered(destination: Destination) {
}

export const supportsRequestStorage = true;
export const requestStorage = new AsyncLocalStorage();
export const requestStorage: AsyncLocalStorage<
Map<Function, mixed>,
> = new AsyncLocalStorage();

const VIEW_SIZE = 2048;
let currentView = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const writeChunk = $$$hostConfig.writeChunk;
export const writeChunkAndReturn = $$$hostConfig.writeChunkAndReturn;
export const completeWriting = $$$hostConfig.completeWriting;
export const flushBuffered = $$$hostConfig.flushBuffered;
export const supportsRequestStorage = $$$hostConfig.supportsRequestStorage;
export const requestStorage = $$$hostConfig.requestStorage;
export const close = $$$hostConfig.close;
export const closeWithError = $$$hostConfig.closeWithError;
export const stringToChunk = $$$hostConfig.stringToChunk;
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/__tests__/ReactFetch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ global.TextDecoder = require('util').TextDecoder;
global.Headers = require('node-fetch').Headers;
global.Request = require('node-fetch').Request;
global.Response = require('node-fetch').Response;
// Patch for Browser environments to be able to polyfill AsyncLocalStorage
global.AsyncLocalStorage = require('async_hooks').AsyncLocalStorage;

let fetchCount = 0;
async function fetchMock(resource, options) {
Expand Down Expand Up @@ -81,14 +83,16 @@ describe('ReactFetch', () => {
async function getData() {
const r1 = await fetch('hello');
const t1 = await r1.text();
const r2 = await fetch('hello');
const r2 = await fetch('world');
const t2 = await r2.text();
return t1 + ' ' + t2;
}
function Component() {
return use(getData());
}
expect(await render(Component)).toMatchInlineSnapshot(`"GET world []"`);
expect(await render(Component)).toMatchInlineSnapshot(
`"GET hello [] GET world []"`,
);
expect(fetchCount).toBe(2);
});

Expand Down
2 changes: 1 addition & 1 deletion scripts/error-codes/codes.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,5 @@
"455": "This CacheSignal was requested outside React which means that it is immediately aborted.",
"456": "Calling Offscreen.detach before instance handle has been set.",
"457": "acquireHeadResource encountered a resource type it did not expect: \"%s\". This is a bug in React.",
"458": "Currently React only supports one RSC renderer at a time"
"458": "Currently React only supports one RSC renderer at a time."
}
16 changes: 16 additions & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,19 @@ declare module 'pg/lib/utils' {
prepareValue(val: any): mixed,
};
}

declare class AsyncLocalStorage<T> {
disable(): void;
getStore(): T | void;
run(store: T, callback: (...args: any[]) => void, ...args: any[]): void;
enterWith(store: T): void;
}

declare module 'async_hooks' {
declare class AsyncLocalStorage<T> {
disable(): void;
getStore(): T | void;
run(store: T, callback: (...args: any[]) => void, ...args: any[]): void;
enterWith(store: T): void;
}
}
11 changes: 6 additions & 5 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ const bundles = [
global: 'ReactDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'async-hooks', 'react-dom'],
externals: ['react', 'util', 'async_hooks', 'react-dom'],
},
{
bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [],
Expand Down Expand Up @@ -350,7 +350,7 @@ const bundles = [
global: 'ReactDOMStatic',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'stream', 'react-dom'],
externals: ['react', 'util', 'async_hooks', 'stream', 'react-dom'],
},

/******* React DOM Fizz Server External Runtime *******/
Expand Down Expand Up @@ -394,7 +394,7 @@ const bundles = [
global: 'ReactServerDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'async-hooks', 'react-dom'],
externals: ['react', 'util', 'async_hooks', 'react-dom'],
},

/******* React Server DOM Webpack Client *******/
Expand Down Expand Up @@ -462,7 +462,7 @@ const bundles = [
bundleTypes: [FB_WWW_DEV, FB_WWW_PROD],
moduleType: RENDERER,
entry: 'react-server-dom-relay',
global: 'ReactFlightDOMRelayClient', // TODO: Rename to Reader
global: 'ReactFlightDOMRelayClient',
minifyWithProdErrorCodes: true,
wrapWithModuleBoundaries: false,
externals: [
Expand All @@ -477,7 +477,7 @@ const bundles = [
bundleTypes: [RN_FB_DEV, RN_FB_PROD],
moduleType: RENDERER,
entry: 'react-server-native-relay/server',
global: 'ReactFlightNativeRelayServer', // TODO: Rename to Writer
global: 'ReactFlightNativeRelayServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: [
Expand All @@ -486,6 +486,7 @@ const bundles = [
'JSResourceReferenceImpl',
'ReactNativeInternalFeatureFlags',
'util',
'async_hooks',
],
},

Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup/validate/eslintrc.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ module.exports = {
Uint8Array: 'readonly',
Promise: 'readonly',

// Temp
AsyncLocalStorage: 'readonly',

// Flight Webpack
__webpack_chunk_load__: 'readonly',
__webpack_require__: 'readonly',
Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup/validate/eslintrc.cjs2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ module.exports = {
Uint8Array: 'readonly',
Promise: 'readonly',

// Temp
AsyncLocalStorage: 'readonly',

// Flight Webpack
__webpack_chunk_load__: 'readonly',
__webpack_require__: 'readonly',
Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup/validate/eslintrc.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module.exports = {
Uint8Array: 'readonly',
Promise: 'readonly',

// Temp
AsyncLocalStorage: 'readonly',

// Flight Webpack
__webpack_chunk_load__: 'readonly',
__webpack_require__: 'readonly',
Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup/validate/eslintrc.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ module.exports = {
Uint8Array: 'readonly',
Promise: 'readonly',

// Temp
AsyncLocalStorage: 'readonly',

// jest
jest: 'readonly',

Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup/validate/eslintrc.rn.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ module.exports = {
TaskController: 'readonly',
reportError: 'readonly',

// Temp
AsyncLocalStorage: 'readonly',

// jest
jest: 'readonly',

Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup/validate/eslintrc.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ module.exports = {
Uint8Array: 'readonly',
Promise: 'readonly',

// Temp
AsyncLocalStorage: 'readonly',

// Flight Webpack
__webpack_chunk_load__: 'readonly',
__webpack_require__: 'readonly',
Expand Down

0 comments on commit e4de611

Please sign in to comment.