From b101f820046cff5bc85b4dfef2eef1e3d2361a79 Mon Sep 17 00:00:00 2001 From: Stephan Lagerwaard Date: Mon, 14 Oct 2024 14:51:49 +0200 Subject: [PATCH] Fix memoizeOne types --- packages/types/memoize-one.d.ts | 7 +++++++ packages/types/package.json | 3 ++- packages/types/src/schemas.ts | 8 +------- packages/types/tsconfig.json | 7 ++++++- pnpm-lock.yaml | 12 +++++++++++- studio/package.json | 1 + .../RuntimeProvider/TauriRuntime/TauriRuntime.tsx | 9 +++++---- .../components/RuntimeProvider/TauriRuntime/utils.ts | 11 ++++++++--- xtask/Cargo.toml | 2 +- xtask/src/commands/schemas.rs | 8 +------- 10 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 packages/types/memoize-one.d.ts diff --git a/packages/types/memoize-one.d.ts b/packages/types/memoize-one.d.ts new file mode 100644 index 000000000..95f4e94a6 --- /dev/null +++ b/packages/types/memoize-one.d.ts @@ -0,0 +1,7 @@ +declare module "memoize-one" { + // biome-ignore lint/suspicious/noExplicitAny: allow override + export default function memoizeOne any>( + resultFn: T, + isEqual?: (newArgs: Parameters, lastArgs: Parameters) => boolean, + ): T; +} diff --git a/packages/types/package.json b/packages/types/package.json index e811dc113..edcaf4f90 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -15,6 +15,7 @@ "build": "tsc" }, "devDependencies": { - "zod": "^3.23.8" + "zod": "^3.23.8", + "memoize-one": "^6.0.0" } } diff --git a/packages/types/src/schemas.ts b/packages/types/src/schemas.ts index cbc30e317..e402ff977 100644 --- a/packages/types/src/schemas.ts +++ b/packages/types/src/schemas.ts @@ -7,13 +7,7 @@ // =========================================== // import { z } from "zod"; - -// TODO: Polyfill memoizeOne for now, seems like this breaks our TypeScript setup -// See: https://github.com/alexreardon/memoize-one/issues/267 -// The suggested `.default` works in the types package but breaks studio -function memoizeOne(callback: () => T) { - return callback; -} +import memoizeOne from "memoize-one"; export const AppStateSchema = memoizeOne(() => z.object({ workspace: z.union([z.lazy(WorkspaceSchema), z.null()]) }), diff --git a/packages/types/tsconfig.json b/packages/types/tsconfig.json index f78ee049a..21c9b1de9 100644 --- a/packages/types/tsconfig.json +++ b/packages/types/tsconfig.json @@ -10,7 +10,12 @@ "esModuleInterop": true, "outDir": "./dist", "rootDir": "./src", - "declaration": true + "declaration": true, + "paths": { + "memoize-one": [ + "./src/memoize-one.d.ts" + ] + } }, "include": [ "src" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f3879e5bc..9a5f7e666 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -303,6 +303,9 @@ importers: packages/types: devDependencies: + memoize-one: + specifier: ^6.0.0 + version: 6.0.0 zod: specifier: ^3.23.8 version: 3.23.8 @@ -429,6 +432,9 @@ importers: immer: specifier: ^10.1.1 version: 10.1.1 + memoize-one: + specifier: ^6.0.0 + version: 6.0.0 proxy-memoize: specifier: ^3.0.1 version: 3.0.1 @@ -5272,7 +5278,6 @@ packages: libsql@0.3.19: resolution: {integrity: sha512-Aj5cQ5uk/6fHdmeW0TiXK42FqUlwx7ytmMLPSaUQPin5HKKKuUPD62MAbN4OEweGBBI7q1BekoEN4gPUEL6MZA==} - cpu: [x64, arm64, wasm32] os: [darwin, linux, win32] lilconfig@2.1.0: @@ -5428,6 +5433,9 @@ packages: mdn-data@2.0.30: resolution: {integrity: sha512-GaqWWShW4kv/G9IEucWScBx9G1/vsFZZJUO+tD26M8J8z3Kw5RDQjaoZe03YAClgeS/SWPOcb4nkFBTEi5DUEA==} + memoize-one@6.0.0: + resolution: {integrity: sha512-rkpe71W0N0c0Xz6QD0eJETuWAJGnJ9afsl1srmwPrI+yBCkge5EycXXbYRyvL29zZVUWQCY7InPRCv3GDXuZNw==} + merge-stream@2.0.0: resolution: {integrity: sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==} @@ -12622,6 +12630,8 @@ snapshots: mdn-data@2.0.30: {} + memoize-one@6.0.0: {} + merge-stream@2.0.0: {} merge2@1.4.1: {} diff --git a/studio/package.json b/studio/package.json index fc3206351..85fd67f0d 100644 --- a/studio/package.json +++ b/studio/package.json @@ -54,6 +54,7 @@ "date-fns": "^3.6.0", "hono": "^4.4.8", "immer": "^10.1.1", + "memoize-one": "^6.0.0", "proxy-memoize": "^3.0.1", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/studio/src/components/RuntimeProvider/TauriRuntime/TauriRuntime.tsx b/studio/src/components/RuntimeProvider/TauriRuntime/TauriRuntime.tsx index 460e2e493..199941b4e 100644 --- a/studio/src/components/RuntimeProvider/TauriRuntime/TauriRuntime.tsx +++ b/studio/src/components/RuntimeProvider/TauriRuntime/TauriRuntime.tsx @@ -15,9 +15,10 @@ import { openWorkspace, showOpenWorkspaceDialog, } from "./utils"; +import { z } from "zod"; export function TauriRuntime({ children }: RuntimeProviderProps) { - const [workspace, setWorkspace] = useState(); + const [workspace, setWorkspace] = useState(null); const [error, setError] = useState(); const handleOpenWorkspaceByPath = useHandler(async (path: string) => { @@ -32,7 +33,7 @@ export function TauriRuntime({ children }: RuntimeProviderProps) { setWorkspace(workspace); } } catch (error) { - const parsed = OpenWorkspaceErrorSchema.safeParse(error); + const parsed = z.lazy(OpenWorkspaceErrorSchema).safeParse(error); if (parsed.success) { return setError(parsed.data); } @@ -43,7 +44,7 @@ export function TauriRuntime({ children }: RuntimeProviderProps) { const handleCloseWorkspaceRequested = useHandler(() => { closeWorkspace(); - setWorkspace(undefined); + setWorkspace(null); }); const handleGetApiBaseUrl = useHandler(() => { @@ -55,7 +56,7 @@ export function TauriRuntime({ children }: RuntimeProviderProps) { }); useEffect(() => { - if (workspace === undefined) { + if (workspace === null) { getCurrentWorkspace().then(setWorkspace); } }, [workspace]); diff --git a/studio/src/components/RuntimeProvider/TauriRuntime/utils.ts b/studio/src/components/RuntimeProvider/TauriRuntime/utils.ts index 71d9459fd..d3311a7dd 100644 --- a/studio/src/components/RuntimeProvider/TauriRuntime/utils.ts +++ b/studio/src/components/RuntimeProvider/TauriRuntime/utils.ts @@ -1,20 +1,25 @@ -import type { Workspace } from "@fiberplane/fpx-types"; +import { WorkspaceSchema } from "@fiberplane/fpx-types"; import { invoke } from "@tauri-apps/api/core"; import { appDataDir } from "@tauri-apps/api/path"; import { open } from "@tauri-apps/plugin-dialog"; +import { z } from "zod"; export async function listRecentWorkspaces() { return await invoke>("list_recent_workspaces"); } export async function openWorkspace(path: string) { - return await invoke("open_workspace_by_path", { + const response = await invoke("open_workspace_by_path", { path, }); + + return z.lazy(WorkspaceSchema).parse(response); } export async function getCurrentWorkspace() { - return await invoke("get_current_workspace"); + const response = await invoke("get_current_workspace"); + + return z.lazy(WorkspaceSchema).parse(response); } export async function closeWorkspace() { diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 441db2ad6..1b629af7f 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -13,7 +13,7 @@ fpx = { version = "0.1.0", path = "../fpx" } fpx-app = { version = "0.1.0", path = "../fpx-app" } regex = "1.11.0" schemars = { workspace = true } -schemars-zod = "0.1.5" +schemars-zod = { version = "0.1.5" } serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal"] } diff --git a/xtask/src/commands/schemas.rs b/xtask/src/commands/schemas.rs index 0398277db..c33dcfbf8 100644 --- a/xtask/src/commands/schemas.rs +++ b/xtask/src/commands/schemas.rs @@ -80,13 +80,7 @@ fn generate_zod_schemas(schemas: Vec) -> Result> { // =========================================== // import { z } from "zod"; - -// TODO: Polyfill memoizeOne for now, seems like this breaks our TypeScript setup -// See: https://github.com/alexreardon/memoize-one/issues/267 -// The suggested `.default` works in the types package but breaks studio -function memoizeOne(callback: () => T) { - return callback; -} +import memoizeOne from "memoize-one"; "#, );