Skip to content

Commit

Permalink
Merge branch 'silh-conditional-module-timer-cleanup'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 25, 2024
2 parents e45cbb0 + e416a64 commit cf8e3c5
Show file tree
Hide file tree
Showing 3 changed files with 1,601 additions and 1,586 deletions.
16 changes: 8 additions & 8 deletions lib/conditional.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export class ConditionalModule {
condition: string | ((env: NodeJS.ProcessEnv) => boolean),
options?: { timeout: number },
) {
let configResolved = false;
const { timeout = 5000 } = options ?? {};
setTimeout(() => {
if (!configResolved) {
throw new Error(
`Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`,
);
}

const timer = setTimeout(() => {
throw new Error(
`Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`,
);
}, timeout);
timer.unref();

const returnModule: Required<
Pick<DynamicModule, 'module' | 'imports' | 'exports'>
> = { module: ConditionalModule, imports: [], exports: [] };
Expand All @@ -32,7 +32,7 @@ export class ConditionalModule {
};
}
await ConfigModule.envVariablesLoaded;
configResolved = true;
clearTimeout(timer);
const evaluation = condition(process.env);
if (evaluation) {
returnModule.imports.push(module);
Expand Down
Loading

0 comments on commit cf8e3c5

Please sign in to comment.