Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions apps/mobile/src/features/files/sourceHighlightingState.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import { AtomRegistry } from "effect/unstable/reactivity";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";

import {
createSourceHighlightAtomFamily,
isSourceHighlightError,
type SourceHighlightTokens,
} from "./sourceHighlightingState";

Expand Down Expand Up @@ -101,8 +104,9 @@ describe("sourceHighlightingState", () => {
});

it("exposes highlighter errors as a failed async result", async () => {
const cause = new Error("highlight failed");
const highlight = vi.fn(async () => {
throw new Error("highlight failed");
throw cause;
});
const sourceHighlightAtom = createSourceHighlightAtomFamily({ highlight });
const registry = AtomRegistry.make();
Expand All @@ -113,8 +117,17 @@ describe("sourceHighlightingState", () => {
});
const unmount = registry.mount(atom);

await vi.waitFor(() => {
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
const result = registry.get(atom);
const error = AsyncResult.isFailure(result)
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
: undefined;

expect(isSourceHighlightError(error)).toBe(true);
expect(error).toMatchObject({
path: "src/example.ts",
theme: "light",
cause,
});

unmount();
Expand Down
21 changes: 17 additions & 4 deletions apps/mobile/src/features/files/sourceHighlightingState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { Atom } from "effect/unstable/reactivity";

import {
Expand All @@ -22,9 +23,20 @@ type SourceHighlighter = (input: SourceHighlightInput) => Promise<SourceHighligh

class SourceHighlightCacheKey extends Data.Class<SourceHighlightInput> {}

class SourceHighlightError extends Data.TaggedError("SourceHighlightError")<{
readonly cause: unknown;
}> {}
export class SourceHighlightError extends Schema.TaggedErrorClass<SourceHighlightError>()(
"SourceHighlightError",
{
path: Schema.String,
theme: Schema.Literals(["light", "dark"]),
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Failed to highlight ${this.path} with the ${this.theme} theme.`;
}
}

export const isSourceHighlightError = Schema.is(SourceHighlightError);

export function createSourceHighlightAtomFamily(options?: {
readonly highlight?: SourceHighlighter;
Expand All @@ -36,7 +48,8 @@ export function createSourceHighlightAtomFamily(options?: {
Atom.make(
Effect.tryPromise({
try: () => highlight(request),
catch: (cause) => new SourceHighlightError({ cause }),
catch: (cause) =>
new SourceHighlightError({ path: request.path, theme: request.theme, cause }),
}),
).pipe(
Atom.setIdleTTL(idleTtlMs),
Expand Down
46 changes: 42 additions & 4 deletions apps/mobile/src/features/files/workspace-file-image-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import { AtomRegistry } from "effect/unstable/reactivity";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import { describe, expect, it, vi } from "vite-plus/test";

import { createWorkspaceFileImageAtomFamily } from "./workspace-file-image-cache";
import {
createWorkspaceFileImageAtomFamily,
isWorkspaceImagePrefetchError,
} from "./workspace-file-image-cache";

describe("workspaceFileImageAtom", () => {
it("reuses a prefetched image across route remounts", async () => {
Expand Down Expand Up @@ -48,14 +53,47 @@ describe("workspaceFileImageAtom", () => {
registry.dispose();
});

it("exposes prefetch failures", async () => {
it("exposes a false native prefetch result", async () => {
const imageAtom = createWorkspaceFileImageAtomFamily({ prefetch: async () => false });
const registry = AtomRegistry.make();
const atom = imageAtom("https://example.test/missing.png");
const unmount = registry.mount(atom);

await vi.waitFor(() => {
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
const result = registry.get(atom);
const error = AsyncResult.isFailure(result)
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
: undefined;

expect(isWorkspaceImagePrefetchError(error)).toBe(true);
expect(error).toMatchObject({ uri: "https://example.test/missing.png" });
expect(error).not.toHaveProperty("cause");

unmount();
registry.dispose();
});

it("preserves thrown prefetch causes", async () => {
const cause = new Error("native image prefetch failed");
const imageAtom = createWorkspaceFileImageAtomFamily({
prefetch: async () => {
throw cause;
},
});
const registry = AtomRegistry.make();
const atom = imageAtom("https://example.test/missing.png");
const unmount = registry.mount(atom);

await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
const result = registry.get(atom);
const error = AsyncResult.isFailure(result)
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
: undefined;

expect(isWorkspaceImagePrefetchError(error)).toBe(true);
expect(error).toMatchObject({
uri: "https://example.test/missing.png",
cause,
});

unmount();
Expand Down
20 changes: 15 additions & 5 deletions apps/mobile/src/features/files/workspace-file-image-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { Atom } from "effect/unstable/reactivity";

const WORKSPACE_IMAGE_IDLE_TTL_MS = 30 * 60_000;
Expand All @@ -8,10 +9,19 @@ type ImagePrefetch = (uri: string) => Promise<boolean>;

class WorkspaceImageCacheKey extends Data.Class<{ readonly uri: string }> {}

export class WorkspaceImagePrefetchError extends Data.TaggedError("WorkspaceImagePrefetchError")<{
readonly cause?: unknown;
readonly uri: string;
}> {}
export class WorkspaceImagePrefetchError extends Schema.TaggedErrorClass<WorkspaceImagePrefetchError>()(
"WorkspaceImagePrefetchError",
{
uri: Schema.String,
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return `Failed to prefetch workspace image ${this.uri}.`;
}
}

export const isWorkspaceImagePrefetchError = Schema.is(WorkspaceImagePrefetchError);

async function prefetchWithNativeImage(uri: string): Promise<boolean> {
const { Image } = await import("react-native");
Expand All @@ -35,7 +45,7 @@ export function createWorkspaceFileImageAtomFamily(options?: {
return key.uri;
},
catch: (cause) =>
cause instanceof WorkspaceImagePrefetchError
isWorkspaceImagePrefetchError(cause)
? cause
: new WorkspaceImagePrefetchError({ uri: key.uri, cause }),
}),
Expand Down
Loading