Skip to content

Commit

Permalink
allow loading the app even when plugins fails to load
Browse files Browse the repository at this point in the history
  • Loading branch information
imanjra committed Sep 4, 2024
1 parent 3ab7f5b commit 842c0c4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 26 deletions.
4 changes: 1 addition & 3 deletions app/packages/app/src/Sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import {
type OperationType,
} from "relay-runtime";
import Setup from "./components/Setup";
import type { IndexPageQuery } from "./pages/__generated__/IndexPageQuery.graphql";
import type {
DatasetPageQuery,
DatasetPageQuery$data,
} from "./pages/datasets/__generated__/DatasetPageQuery.graphql";
import type { IndexPageQuery } from "./pages/__generated__/IndexPageQuery.graphql";
import { useRouterContext, type Entry } from "./routing";
import { AppReadyState } from "./useEvents/registerEvent";
import useEventSource from "./useEventSource";
Expand All @@ -45,8 +45,6 @@ export const SessionContext = React.createContext<Session>(SESSION_DEFAULT);
const Plugins = ({ children }: { children: React.ReactNode }) => {
const plugins = usePlugins();
if (plugins.isLoading) return <Loading>Pixelating...</Loading>;
if (plugins.hasError) return <Loading>Plugin error...</Loading>;

return <>{children}</>;
};

Expand Down
8 changes: 4 additions & 4 deletions app/packages/core/src/components/Starter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useOperatorExecutor,
usePromptOperatorInput,
} from "@fiftyone/operators/src/state";
import { datasetName as datasetNameAtom } from "@fiftyone/state";
import {
Button,
ButtonProps,
Expand All @@ -15,9 +16,8 @@ import {
Typography,
} from "@mui/material";
import React, { useCallback, useMemo } from "react";
import { CONTENT_BY_MODE } from "./content";
import { useRecoilValue } from "recoil";
import { datasetName as datasetNameAtom } from "@fiftyone/state";
import { CONTENT_BY_MODE } from "./content";

const CREATE_DATASET_OPERATOR = "@voxel51/utils/create_dataset";
const IMPORT_SAMPLES_OPERATOR = "@voxel51/io/import_samples";
Expand All @@ -30,12 +30,12 @@ const INSTALL_IO_PLUGIN_LABEL = "@voxel51/io";

export function Starter(props: StarterPropsType) {
const { mode } = props;
const ready = useOperators(true);
const { isLoading } = useOperators(true);
const datasetName = useRecoilValue(datasetNameAtom);

if (!mode) return null;

if (!ready) return <Loading>Pixelating...</Loading>;
if (isLoading) return <Loading>Pixelating...</Loading>;

const { code, codeTitle, learnMoreLabel, learnMoreLink, title } =
CONTENT_BY_MODE[mode];
Expand Down
28 changes: 20 additions & 8 deletions app/packages/operators/src/loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ async function loadOperators(datasetName: string) {
* start-up operators for execution.
*/
export function useOperators(datasetLess?: boolean) {
const [ready, setReady] = useState(false);
const [state, setState] = useState<"loading" | "error" | "ready">("loading");
const [error, setError] = useState<Error | null>(null);
const datasetName = useRecoilValue(datasetNameAtom);
const setAvailableOperatorsRefreshCount = useSetRecoilState(
availableOperatorsRefreshCount
Expand All @@ -37,12 +38,17 @@ export function useOperators(datasetLess?: boolean) {

useEffect(() => {
if (isPrimitiveString(datasetName) || datasetLess) {
loadOperators(datasetName).then(() => {
// trigger force refresh
setAvailableOperatorsRefreshCount((count) => count + 1);
setReady(true);
setOperatorsInitialized(true);
});
loadOperators(datasetName)
.then(() => {
// trigger force refresh
setAvailableOperatorsRefreshCount((count) => count + 1);
setState("ready");
setOperatorsInitialized(true);
})
.catch((error) => {
setState("error");
setError(error);
});
}
}, [
datasetLess,
Expand All @@ -51,5 +57,11 @@ export function useOperators(datasetLess?: boolean) {
setOperatorsInitialized,
]);

return ready && (initialized || datasetLess);
return {
ready: state === "ready" && (initialized || datasetLess),
hasError: state === "error",
isLoading: state === "loading",
error,
state,
};
}
19 changes: 15 additions & 4 deletions app/packages/plugins/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function loadPlugins() {
if (plugin.hasJS) {
const name = plugin.name;
const scriptPath = plugin.jsBundleServerPath;
const cacheKey = plugin.jsBundleHash ? `?h=${plugin.jsBundleHash}` : '';
const cacheKey = plugin.jsBundleHash ? `?h=${plugin.jsBundleHash}` : "";
if (usingRegistry().hasScript(name)) {
console.debug(`Plugin "${name}": already loaded`);
continue;
Expand Down Expand Up @@ -157,11 +157,22 @@ async function loadScript(name, url) {
export function usePlugins() {
const datasetName = recoil.useRecoilValue(fos.datasetName);
const [state, setState] = recoil.useRecoilState(pluginsLoaderAtom);
const operatorsReady = useOperators(datasetName === null);
const notify = fos.useNotification();
const {
ready: operatorsReady,
hasError: operatorHasError,
isLoading: operatorIsLoading,
} = useOperators(datasetName === null);

useEffect(() => {
loadPlugins()
.catch(() => {
notify({
msg:
"Failed to initialize Python plugins. You may not be able to use" +
" panels, operators, and other artifacts of plugins installed.",
variant: "error",
});
setState("error");
})
.then(() => {
Expand All @@ -170,8 +181,8 @@ export function usePlugins() {
}, [setState]);

return {
isLoading: state === "loading" || !operatorsReady,
hasError: state === "error",
isLoading: state === "loading" || operatorIsLoading,
hasError: state === "error" || operatorHasError,
ready: state === "ready" && operatorsReady,
};
}
Expand Down
9 changes: 6 additions & 3 deletions app/packages/spaces/src/components/Workspaces/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import { useRecoilState, useRecoilValue, useResetRecoilState } from "recoil";
import { savedWorkspacesAtom } from "../../state";
import { LIST_WORKSPACES_OPERATOR, LOAD_WORKSPACE_OPERATOR } from "./constants";
import { operatorsInitializedAtom } from "@fiftyone/operators/src/state";

export function useWorkspaces() {
const [state, setState] = useRecoilState(savedWorkspacesAtom);
const resetState = useResetRecoilState(savedWorkspacesAtom);
const [listWorkspaceExecuting, setListWorkspaceExecuting] = useState(false);
const currentDataset = useRecoilValue(datasetName);
const operatorsInitialized = useRecoilValue(operatorsInitializedAtom);

const listWorkspace = useCallback(() => {
if (listWorkspaceExecuting) return;
if (listWorkspaceExecuting || !operatorsInitialized) return;
setListWorkspaceExecuting(true);
executeOperator(
LIST_WORKSPACES_OPERATOR,
Expand All @@ -24,7 +26,7 @@ export function useWorkspaces() {
return {
...state,
initialized: true,
workspaces: result?.result?.workspaces,
workspaces: result?.result?.workspaces || [],
dataset: currentDataset,
};
});
Expand All @@ -36,7 +38,7 @@ export function useWorkspaces() {
skipOutput: true,
}
);
}, [listWorkspaceExecuting, setState, currentDataset]);
}, [listWorkspaceExecuting, setState, currentDataset, operatorsInitialized]);

const loadWorkspace = useCallback((name: string) => {
executeOperator(LOAD_WORKSPACE_OPERATOR, { name }, { skipOutput: true });
Expand All @@ -59,5 +61,6 @@ export function useWorkspaces() {
listWorkspace,
reset: resetState,
existingSlugs,
canInitialize: operatorsInitialized,
};
}
15 changes: 11 additions & 4 deletions app/packages/spaces/src/components/Workspaces/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ import { useWorkspaces } from "./hooks";
export default function Workspaces() {
const [open, setOpen] = useState(false);
const [searchTerm, setSearchTerm] = useState("");
const { workspaces, loadWorkspace, initialized, listWorkspace } =
useWorkspaces();
const {
workspaces,
loadWorkspace,
initialized,
listWorkspace,
canInitialize,
} = useWorkspaces();
const setWorkspaceEditorState = useSetRecoilState(workspaceEditorStateAtom);
const canEditWorkSpace = useRecoilValue(canEditWorkspaces);
const disabled = canEditWorkSpace.enabled !== true;
Expand All @@ -45,10 +50,12 @@ export default function Workspaces() {
}, [workspaces, searchTerm]);

useEffect(() => {
if (!initialized) {
if (!initialized && canInitialize) {
listWorkspace();
}
}, [open, initialized, listWorkspace]);
}, [open, initialized, listWorkspace, canInitialize]);

if (!canInitialize) return null;

return (
<Box>
Expand Down

0 comments on commit 842c0c4

Please sign in to comment.