Line data Source code
1 : import {
2 : DEPLOYMENT_LISTING_BACKSTOP_MS,
3 : DEPLOYMENT_LISTING_NO_HINT_WAIT_MS
4 : } from '@/platform/workspace/api/deploymentListingTimeouts'
5 : import type { DeploymentHint } from '@/platform/workspace/deploymentHint'
6 : import {
7 : hasDeploymentLink,
8 : readDeploymentHint
9 : } from '@/platform/workspace/deploymentHint'
10 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
11 : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
12 :
13 0 : function bootHint(): DeploymentHint {
14 0 : if (hasDeploymentLink()) return 'deployment'
15 0 : return readDeploymentHint(useTeamWorkspaceStore().workspaceId)
16 : }
17 :
18 : /** Whether this page may boot on a deployment: the boot listing's answer, else false only for a confirmed Comfy Cloud hint. */
19 0 : export function couldBootOnDeployment(): boolean {
20 0 : const { bootDeployment } = useDeploymentPickStore()
21 0 : if (bootDeployment !== undefined) return bootDeployment !== null
22 0 : return bootHint() !== 'cloud'
23 : }
24 :
25 0 : function listingWaitMs(hint: DeploymentHint): number {
26 0 : if (hint === 'deployment') return DEPLOYMENT_LISTING_BACKSTOP_MS
27 0 : if (hint === 'unconfirmed') return 0
28 0 : return DEPLOYMENT_LISTING_NO_HINT_WAIT_MS
29 : }
30 :
31 : /** Waits at most `ms` for the page's first deployments listing to settle. */
32 0 : export function waitForDeploymentListing(ms: number): Promise<void> {
33 0 : let timeout: ReturnType<typeof setTimeout> | undefined
34 0 : return Promise.race([
35 : useDeploymentPickStore().loadOnce(),
36 : new Promise<void>((resolve) => {
37 0 : timeout = setTimeout(resolve, ms)
38 : })
39 : ]).finally(() => clearTimeout(timeout))
40 : }
41 :
42 : /** Waits for the boot listing to say where this page runs; `onLateDeployment` runs if a late listing names a deployment. */
43 0 : export async function waitForDeploymentPick(
44 : onLateDeployment: () => void = () => {}
45 : ): Promise<boolean> {
46 0 : const store = useDeploymentPickStore()
47 0 : const hint = bootHint()
48 0 : if (!couldBootOnDeployment()) {
49 0 : store.bootOnComfyCloud()
50 0 : return true
51 : }
52 0 : const listing = store.loadOnce()
53 0 : let timeout: ReturnType<typeof setTimeout> | undefined
54 0 : try {
55 : const confirmed = await Promise.race([
56 : listing.then(() => store.bootDeployment !== undefined),
57 : new Promise<false>((resolve) => {
58 0 : timeout = setTimeout(() => resolve(false), listingWaitMs(hint))
59 : })
60 : ])
61 0 : if (confirmed) return true
62 0 : store.bootOnComfyCloud()
63 0 : if (hint === 'deployment') return false
64 0 : void listing.then(() => {
65 0 : if (store.pickedDeployment !== null) onLateDeployment()
66 : })
67 0 : return true
68 : } finally {
69 0 : clearTimeout(timeout)
70 : }
71 : }
|