Unverified Commit 199b869d authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

refactor: avoid unnecessary after wrapper (#67463)

I think the wrap function is a leftover from before #66767. We don't
need to return a tuple here, and can just use the `AfterContext`
directly on the call site. This makes its optionality a bit more
obvious, and avoids the noop callback.
parent 3f11815b
...@@ -4,7 +4,7 @@ import { AsyncLocalStorage } from 'async_hooks' ...@@ -4,7 +4,7 @@ import { AsyncLocalStorage } from 'async_hooks'
import type { RequestStore } from '../../client/components/request-async-storage.external' import type { RequestStore } from '../../client/components/request-async-storage.external'
import type { AfterContext } from './after-context' import type { AfterContext } from './after-context'
describe('createAfterContext', () => { describe('AfterContext', () => {
// 'async-local-storage.ts' needs `AsyncLocalStorage` on `globalThis` at import time, // 'async-local-storage.ts' needs `AsyncLocalStorage` on `globalThis` at import time,
// so we have to do some contortions here to set it up before running anything else // so we have to do some contortions here to set it up before running anything else
type RASMod = type RASMod =
...@@ -13,7 +13,7 @@ describe('createAfterContext', () => { ...@@ -13,7 +13,7 @@ describe('createAfterContext', () => {
type AfterContextMod = typeof import('./after-context') type AfterContextMod = typeof import('./after-context')
let requestAsyncStorage: RASMod['requestAsyncStorage'] let requestAsyncStorage: RASMod['requestAsyncStorage']
let createAfterContext: AfterContextMod['createAfterContext'] let AfterContext: AfterContextMod['AfterContext']
let after: AfterMod['unstable_after'] let after: AfterMod['unstable_after']
beforeAll(async () => { beforeAll(async () => {
...@@ -26,7 +26,7 @@ describe('createAfterContext', () => { ...@@ -26,7 +26,7 @@ describe('createAfterContext', () => {
requestAsyncStorage = RASMod.requestAsyncStorage requestAsyncStorage = RASMod.requestAsyncStorage
const AfterContextMod = await import('./after-context') const AfterContextMod = await import('./after-context')
createAfterContext = AfterContextMod.createAfterContext AfterContext = AfterContextMod.AfterContext
const AfterMod = await import('./after') const AfterMod = await import('./after')
after = AfterMod.unstable_after after = AfterMod.unstable_after
...@@ -49,7 +49,7 @@ describe('createAfterContext', () => { ...@@ -49,7 +49,7 @@ describe('createAfterContext', () => {
onCloseCallback = cb onCloseCallback = cb
}) })
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
...@@ -116,7 +116,7 @@ describe('createAfterContext', () => { ...@@ -116,7 +116,7 @@ describe('createAfterContext', () => {
onCloseCallback = cb onCloseCallback = cb
}) })
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
...@@ -164,7 +164,7 @@ describe('createAfterContext', () => { ...@@ -164,7 +164,7 @@ describe('createAfterContext', () => {
onCloseCallback = cb onCloseCallback = cb
}) })
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
...@@ -255,7 +255,7 @@ describe('createAfterContext', () => { ...@@ -255,7 +255,7 @@ describe('createAfterContext', () => {
onCloseCallback = cb onCloseCallback = cb
}) })
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
...@@ -315,7 +315,7 @@ describe('createAfterContext', () => { ...@@ -315,7 +315,7 @@ describe('createAfterContext', () => {
throw new Error('onClose is broken for some reason') throw new Error('onClose is broken for some reason')
}) })
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
...@@ -353,7 +353,7 @@ describe('createAfterContext', () => { ...@@ -353,7 +353,7 @@ describe('createAfterContext', () => {
onCloseCallback = cb onCloseCallback = cb
}) })
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
...@@ -406,7 +406,7 @@ describe('createAfterContext', () => { ...@@ -406,7 +406,7 @@ describe('createAfterContext', () => {
const waitUntil = undefined const waitUntil = undefined
const onClose = jest.fn() const onClose = jest.fn()
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
...@@ -436,7 +436,7 @@ describe('createAfterContext', () => { ...@@ -436,7 +436,7 @@ describe('createAfterContext', () => {
const onClose = undefined const onClose = undefined
const afterContext = createAfterContext({ const afterContext = new AfterContext({
waitUntil, waitUntil,
onClose, onClose,
cacheScope: undefined, cacheScope: undefined,
......
...@@ -9,22 +9,13 @@ import type { RequestLifecycleOpts } from '../base-server' ...@@ -9,22 +9,13 @@ import type { RequestLifecycleOpts } from '../base-server'
import type { AfterCallback, AfterTask } from './after' import type { AfterCallback, AfterTask } from './after'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
export interface AfterContext {
run<T>(requestStore: RequestStore, callback: () => T): T
after(task: AfterTask): void
}
export type AfterContextOpts = { export type AfterContextOpts = {
waitUntil: RequestLifecycleOpts['waitUntil'] | undefined waitUntil: RequestLifecycleOpts['waitUntil'] | undefined
onClose: RequestLifecycleOpts['onClose'] | undefined onClose: RequestLifecycleOpts['onClose'] | undefined
cacheScope: CacheScope | undefined cacheScope: CacheScope | undefined
} }
export function createAfterContext(opts: AfterContextOpts): AfterContext { export class AfterContext {
return new AfterContextImpl(opts)
}
export class AfterContextImpl implements AfterContext {
private waitUntil: RequestLifecycleOpts['waitUntil'] | undefined private waitUntil: RequestLifecycleOpts['waitUntil'] | undefined
private onClose: RequestLifecycleOpts['onClose'] | undefined private onClose: RequestLifecycleOpts['onClose'] | undefined
private cacheScope: CacheScope | undefined private cacheScope: CacheScope | undefined
......
...@@ -20,7 +20,7 @@ import { ...@@ -20,7 +20,7 @@ import {
import { ResponseCookies, RequestCookies } from '../web/spec-extension/cookies' import { ResponseCookies, RequestCookies } from '../web/spec-extension/cookies'
import { DraftModeProvider } from './draft-mode-provider' import { DraftModeProvider } from './draft-mode-provider'
import { splitCookiesString } from '../web/utils' import { splitCookiesString } from '../web/utils'
import { createAfterContext, type AfterContext } from '../after/after-context' import { AfterContext } from '../after/after-context'
import type { RequestLifecycleOpts } from '../base-server' import type { RequestLifecycleOpts } from '../base-server'
function getHeaders(headers: Headers | IncomingHttpHeaders): ReadonlyHeaders { function getHeaders(headers: Headers | IncomingHttpHeaders): ReadonlyHeaders {
...@@ -84,8 +84,6 @@ export const withRequestStore: WithStore<RequestStore, RequestContext> = < ...@@ -84,8 +84,6 @@ export const withRequestStore: WithStore<RequestStore, RequestContext> = <
{ req, url, res, renderOpts }: RequestContext, { req, url, res, renderOpts }: RequestContext,
callback: (store: RequestStore) => Result callback: (store: RequestStore) => Result
): Result => { ): Result => {
const [wrapWithAfter, afterContext] = createAfterWrapper(renderOpts)
function defaultOnUpdateCookies(cookies: string[]) { function defaultOnUpdateCookies(cookies: string[]) {
if (res) { if (res) {
res.setHeader('Set-Cookie', cookies) res.setHeader('Set-Cookie', cookies)
...@@ -172,33 +170,33 @@ export const withRequestStore: WithStore<RequestStore, RequestContext> = < ...@@ -172,33 +170,33 @@ export const withRequestStore: WithStore<RequestStore, RequestContext> = <
reactLoadableManifest: renderOpts?.reactLoadableManifest || {}, reactLoadableManifest: renderOpts?.reactLoadableManifest || {},
assetPrefix: renderOpts?.assetPrefix || '', assetPrefix: renderOpts?.assetPrefix || '',
afterContext, afterContext: createAfterContext(renderOpts),
}
if (store.afterContext) {
return store.afterContext.run(store, () =>
storage.run(store, callback, store)
)
} }
return wrapWithAfter(store, () => storage.run(store, callback, store))
return storage.run(store, callback, store)
} }
function createAfterWrapper( function createAfterContext(
renderOpts: WrapperRenderOpts | undefined renderOpts: WrapperRenderOpts | undefined
): [ ): AfterContext | undefined {
wrap: <Result>(requestStore: RequestStore, callback: () => Result) => Result, if (!isAfterEnabled(renderOpts)) {
afterContext: AfterContext | undefined, return undefined
] {
const isAfterEnabled = renderOpts?.experimental?.after ?? false
if (!renderOpts || !isAfterEnabled) {
return [(_, callback) => callback(), undefined]
} }
const { waitUntil, onClose } = renderOpts const { waitUntil, onClose, ComponentMod } = renderOpts
const cacheScope = renderOpts.ComponentMod?.createCacheScope() const cacheScope = ComponentMod?.createCacheScope()
const afterContext = createAfterContext({ return new AfterContext({ waitUntil, onClose, cacheScope })
waitUntil, }
onClose,
cacheScope,
})
const wrap = <Result>(requestStore: RequestStore, callback: () => Result) =>
afterContext.run(requestStore, callback)
return [wrap, afterContext] function isAfterEnabled(
renderOpts: WrapperRenderOpts | undefined
): renderOpts is WrapperRenderOpts {
return renderOpts?.experimental?.after ?? false
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment