History

Use Effect exit for local update check

Rhys Sullivan committed 6 months ago

Commit c1c64301 parent b039b6e1 file · +21 −9Browse files

Unified · Split

apps/local/src/web/shell.tsx+21−9
@@ -1,6 +1,7 @@
11 import { Link, Outlet, useLocation } from "@tanstack/react-router";
22 import { useCallback, useEffect, useRef, useState } from "react";
33 import { useAtomRefresh } from "@effect/atom-react";
4+import { Data, Effect, Exit } from "effect";
45 import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
56 import { sourcesAtom, toolsAtom } from "@executor-js/react/api/atoms";
67 import { useSourcesWithPending } from "@executor-js/react/api/optimistic";
@@ -29,6 +30,10 @@ type UpdateChannel = "latest" | "beta"; · 22 unmodified lines
2930
3031 const EXECUTOR_DIST_TAGS_PATH = "/v1/app/npm/dist-tags";
3132
33+class LatestVersionCheckError extends Data.TaggedError("LatestVersionCheckError")<{
34+ readonly cause: unknown;
35+}> {}
36+
3237 type ParsedVersion = {
3338 readonly major: number;
3439 readonly minor: number;
@@ -95,15 +100,22 @@ function useLatestVersion(currentVersion: string) { · 60 unmodified lines
95100
96101 useEffect(() => {
97102 let cancelled = false;
98− fetch(EXECUTOR_DIST_TAGS_PATH)
99− .then(async (res) => {
100− if (!res.ok) throw new Error(`Failed to load dist tags: ${res.status}`);
101− return res.json() as Promise<Partial<Record<UpdateChannel, string>>>;
102− })
103− .then((data) => {
104− if (!cancelled) setLatestVersion(data[channel] ?? null);
105− })
106− .catch(() => {});
103+
104+ const latestVersionEffect = Effect.tryPromise({
105+ try: async () => {
106+ const res = await fetch(EXECUTOR_DIST_TAGS_PATH);
107+ if (!res.ok) return {};
108+ return (await res.json()) as Partial<Record<UpdateChannel, string>>;
109+ },
110+ catch: (cause) => new LatestVersionCheckError({ cause }),
111+ });
112+
113+ void Effect.runPromiseExit(latestVersionEffect).then((exit) => {
114+ if (!cancelled && Exit.isSuccess(exit)) {
115+ setLatestVersion(exit.value[channel] ?? null);
116+ }
117+ });
118+
107119 return () => {
108120 cancelled = true;
109121 };