Skip to content

Commit

Permalink
refactor: keep conditionals checks inline
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 1, 2024
1 parent 997eed2 commit 4efabc8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 47 deletions.
11 changes: 0 additions & 11 deletions src/helpers.ts
Expand Up @@ -144,14 +144,3 @@ export async function resolveDefault(importPath: string, parentURL: URL | string

return moduleExports.default
}

/**
* - if `import.meta.hot` is true then the callback will always be evaluated
* - otherwise, the callback will be evaluated only when the cached value is missing
*/
export async function resolveCachedWithHotFallback(cachedValue: any, callback: () => any) {
// @ts-expect-error import.meta.hot is not defined in this context.
if (import.meta.hot) return await callback()

return cachedValue || (await callback())
}
32 changes: 13 additions & 19 deletions src/module_expression.ts
Expand Up @@ -8,9 +8,9 @@
*/

import { Container } from './container.js'
import { resolveDefault } from './helpers.js'
import { ContainerResolver } from './resolver.js'
import type { ModuleHandler, ModuleCallable } from './types.js'
import { resolveCachedWithHotFallback, resolveDefault } from './helpers.js'

/**
* The moduleExpression module works around a very specific pattern we use
Expand Down Expand Up @@ -106,11 +106,9 @@ export function moduleExpression(expression: string, parentURL: URL | string) {
*/
if (container) {
return async function (...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await resolveDefault(importPath, parentURL)
)

if (!defaultExport || 'hot' in import.meta) {
defaultExport = await resolveDefault(importPath, parentURL)
}
return container.call(await container.make(defaultExport), method, args)
} as ModuleCallable<T, Args>
}
Expand All @@ -119,11 +117,9 @@ export function moduleExpression(expression: string, parentURL: URL | string) {
* Otherwise the return function asks for the resolver or container
*/
return async function (resolver: ContainerResolver<any> | Container<any>, ...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await resolveDefault(importPath, parentURL)
)

if (!defaultExport || 'hot' in import.meta) {
defaultExport = await resolveDefault(importPath, parentURL)
}
return resolver.call(await resolver.make(defaultExport), method, args)
} as ModuleCallable<T, Args>
},
Expand Down Expand Up @@ -167,21 +163,19 @@ export function moduleExpression(expression: string, parentURL: URL | string) {
if (container) {
return {
async handle(...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await resolveDefault(importPath, parentURL)
)
if (!defaultExport || 'hot' in import.meta) {
defaultExport = await resolveDefault(importPath, parentURL)
}
return container.call(await container.make(defaultExport), method, args)
},
} as ModuleHandler<T, Args>
}

return {
async handle(resolver: ContainerResolver<any> | Container<any>, ...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await resolveDefault(importPath, parentURL)
)
if (!defaultExport || 'hot' in import.meta) {
defaultExport = await resolveDefault(importPath, parentURL)
}
return resolver.call(await resolver.make(defaultExport), method, args)
},
} as ModuleHandler<T, Args>
Expand Down
29 changes: 12 additions & 17 deletions src/module_importer.ts
Expand Up @@ -11,7 +11,6 @@ import { importDefault } from '@poppinss/utils'

import { Container } from './container.js'
import { ContainerResolver } from './resolver.js'
import { resolveCachedWithHotFallback } from './helpers.js'
import type { ModuleHandler, ModuleCallable, Constructor } from './types.js'

/**
Expand Down Expand Up @@ -86,10 +85,9 @@ export function moduleImporter(
*/
if (container) {
return async function (...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await importDefault(importFn)
)
if (!defaultExport || 'hot' in import.meta) {
defaultExport = await importDefault(importFn)
}
return container.call(await container.make(defaultExport), method, args)
} as ModuleCallable<T, Args>
}
Expand All @@ -98,10 +96,9 @@ export function moduleImporter(
* Otherwise the return function asks for the resolver or container
*/
return async function (resolver: ContainerResolver<any> | Container<any>, ...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await importDefault(importFn)
)
if (!defaultExport || 'hot' in import.meta) {
defaultExport = await importDefault(importFn)
}
return resolver.call(await resolver.make(defaultExport), method, args)
} as ModuleCallable<T, Args>
},
Expand Down Expand Up @@ -145,10 +142,9 @@ export function moduleImporter(
return {
name: importFn.name,
async handle(...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await importDefault(importFn)
)
if (!defaultExport || 'hot' in import.meta) {
defaultExport = await importDefault(importFn)
}
return container.call(await container.make(defaultExport), method, args)
},
} as ModuleHandler<T, Args>
Expand All @@ -157,10 +153,9 @@ export function moduleImporter(
return {
name: importFn.name,
async handle(resolver: ContainerResolver<any> | Container<any>, ...args: Args) {
defaultExport = await resolveCachedWithHotFallback(
defaultExport,
async () => await importDefault(importFn)
)
if (!defaultExport || 'hot' in import.meta) {
defaultExport = await importDefault(importFn)
}
return resolver.call(await resolver.make(defaultExport), method, args)
},
} as ModuleHandler<T, Args>
Expand Down

0 comments on commit 4efabc8

Please sign in to comment.