Skip to content

Commit 8616cef

Browse files
committed
fix(desktop): refresh local media when the primary reconnects
1 parent e9d2458 commit 8616cef

3 files changed

Lines changed: 41 additions & 22 deletions

File tree

‎apps/web/src/state/assets.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ import {
22
createAssetEnvironmentAtoms,
33
createProjectFaviconUrlAtomFamily,
44
} from "@t3tools/client-runtime/state/assets";
5+
import { Atom } from "effect/unstable/reactivity";
56

67
import { connectionAtomRuntime } from "../connection/runtime";
78
import { projectFaviconCache } from "../assets/projectFaviconCache";
89
import { isElectron } from "../env";
910
import { appAtomRegistry } from "../rpc/atomRegistry";
1011
import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
11-
import { environmentSession, readPreparedConnection } from "./session";
12+
import { environmentSession } from "./session";
13+
14+
const localMediaEnvironment = Atom.make((get) => {
15+
if (!isElectron) return null;
16+
const environmentId = get(primaryEnvironmentIdAtom);
17+
if (environmentId === null) return null;
18+
const connection = get(environmentSession.preparedConnectionValueAtom(environmentId));
19+
// The session's bootstrap config clears on disconnect and refreshes on reconnect.
20+
const config = get(environmentSession.initialConfigValueAtom(environmentId));
21+
return connection._tag === "None" || config === null
22+
? null
23+
: { environmentId, httpBaseUrl: connection.value.httpBaseUrl };
24+
});
1225

1326
export const assetEnvironment = createAssetEnvironmentAtoms(connectionAtomRuntime, {
14-
localMediaEnvironment: () => {
15-
if (!isElectron) return null;
16-
const environmentId = appAtomRegistry.get(primaryEnvironmentIdAtom);
17-
if (environmentId === null) return null;
18-
const connection = readPreparedConnection(environmentId);
19-
return connection === null ? null : { environmentId, httpBaseUrl: connection.httpBaseUrl };
20-
},
27+
localMediaEnvironment: () => appAtomRegistry.get(localMediaEnvironment),
28+
localMediaRefreshTrigger: localMediaEnvironment,
2129
});
2230

2331
export const projectFaviconUrlAtom = createProjectFaviconUrlAtomFamily({

‎packages/client-runtime/src/state/assets.test.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe("createAssetEnvironmentAtoms", () => {
6767
{ name: "remote success", path: "/tmp/clip.mp4", success: true },
6868
{ name: "relative path", path: "clip.mp4" },
6969
{ name: "no primary", path: "/tmp/clip.mp4", primary: "none" },
70+
{ name: "primary reconnect", path: "/tmp/frame.png", primary: "reconnecting" },
7071
{ name: "same environment", path: "/tmp/clip.mp4", primary: "same" },
7172
{ name: "non-media", path: "/tmp/report.html" },
7273
{ name: "authorization failure", path: "/tmp/clip.mp4", error: "auth" },
@@ -133,20 +134,22 @@ describe("createAssetEnvironmentAtoms", () => {
133134
followStream: (id, stream) =>
134135
Stream.provideService(stream, EnvironmentSupervisor, supervisors.get(id)!),
135136
} as EnvironmentRegistry["Service"]);
137+
const registry = AtomRegistry.make();
138+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
139+
const localTarget = {
140+
environmentId: scenario.primary === "same" ? remoteId : localId,
141+
httpBaseUrl: "https://local.test",
142+
};
143+
const localEnvironment = Atom.make<typeof localTarget | null>(
144+
scenario.primary === "none" || scenario.primary === "reconnecting" ? null : localTarget,
145+
);
136146
const assets = createAssetEnvironmentAtoms(
137147
Atom.runtime(Layer.succeed(EnvironmentRegistry, environments)),
138148
{
139-
localMediaEnvironment: () =>
140-
scenario.primary === "none"
141-
? null
142-
: {
143-
environmentId: scenario.primary === "same" ? remoteId : localId,
144-
httpBaseUrl: "https://local.test",
145-
},
149+
localMediaEnvironment: () => registry.get(localEnvironment),
150+
localMediaRefreshTrigger: localEnvironment,
146151
},
147152
);
148-
const registry = AtomRegistry.make();
149-
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
150153
const query = assets.createUrl({ environmentId: remoteId, input: { resource } });
151154
const result = AtomRegistry.getResult(registry, query, { suspendOnWaiting: true });
152155
if (scenario.fallback || scenario.success) {
@@ -159,6 +162,11 @@ describe("createAssetEnvironmentAtoms", () => {
159162
expect(yield* Effect.flip(result)).toEqual(error);
160163
}
161164
expect(calls).toEqual(scenario.fallback ? [remoteId, localId] : [remoteId]);
165+
if (scenario.primary === "reconnecting") {
166+
registry.set(localEnvironment, localTarget);
167+
expect((yield* result).relativeUrl).toBe("https://local.test/api/assets/local/media");
168+
expect(calls).toEqual([remoteId, remoteId, localId]);
169+
}
162170
}).pipe(Effect.scoped),
163171
);
164172
}

‎packages/client-runtime/src/state/assets.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import * as Option from "effect/Option";
2020
import * as Schema from "effect/Schema";
2121
import { AsyncResult, Atom } from "effect/unstable/reactivity";
2222

23-
import { EnvironmentRegistry } from "../connection/registry.ts";
24-
import { EnvironmentSupervisor } from "../connection/supervisor.ts";
23+
import * as EnvironmentRegistry from "../connection/registry.ts";
24+
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
2525
import { request } from "../rpc/client.ts";
2626
import type { ProjectFaviconCache, ProjectFaviconTarget } from "../projectFaviconCache.ts";
2727
import { createEnvironmentQueryAtomFamily } from "./runtime.ts";
@@ -99,12 +99,13 @@ export function assetUrlStateFromResult(
9999
}
100100

101101
export function createAssetEnvironmentAtoms<R, E>(
102-
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
102+
runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, E>,
103103
options?: {
104104
readonly localMediaEnvironment?: () => {
105105
readonly environmentId: EnvironmentId;
106106
readonly httpBaseUrl: string;
107107
} | null;
108+
readonly localMediaRefreshTrigger?: Atom.Atom<unknown>;
108109
},
109110
) {
110111
const execute = Effect.fn("assets.createUrl")(function* (input: AssetCreateUrlInput) {
@@ -124,9 +125,9 @@ export function createAssetEnvironmentAtoms<R, E>(
124125
)
125126
return yield* error;
126127
const local = options?.localMediaEnvironment?.();
127-
const supervisor = yield* EnvironmentSupervisor;
128+
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
128129
if (!local || local.environmentId === supervisor.target.environmentId) return yield* error;
129-
const registry = yield* EnvironmentRegistry;
130+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
130131
const result = yield* registry.run(
131132
local.environmentId,
132133
request(WS_METHODS.assetsCreateUrl, input),
@@ -148,6 +149,8 @@ export function createAssetEnvironmentAtoms<R, E>(
148149
staleTimeMs: ASSET_URL_STALE_TIME_MS,
149150
idleTtlMs: ASSET_URL_IDLE_TTL_MS,
150151
refreshIntervalMs: ASSET_URL_REFRESH_INTERVAL_MS,
152+
refreshTrigger: ({ input }) =>
153+
input.resource._tag === "media-file" ? options?.localMediaRefreshTrigger : undefined,
151154
});
152155
const createUrlsFamily = Atom.family((key: string) => {
153156
const [environmentId, resources] = parseAssetCollectionKey(key);

0 commit comments

Comments
 (0)