Skip to content

Commit

Permalink
fix: fix a bug of id type not inferred
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiosaka committed Oct 25, 2023
1 parent a44ff5a commit 5d2d75a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 73 deletions.
79 changes: 10 additions & 69 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,37 @@
import type { Duration } from "date-fns";
import type { Types } from "mongoose";
import type Job from "./job";
import JobRunner from "./job-runner";
import type BaseJobStore from "./job-store";

/**
* @public
*/
export type { default as BaseJobLock } from "./job-lock";

/**
* @public
*/
export type { default as MongodbJobLock } from "./job-lock/mongodb";

/**
* @public
*/
export type { default as RedisJobLock } from "./job-lock/redis";

/**
* @public
*/
export type { default as TypeormJobLock } from "./job-lock/typeorm";

/**
* @public
*/
export type { default as BaseJobStore } from "./job-store";

/**
* @public
*/
export { default as MongodbJobStore } from "./job-store/mongodb";

/**
* @public
*/
export { default as RedisJobStore } from "./job-store/redis";

/**
* @public
*/
export { default as MysqlJobStore } from "./job-store/typeorm/mysql";

/**
* @public
*/
export { default as PostgresJobStore } from "./job-store/typeorm/postgres";

/**
* @public
*/
export { default as Job } from "./job";

/**
* @public
*/
export { CronyxError } from "./error";

/**
* @public
*/
export enum Source {
Mongodb = "mongodb",
Redis = "redis",
Mysql = "mysql",
Postgres = "Postgres",
}
export type JobLockId<S> = S extends BaseJobStore<infer I> ? I : never;

/**
* @public
*/
export type JobLockId<S extends Source> = S extends Source.Mongodb
? Types.ObjectId
: S extends Source.Redis
? string
: S extends Source.Mysql
? string
: S extends Source.Postgres
? string
: never;
export type CronyxOptions<S extends BaseJobStore<unknown>> = {
jobStore: S;
timezone?: string;
};

/**
* @public
*/
export type CronyxOptions<S extends Source> = {
jobStore: BaseJobStore<JobLockId<S>>;
timezone?: string;
};

type BaseRequestJobOptions = {
export type BaseRequestJobOptions = {
jobName: string;
jobInterval: Duration | string | number;
startBuffer?: Duration | number;
Expand All @@ -108,16 +49,16 @@ export type RequestJobOptions =
/**
* @public
*/
export default class Cronyx<S extends Source> {
#jobStore: BaseJobStore<JobLockId<S>>;
export default class Cronyx<S extends BaseJobStore<I>, I = JobLockId<S>> {
#jobStore: S;
#timezone: string | undefined;

constructor(options: CronyxOptions<S>) {
this.#jobStore = options.jobStore;
this.#timezone = options.timezone;
}

async requestJobExec(options: RequestJobOptions, task: (job: Job<JobLockId<S>>) => Promise<void>): Promise<void> {
async requestJobExec(options: RequestJobOptions, task: (job: Job<I>) => Promise<void>): Promise<void> {
const jobRunner = new JobRunner(this.#jobStore, options.jobName, options.jobInterval, {
timezone: this.#timezone,
requiredJobNames: options.requiredJobNames,
Expand All @@ -129,7 +70,7 @@ export default class Cronyx<S extends Source> {
return await jobRunner.requestJobExec(task);
}

async requestJobStart(options: RequestJobOptions): Promise<Job<JobLockId<S>> | null> {
async requestJobStart(options: RequestJobOptions): Promise<Job<I> | null> {
const jobRunner = new JobRunner(this.#jobStore, options.jobName, options.jobInterval, {
timezone: this.#timezone,
requiredJobNames: options.requiredJobNames,
Expand Down
8 changes: 4 additions & 4 deletions test/integrations/shared.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { afterEach, beforeEach, expect, Mock, mock, setSystemTime, test } from "bun:test";
import { add, sub } from "date-fns";
import Cronyx from "../../src";
import type { JobLockId, Source } from "../../src";
import type { JobLockId } from "../../src";
import type BaseJobStore from "../../src/job-store";

export function testBehavesLikeCronyx<S extends Source>(getJobStore: () => BaseJobStore<JobLockId<S>>) {
export function testBehavesLikeCronyx<S extends BaseJobStore<I>, I = JobLockId<S>>(getJobStore: () => S) {
const requestedAt = new Date("2023-02-03T15:00:00.000Z");
const firstJobIntervalStartedAt = sub(requestedAt, { days: 1 });
const firstJobIntervalEndedAt = requestedAt;
Expand All @@ -22,8 +22,8 @@ export function testBehavesLikeCronyx<S extends Source>(getJobStore: () => BaseJ
jobInterval: "0 0 * * * *", // hourly
};

let jobStore: BaseJobStore<JobLockId<S>>;
let cronyx: Cronyx<S>;
let jobStore: S;
let cronyx: Cronyx<S, I>;
let jobTask: Mock<() => Promise<void>>;
let requiredJobTask: Mock<() => Promise<void>>;

Expand Down

0 comments on commit 5d2d75a

Please sign in to comment.