Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.4 KB
Raw Blame
import type { JobDefinition } from "./index";

// Add handlers/{name}-job.ts. Set defineJob({ name: "{name}-job" }).
// Each entry uses a static import() so webpack code-splits handlers into separate chunks.
const jobLoaders = {
  "folder-deleted-job": () =>
    import("./handlers/folder-deleted-job").then((m) => m.folderDeletedJob),

  "link-tag-deleted-job": () =>
    import("./handlers/link-tag-deleted-job").then((m) => m.linkTagDeletedJob),

  "domain-deleted-job": () =>
    import("./handlers/domain-deleted-job").then((m) => m.domainDeletedJob),

  "process-shopify-order-job": () =>
    import("./handlers/process-shopify-order-job").then(
      (m) => m.processShopifyOrderJob,
    ),

  "welcome-user-job": () =>
    import("./handlers/welcome-user-job").then((m) => m.welcomeUserJob),
} as const satisfies Record<string, () => Promise<JobDefinition>>;

const jobCache = new Map<string, JobDefinition>();

export async function loadJob(
  name: string,
): Promise<JobDefinition | undefined> {
  const cached = jobCache.get(name);
  if (cached) return cached;

  const loader = jobLoaders[name as keyof typeof jobLoaders];
  if (!loader) return undefined;

  const job = await loader();
  if (job.name !== name) {
    throw new Error(`Job name mismatch: ${job.name} !== ${name}`);
  }

  jobCache.set(name, job);
  return job;
}

export const registeredJobNames = Object.keys(jobLoaders);