Skip to content

Commit

Permalink
Fix memoizeOne types
Browse files Browse the repository at this point in the history
  • Loading branch information
stephlow committed Oct 14, 2024
1 parent deada04 commit b101f82
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 25 deletions.
7 changes: 7 additions & 0 deletions packages/types/memoize-one.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module "memoize-one" {
// biome-ignore lint/suspicious/noExplicitAny: allow override
export default function memoizeOne<T extends (...args: any[]) => any>(
resultFn: T,
isEqual?: (newArgs: Parameters<T>, lastArgs: Parameters<T>) => boolean,
): T;
}
3 changes: 2 additions & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"build": "tsc"
},
"devDependencies": {
"zod": "^3.23.8"
"zod": "^3.23.8",
"memoize-one": "^6.0.0"
}
}
8 changes: 1 addition & 7 deletions packages/types/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(callback: () => T) {
return callback;
}
import memoizeOne from "memoize-one";

export const AppStateSchema = memoizeOne(() =>
z.object({ workspace: z.union([z.lazy(WorkspaceSchema), z.null()]) }),
Expand Down
7 changes: 6 additions & 1 deletion packages/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": "./src",
"declaration": true
"declaration": true,
"paths": {
"memoize-one": [
"./src/memoize-one.d.ts"
]
}
},
"include": [
"src"
Expand Down
12 changes: 11 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import {
openWorkspace,
showOpenWorkspaceDialog,
} from "./utils";
import { z } from "zod";

export function TauriRuntime({ children }: RuntimeProviderProps) {
const [workspace, setWorkspace] = useState<Workspace | undefined>();
const [workspace, setWorkspace] = useState<Workspace | null>(null);
const [error, setError] = useState<OpenWorkspaceError | undefined>();

const handleOpenWorkspaceByPath = useHandler(async (path: string) => {
Expand All @@ -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);
}
Expand All @@ -43,7 +44,7 @@ export function TauriRuntime({ children }: RuntimeProviderProps) {

const handleCloseWorkspaceRequested = useHandler(() => {
closeWorkspace();
setWorkspace(undefined);
setWorkspace(null);
});

const handleGetApiBaseUrl = useHandler(() => {
Expand All @@ -55,7 +56,7 @@ export function TauriRuntime({ children }: RuntimeProviderProps) {
});

useEffect(() => {
if (workspace === undefined) {
if (workspace === null) {
getCurrentWorkspace().then(setWorkspace);
}
}, [workspace]);
Expand Down
11 changes: 8 additions & 3 deletions studio/src/components/RuntimeProvider/TauriRuntime/utils.ts
Original file line number Diff line number Diff line change
@@ -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<Array<string>>("list_recent_workspaces");
}

export async function openWorkspace(path: string) {
return await invoke<Workspace | undefined>("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<Workspace | undefined>("get_current_workspace");
const response = await invoke("get_current_workspace");

return z.lazy(WorkspaceSchema).parse(response);
}

export async function closeWorkspace() {
Expand Down
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
8 changes: 1 addition & 7 deletions xtask/src/commands/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,7 @@ fn generate_zod_schemas(schemas: Vec<RootSchema>) -> Result<Vec<u8>> {
// =========================================== //
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<T>(callback: () => T) {
return callback;
}
import memoizeOne from "memoize-one";
"#,
);

Expand Down

0 comments on commit b101f82

Please sign in to comment.