MCPcopy
hub / github.com/anomalyco/opencode / waitForWorktree

Function waitForWorktree

packages/app/src/components/prompt-input/submit.ts:507–563  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

505 clearInput()
506
507 const waitForWorktree = async () => {
508 const worktree = WorktreeState.get(sdk().scope, sessionDirectory)
509 if (!worktree || worktree.status !== "pending") return true
510
511 if (sessionDirectory === projectDirectory) {
512 sync().set("session_status", session.id, { type: "busy" })
513 }
514
515 const controller = new AbortController()
516 const cleanup = () => {
517 if (sessionDirectory === projectDirectory) {
518 sync().set("session_status", session.id, { type: "idle" })
519 }
520 removeOptimisticMessage()
521 if (restoreInput()) restoreCommentItems(submission.target(), commentItems)
522 }
523
524 pending.set(pendingKey(session.id), { abort: controller, cleanup })
525
526 const abortWait = new Promise<Awaited<ReturnType<typeof WorktreeState.wait>>>((resolve) => {
527 if (controller.signal.aborted) {
528 resolve({ status: "failed", message: "aborted" })
529 return
530 }
531 controller.signal.addEventListener(
532 "abort",
533 () => {
534 resolve({ status: "failed", message: "aborted" })
535 },
536 { once: true },
537 )
538 })
539
540 const timeoutMs = 5 * 60 * 1000
541 const timer = { id: undefined as number | undefined }
542 const timeout = new Promise<Awaited<ReturnType<typeof WorktreeState.wait>>>((resolve) => {
543 timer.id = window.setTimeout(() => {
544 resolve({
545 status: "failed",
546 message: language.t("workspace.error.stillPreparing"),
547 })
548 }, timeoutMs)
549 })
550
551 const result = await Promise.race([
552 WorktreeState.wait(sdk().scope, sessionDirectory),
553 abortWait,
554 timeout,
555 ]).finally(() => {
556 if (timer.id === undefined) return
557 clearTimeout(timer.id)
558 })
559 pending.delete(pendingKey(session.id))
560 if (controller.signal.aborted) return false
561 if (result.status === "failed") throw new Error(result.message)
562 return true
563 }
564

Callers

nothing calls this directly

Calls 9

sdkFunction · 0.85
pendingKeyFunction · 0.85
addEventListenerMethod · 0.80
getMethod · 0.65
syncFunction · 0.50
resolveFunction · 0.50
setMethod · 0.45
waitMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected