From 4efabc8adab7ce41f9cfc97108b696c8010788ab Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 1 Apr 2024 11:09:44 +0530 Subject: [PATCH] refactor: keep conditionals checks inline --- src/helpers.ts | 11 ----------- src/module_expression.ts | 32 +++++++++++++------------------- src/module_importer.ts | 29 ++++++++++++----------------- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 771aa79..fdd3f9f 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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()) -} diff --git a/src/module_expression.ts b/src/module_expression.ts index eecc9be..5f8c370 100644 --- a/src/module_expression.ts +++ b/src/module_expression.ts @@ -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 @@ -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 } @@ -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 | Container, ...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 }, @@ -167,10 +163,9 @@ 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 @@ -178,10 +173,9 @@ export function moduleExpression(expression: string, parentURL: URL | string) { return { async handle(resolver: ContainerResolver | Container, ...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 diff --git a/src/module_importer.ts b/src/module_importer.ts index 01b09a5..e2a6095 100644 --- a/src/module_importer.ts +++ b/src/module_importer.ts @@ -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' /** @@ -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 } @@ -98,10 +96,9 @@ export function moduleImporter( * Otherwise the return function asks for the resolver or container */ return async function (resolver: ContainerResolver | Container, ...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 }, @@ -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 @@ -157,10 +153,9 @@ export function moduleImporter( return { name: importFn.name, async handle(resolver: ContainerResolver | Container, ...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