summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/server.tsx
diff options
context:
space:
mode:
authorAdam <[email protected]>2026-02-12 09:49:14 -0600
committerGitHub <[email protected]>2026-02-12 09:49:14 -0600
commitff4414bb152acfddb5c0eb073c38bedc1df4ae14 (patch)
tree78381c67d21ef6f089647f6b19e7aa2976840dbc /packages/app/src/context/server.tsx
parent56ad2db02055955f926fda0e4a89055b22ead6f9 (diff)
downloadopencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.tar.gz
opencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.zip
chore: refactor packages/app files (#13236)
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Frank <[email protected]>
Diffstat (limited to 'packages/app/src/context/server.tsx')
-rw-r--r--packages/app/src/context/server.tsx131
1 files changed, 69 insertions, 62 deletions
diff --git a/packages/app/src/context/server.tsx b/packages/app/src/context/server.tsx
index 351407d91..5d3d0cf3a 100644
--- a/packages/app/src/context/server.tsx
+++ b/packages/app/src/context/server.tsx
@@ -6,6 +6,7 @@ import { Persist, persisted } from "@/utils/persist"
import { checkServerHealth } from "@/utils/server-health"
type StoredProject = { worktree: string; expanded: boolean }
+const HEALTH_POLL_INTERVAL_MS = 10_000
export function normalizeServerUrl(input: string) {
const trimmed = input.trim()
@@ -48,81 +49,51 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(
const healthy = () => state.healthy
- function setActive(input: string) {
- const url = normalizeServerUrl(input)
- if (!url) return
- setState("active", url)
- }
+ const defaultUrl = () => normalizeServerUrl(props.defaultUrl)
- function add(input: string) {
- const url = normalizeServerUrl(input)
- if (!url) return
+ function reconcileStartup() {
+ const fallback = defaultUrl()
+ if (!fallback) return
- const fallback = normalizeServerUrl(props.defaultUrl)
- if (fallback && url === fallback) {
+ const previousSidecarUrl = normalizeServerUrl(store.currentSidecarUrl)
+ const list = previousSidecarUrl ? store.list.filter((url) => url !== previousSidecarUrl) : store.list
+ if (!props.isSidecar) {
batch(() => {
- if (!store.list.includes(url)) {
- // Add the fallback url to the list if it's not already in the list
- setStore("list", store.list.length, url)
- }
- setState("active", url)
+ setStore("list", list)
+ if (store.currentSidecarUrl) setStore("currentSidecarUrl", "")
+ setState("active", fallback)
})
return
}
+ const nextList = list.includes(fallback) ? list : [...list, fallback]
batch(() => {
- if (!store.list.includes(url)) {
- setStore("list", store.list.length, url)
- }
- setState("active", url)
+ setStore("list", nextList)
+ setStore("currentSidecarUrl", fallback)
+ setState("active", fallback)
})
}
- function remove(input: string) {
- const url = normalizeServerUrl(input)
- if (!url) return
-
- const list = store.list.filter((x) => x !== url)
- const next = state.active === url ? (list[0] ?? normalizeServerUrl(props.defaultUrl) ?? "") : state.active
-
- batch(() => {
- setStore("list", list)
- setState("active", next)
- })
- }
+ function updateServerList(url: string, remove = false) {
+ if (remove) {
+ const list = store.list.filter((x) => x !== url)
+ const next = state.active === url ? (list[0] ?? defaultUrl() ?? "") : state.active
+ batch(() => {
+ setStore("list", list)
+ setState("active", next)
+ })
+ return
+ }
- createEffect(() => {
- if (!ready()) return
- if (state.active) return
- const url = normalizeServerUrl(props.defaultUrl)
- if (!url) return
batch(() => {
- // Remove the previous startup sidecar url
- if (store.currentSidecarUrl) {
- remove(store.currentSidecarUrl)
- }
-
- // Add the new sidecar url
- if (props.isSidecar && props.defaultUrl) {
- add(props.defaultUrl)
- setStore("currentSidecarUrl", props.defaultUrl)
+ if (!store.list.includes(url)) {
+ setStore("list", store.list.length, url)
}
-
setState("active", url)
})
- })
-
- const isReady = createMemo(() => ready() && !!state.active)
-
- const fetcher = platform.fetch ?? globalThis.fetch
- const check = (url: string) => checkServerHealth(url, fetcher).then((x) => x.healthy)
-
- createEffect(() => {
- const url = state.active
- if (!url) return
-
- setState("healthy", undefined)
+ }
+ function startHealthPolling(url: string) {
let alive = true
let busy = false
@@ -140,12 +111,48 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(
}
run()
- const interval = setInterval(run, 10_000)
-
- onCleanup(() => {
+ const interval = setInterval(run, HEALTH_POLL_INTERVAL_MS)
+ return () => {
alive = false
clearInterval(interval)
- })
+ }
+ }
+
+ function setActive(input: string) {
+ const url = normalizeServerUrl(input)
+ if (!url) return
+ setState("active", url)
+ }
+
+ function add(input: string) {
+ const url = normalizeServerUrl(input)
+ if (!url) return
+ updateServerList(url)
+ }
+
+ function remove(input: string) {
+ const url = normalizeServerUrl(input)
+ if (!url) return
+ updateServerList(url, true)
+ }
+
+ createEffect(() => {
+ if (!ready()) return
+ if (state.active) return
+ reconcileStartup()
+ })
+
+ const isReady = createMemo(() => ready() && !!state.active)
+
+ const fetcher = platform.fetch ?? globalThis.fetch
+ const check = (url: string) => checkServerHealth(url, fetcher).then((x) => x.healthy)
+
+ createEffect(() => {
+ const url = state.active
+ if (!url) return
+
+ setState("healthy", undefined)
+ onCleanup(startHealthPolling(url))
})
const origin = createMemo(() => projectsKey(state.active))