Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cb9811e93 | ||
|
|
a2b6fd8274 |
No files matched your search
@@ -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";
|
||||
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
@@ -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),
|
||||
|
||||
@@ -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 () => {
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
@@ -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");
|
||||
@@ -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 }),
|
||||
}),
|
||||
|
||||
Reference in new issue
Block a user