{
  "version": 3,
  "sources": ["../../src/web/pages/project/lib/api/pollProject.ts"],
  "sourcesContent": ["import type { Project } from \"@framerjs/app-shared\"\nimport { delay, getLogger } from \"@framerjs/shared\"\nimport { experiments } from \"app/experiments.ts\"\nimport { apiFetcher } from \"web/lib/apiFetcher.ts\"\n\nconst log = getLogger(\"pollProject\")\n\nexport enum PollStatus {\n\tSuccess = 0,\n\tTimeoutError = 1,\n\tUnhandledError = 2,\n}\n\nexport type PollResponse =\n\t| {\n\t\t\tstatus: PollStatus.Success\n\t\t\tproject: Project\n\t  }\n\t| {\n\t\t\tstatus: PollStatus.TimeoutError\n\t\t\tmessage: string\n\t  }\n\t| {\n\t\t\tstatus: PollStatus.UnhandledError\n\t\t\terror?: unknown\n\t  }\n\nexport interface PollProjectOptions {\n\tintervalMillis: number\n\tattempts: number\n\tstopCondition: (project: Project) => boolean\n}\n\n/**\n * Plan updates may take some time to propagate through our billing pipeline and\n * start being reflected in the project metadata (most notably, in the project's\n * feature flags).\n *\n * But fret not! Provide this pollProject wrapper function with the site plan\n * the user just bought (or upgraded to), and it will poll for the update.\n */\nexport async function pollProject(projectId: string, options: PollProjectOptions): Promise<PollResponse> {\n\tconst { intervalMillis, attempts, stopCondition } = options\n\n\ttry {\n\t\tfor (let i = 0; i < attempts; i++) {\n\t\t\tconst project: Project = await apiFetcher.get(`/web/projects/${projectId}`, {\n\t\t\t\tincludeUsageDataV2: \"true\",\n\t\t\t\tincludeAiCreditLimit: experiments.isOn(\"agent\") ? \"true\" : undefined,\n\t\t\t})\n\n\t\t\tif (stopCondition(project)) {\n\t\t\t\treturn { status: PollStatus.Success, project }\n\t\t\t}\n\n\t\t\tif (i < attempts - 1) {\n\t\t\t\tawait delay(intervalMillis)\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tstatus: PollStatus.TimeoutError,\n\t\t\tmessage: `Polling exceeded ${attempts} attempts`,\n\t\t}\n\t} catch (error: unknown) {\n\t\tlog.reportError(error)\n\t\treturn {\n\t\t\tstatus: PollStatus.UnhandledError,\n\t\t\terror,\n\t\t}\n\t}\n}\n"],
  "mappings": ";;;;;;;;;;;;AAKA,IAAM,MAAM,UAAU,aAAa;AAoCnC,eAAsB,YAAY,WAAmB,SAAoD;AACxG,QAAM,EAAE,gBAAgB,UAAU,cAAc,IAAI;AAEpD,MAAI;AACH,aAAS,IAAI,GAAG,IAAI,UAAU,KAAK;AAClC,YAAM,UAAmB,MAAM,WAAW,IAAI,iBAAiB,SAAS,IAAI;AAAA,QAC3E,oBAAoB;AAAA,QACpB,sBAAsB,YAAY,KAAK,OAAO,IAAI,SAAS;AAAA,MAC5D,CAAC;AAED,UAAI,cAAc,OAAO,GAAG;AAC3B,eAAO,EAAE,QAAQ,iBAAoB,QAAQ;AAAA,MAC9C;AAEA,UAAI,IAAI,WAAW,GAAG;AACrB,cAAM,MAAM,cAAc;AAAA,MAC3B;AAAA,IACD;AAEA,WAAO;AAAA,MACN,QAAQ;AAAA,MACR,SAAS,oBAAoB,QAAQ;AAAA,IACtC;AAAA,EACD,SAAS,OAAgB;AACxB,QAAI,YAAY,KAAK;AACrB,WAAO;AAAA,MACN,QAAQ;AAAA,MACR;AAAA,IACD;AAAA,EACD;AACD;",
  "names": []
}
