Skip to content

Commit

Permalink
feat: rename module option properties, adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uvera committed Jul 2, 2024
1 parent a3f0be4 commit ad3ca9c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
6 changes: 3 additions & 3 deletions lib/interfaces/schedule-module-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ScheduleModuleOptions {
disableCronJobDiscovery?: boolean;
disableIntervalDiscovery?: boolean;
disableTimeoutDiscovery?: boolean;
cronJobs: boolean;
intervals: boolean;
timeouts: boolean;
}
12 changes: 6 additions & 6 deletions lib/schedule.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ScheduleExplorer implements OnModuleInit {

switch (metadata) {
case SchedulerType.CRON: {
if (this.moduleOptions.disableCronJobDiscovery) {
if (!this.moduleOptions.cronJobs) {
return;
}
const cronMetadata = this.metadataAccessor.getCronMetadata(methodRef);
Expand All @@ -73,7 +73,7 @@ export class ScheduleExplorer implements OnModuleInit {
return this.schedulerOrchestrator.addCron(cronFn, cronMetadata!);
}
case SchedulerType.TIMEOUT: {
if (this.moduleOptions.disableTimeoutDiscovery) {
if (!this.moduleOptions.timeouts) {
return;
}
const timeoutMetadata =
Expand All @@ -91,7 +91,7 @@ export class ScheduleExplorer implements OnModuleInit {
);
}
case SchedulerType.INTERVAL: {
if (this.moduleOptions.disableIntervalDiscovery) {
if (!this.moduleOptions.intervals) {
return;
}
const intervalMetadata =
Expand Down Expand Up @@ -121,7 +121,7 @@ export class ScheduleExplorer implements OnModuleInit {

switch (metadata) {
case SchedulerType.CRON: {
if (this.moduleOptions.disableCronJobDiscovery) {
if (!this.moduleOptions.cronJobs) {
return;
}
this.logger.warn(
Expand All @@ -130,7 +130,7 @@ export class ScheduleExplorer implements OnModuleInit {
break;
}
case SchedulerType.TIMEOUT: {
if (this.moduleOptions.disableTimeoutDiscovery) {
if (!this.moduleOptions.timeouts) {
return;
}
this.logger.warn(
Expand All @@ -139,7 +139,7 @@ export class ScheduleExplorer implements OnModuleInit {
break;
}
case SchedulerType.INTERVAL: {
if (this.moduleOptions.disableIntervalDiscovery) {
if (!this.moduleOptions.intervals) {
return;
}
this.logger.warn(
Expand Down
16 changes: 14 additions & 2 deletions lib/schedule.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ import { SCHEDULE_MODULE_OPTIONS } from './schedule.constants';
providers: [SchedulerMetadataAccessor, SchedulerOrchestrator],
})
export class ScheduleModule {
static forRoot(options: ScheduleModuleOptions = {}): DynamicModule {
static forRoot(
options: Partial<ScheduleModuleOptions> = {
cronJobs: true,
intervals: true,
timeouts: true,
},
): DynamicModule {
const optionsWithDefaults = {
cronJobs: true,
intervals: true,
timeouts: true,
...options,
};
return {
global: true,
module: ScheduleModule,
Expand All @@ -21,7 +33,7 @@ export class ScheduleModule {
SchedulerRegistry,
{
provide: SCHEDULE_MODULE_OPTIONS,
useValue: options,
useValue: optionsWithDefaults,
},
],
exports: [SchedulerRegistry],
Expand Down
18 changes: 15 additions & 3 deletions tests/e2e/disabled-discovery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ describe('Cron', () => {
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
AppModule.registerCron({ disableCronJobDiscovery: true }),
AppModule.registerCron({
cronJobs: false,
intervals: true,
timeouts: true,
}),
],
}).compile();

Expand All @@ -34,7 +38,11 @@ describe('Interval', () => {
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
AppModule.registerInterval({ disableIntervalDiscovery: true }),
AppModule.registerInterval({
cronJobs: true,
intervals: false,
timeouts: true,
}),
],
}).compile();

Expand All @@ -59,7 +67,11 @@ describe('Timeout', () => {
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
AppModule.registerTimeout({ disableTimeoutDiscovery: true }),
AppModule.registerTimeout({
cronJobs: true,
intervals: true,
timeouts: false,
}),
],
}).compile();

Expand Down
36 changes: 30 additions & 6 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { ScheduleModuleOptions } from '../../lib/interfaces/schedule-module-opti
@Module({})
export class AppModule {
static registerTimeout(
scheduleModuleOptions: ScheduleModuleOptions = {},
scheduleModuleOptions: ScheduleModuleOptions = {
cronJobs: true,
intervals: true,
timeouts: true,
},
): DynamicModule {
return {
module: AppModule,
Expand All @@ -21,7 +25,11 @@ export class AppModule {
}

static registerRequestScopedTimeout(
scheduleModuleOptions: ScheduleModuleOptions = {},
scheduleModuleOptions: ScheduleModuleOptions = {
cronJobs: true,
intervals: true,
timeouts: true,
},
): DynamicModule {
return {
module: AppModule,
Expand All @@ -31,7 +39,11 @@ export class AppModule {
}

static registerInterval(
scheduleModuleOptions: ScheduleModuleOptions = {},
scheduleModuleOptions: ScheduleModuleOptions = {
cronJobs: true,
intervals: true,
timeouts: true,
},
): DynamicModule {
return {
module: AppModule,
Expand All @@ -41,7 +53,11 @@ export class AppModule {
}

static registerRequestScopedInterval(
scheduleModuleOptions: ScheduleModuleOptions = {},
scheduleModuleOptions: ScheduleModuleOptions = {
cronJobs: true,
intervals: true,
timeouts: true,
},
): DynamicModule {
return {
module: AppModule,
Expand All @@ -51,7 +67,11 @@ export class AppModule {
}

static registerCron(
scheduleModuleOptions: ScheduleModuleOptions = {},
scheduleModuleOptions: ScheduleModuleOptions = {
cronJobs: true,
intervals: true,
timeouts: true,
},
): DynamicModule {
return {
module: AppModule,
Expand All @@ -61,7 +81,11 @@ export class AppModule {
}

static registerRequestScopedCron(
scheduleModuleOptions: ScheduleModuleOptions = {},
scheduleModuleOptions: ScheduleModuleOptions = {
cronJobs: true,
intervals: true,
timeouts: true,
},
): DynamicModule {
return {
module: AppModule,
Expand Down

0 comments on commit ad3ca9c

Please sign in to comment.