Skip to content

Commit

Permalink
refactor: rename ALS instances to prevent bad auto imports (#73480)
Browse files Browse the repository at this point in the history
this is a little quality-of-life improvement, with no behavioral
changes. it's slightly silly though.

for bundling reasons, our ALSes should only ever be imported from
`some-async-storage.external`, not `some-async-storage-instance`.
unfortunately editor autocomplete doesn't care about that, and always
suggests `-instance` first.

we can solve this by renaming the undesireable one from
`someAsyncStorage` to `someAsyncStorageInstace`, which makes it rank
lower in the autocomplete suggestions, so the top suggestion becomes the
correct one.

before the rename:
<img width="617" alt="Screenshot 2024-12-03 at 17 53 24"
src="https://github.com/user-attachments/assets/750f653a-f108-4fbf-9da5-4825dac6389d">
after the rename:
<img width="587" alt="Screenshot 2024-12-03 at 17 52 45"
src="https://github.com/user-attachments/assets/fa016050-9df3-44fb-b0c1-2821f84696dc">

### Alternatives considered
marking the instance with `/** @ignore */` doesn't work, because JSDoc
comments propagate to reexports, so it hides *both* from autocomplete,
which sucks.
  • Loading branch information
lubieowoce authored Dec 3, 2024
1 parent e5336c2 commit 1f44f13
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ActionAsyncStorage } from './action-async-storage.external'
import { createAsyncLocalStorage } from './async-local-storage'

export const actionAsyncStorage: ActionAsyncStorage = createAsyncLocalStorage()
export const actionAsyncStorageInstance: ActionAsyncStorage =
createAsyncLocalStorage()
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AsyncLocalStorage } from 'async_hooks'

// Share the instance module in the next-shared layer
import { actionAsyncStorage } from './action-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
import { actionAsyncStorageInstance } from './action-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
export interface ActionStore {
readonly isAction?: boolean
readonly isAppRoute?: boolean
}

export type ActionAsyncStorage = AsyncLocalStorage<ActionStore>

export { actionAsyncStorage }
export { actionAsyncStorageInstance as actionAsyncStorage }
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { WorkAsyncStorage } from './work-async-storage.external'
import { createAsyncLocalStorage } from './async-local-storage'

export const workAsyncStorage: WorkAsyncStorage = createAsyncLocalStorage()
export const workAsyncStorageInstance: WorkAsyncStorage =
createAsyncLocalStorage()
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AfterContext } from '../after/after-context'
import type { CacheLife } from '../use-cache/cache-life'

// Share the instance module in the next-shared layer
import { workAsyncStorage } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
import { workAsyncStorageInstance } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' }

export interface WorkStore {
readonly isStaticGeneration: boolean
Expand Down Expand Up @@ -73,4 +73,4 @@ export interface WorkStore {

export type WorkAsyncStorage = AsyncLocalStorage<WorkStore>

export { workAsyncStorage }
export { workAsyncStorageInstance as workAsyncStorage }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAsyncLocalStorage } from './async-local-storage'
import type { WorkUnitAsyncStorage } from './work-unit-async-storage.external'

export const workUnitAsyncStorage: WorkUnitAsyncStorage =
export const workUnitAsyncStorageInstance: WorkUnitAsyncStorage =
createAsyncLocalStorage()
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { CacheSignal } from './cache-signal'
import type { DynamicTrackingState } from './dynamic-rendering'

// Share the instance module in the next-shared layer
import { workUnitAsyncStorage } from './work-unit-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
import { workUnitAsyncStorageInstance } from './work-unit-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
import type { ServerComponentsHmrCache } from '../response-cache'
import type {
RenderResumeDataCache,
Expand Down Expand Up @@ -176,12 +176,12 @@ export type WorkUnitStore = RequestStore | CacheStore | PrerenderStore

export type WorkUnitAsyncStorage = AsyncLocalStorage<WorkUnitStore>

export { workUnitAsyncStorage }
export { workUnitAsyncStorageInstance as workUnitAsyncStorage }

export function getExpectedRequestStore(
callingExpression: string
): RequestStore {
const workUnitStore = workUnitAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorageInstance.getStore()
if (workUnitStore) {
if (workUnitStore.type === 'request') {
return workUnitStore
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '../../../lib/constants'
import { toRoute } from '../to-route'
import { SharedRevalidateTimings } from './shared-revalidate-timings'
import { workUnitAsyncStorage } from '../../app-render/work-unit-async-storage-instance'
import { workUnitAsyncStorageInstance } from '../../app-render/work-unit-async-storage-instance'
import {
getPrerenderResumeDataCache,
getRenderResumeDataCache,
Expand Down Expand Up @@ -405,7 +405,7 @@ export class IncrementalCache implements IncrementalCacheType {
// unlike other caches if we have a cacheScope we use it even if
// testmode would normally disable it or if requestHeaders say 'no-cache'.
if (this.hasDynamicIO && ctx.kind === IncrementalCacheKind.FETCH) {
const workUnitStore = workUnitAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorageInstance.getStore()
const resumeDataCache = workUnitStore
? getRenderResumeDataCache(workUnitStore)
: null
Expand Down Expand Up @@ -550,7 +550,7 @@ export class IncrementalCache implements IncrementalCacheType {
// is a transient in memory cache that populates caches ahead of a dynamic render in dev mode
// to allow the RSC debug info to have the right environment associated to it.
if (this.hasDynamicIO && data?.kind === CachedRouteKind.FETCH) {
const workUnitStore = workUnitAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorageInstance.getStore()
const prerenderResumeDataCache = workUnitStore
? getPrerenderResumeDataCache(workUnitStore)
: null
Expand Down

0 comments on commit 1f44f13

Please sign in to comment.