{"files": {"main.ts": "import {run} from './core.ts';\nimport {POLICY} from './policy.ts';\nexport function evaluate(payload: any): any { return run(payload, POLICY); }\n", "core.ts": "import {isRetryable, computeDue} from './helpers.ts';\nexport function run(payload: any, policy: any): any {\n  const now: number = payload.now;\n  const jobs: any[] = payload.jobs;\n  const byKey: any = new Map();\n  for (let i = 0; i < jobs.length; i++) {\n    const job: any = jobs[i];\n    byKey.set(job[policy.dedup_key], {job: job, index: i});\n  }\n  const selected: any[] = Array.from(byKey.values());\n  selected.sort((a: any, b: any) => a.index - b.index);\n  const ready: any[] = [];\n  const waiting: any[] = [];\n  const dead: any[] = [];\n  const cancelled: any[] = [];\n  for (const item of selected) {\n    const job: any = item.job;\n    const index: number = item.index;\n    const retryable: boolean = isRetryable(job.last_code, policy);\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push({id: job.id, index: index});\n      continue;\n    }\n    const age: number = now - job.created;\n    const stale: boolean = policy.stale_inclusive ? age >= policy.stale_after : age > policy.stale_after;\n    if (stale || job.attempt >= policy.max_attempts || (job.last_code !== 0 && !retryable)) {\n      dead.push({id: job.id, index: index});\n      continue;\n    }\n    const due: number = computeDue(job, policy);\n    if (due > now) {\n      waiting.push({id: job.id, at: due, index: index});\n    } else {\n      ready.push({id: job.id, priority: job.priority, created: job.created, index: index});\n    }\n  }\n  ready.sort((a: any, b: any) => {\n    const p: number = policy.priority_high_first ? b.priority - a.priority : a.priority - b.priority;\n    if (p !== 0) return p;\n    const c: number = policy.fifo_ties ? a.created - b.created : b.created - a.created;\n    if (c !== 0) return c;\n    return a.index - b.index;\n  });\n  if (policy.dead_order === 'id') {\n    dead.sort((a: any, b: any) => a.id < b.id ? -1 : a.id > b.id ? 1 : a.index - b.index);\n  } else {\n    dead.sort((a: any, b: any) => a.index - b.index);\n  }\n  waiting.sort((a: any, b: any) => a.index - b.index);\n  cancelled.sort((a: any, b: any) => a.index - b.index);\n  return {ready: ready.map((x: any) => x.id), waiting: waiting.map((x: any) => ({id: x.id, at: x.at})), dead: dead.map((x: any) => x.id), cancelled: cancelled.map((x: any) => x.id)};\n}\n", "policy.ts": "export const POLICY: any = {\"base_delay\": 200, \"cancel_wins\": false, \"dead_order\": \"input\", \"dedup_key\": \"key\", \"delay_cap\": 400, \"exponential\": false, \"fifo_ties\": false, \"max_attempts\": 4, \"priority_high_first\": false, \"retryable_codes\": [408, 503], \"stale_after\": 2000, \"stale_inclusive\": false};\n", "helpers.ts": "export function isRetryable(lastCode: number, policy: any): boolean {\n  return policy.retryable_codes.indexOf(lastCode) !== -1;\n}\nexport function computeDue(job: any, policy: any): number {\n  if (job.last_code === 0 || !isRetryable(job.last_code, policy)) return job.due;\n  const multiplier: number = policy.exponential ? Math.pow(2, Math.max(0, job.attempt - 1)) : 1;\n  const delay: number = Math.min(policy.base_delay * multiplier, policy.delay_cap);\n  return Math.max(job.due, job.failed_at + delay);\n}\n"}}