Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance with useMemo #872

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useCallback, useEffect, useState } from "react";
import React, { createContext, useCallback, useEffect, useMemo, useState } from "react";
import Alert from "../components/shared/Alert";

interface AlertType {
Expand Down Expand Up @@ -46,8 +46,12 @@ export default function AlertProvider({ children }: { children: React.ReactNode

const topAlert = getTopAlert(alerts);

const contextValue = useMemo(() => {
return { openAlert, isOpen, closeAlert };
}, [openAlert, isOpen, closeAlert]);

return (
<AlertContext.Provider value={{ openAlert, isOpen, closeAlert }}>
<AlertContext.Provider value={contextValue}>
{children}
<Alert
open={Boolean(topAlert?.id)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import { makeProxy } from "../utilities/rpc";
Expand Down Expand Up @@ -74,15 +75,16 @@ export default function DependenciesProvider({ children }: PropsWithChildren) {
};
}, []);

const contextValue = useMemo(() => {
return {
dependencies: depsState,
runDiagnostics,
errors: getErrors(depsState),
};
}, [depsState, runDiagnostics, getErrors]);

return (
<DependenciesContext.Provider
value={{
dependencies: depsState,
runDiagnostics,
errors: getErrors(depsState),
}}>
{children}
</DependenciesContext.Provider>
<DependenciesContext.Provider value={contextValue}>{children}</DependenciesContext.Provider>
);
}

Expand Down
26 changes: 13 additions & 13 deletions packages/vscode-extension/src/webview/providers/DevicesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useState,
useEffect,
useCallback,
useMemo,
} from "react";
import { makeProxy } from "../utilities/rpc";
import {
Expand Down Expand Up @@ -64,19 +65,18 @@ export default function DevicesProvider({ children }: PropsWithChildren) {
};
}, []);

return (
<DevicesContext.Provider
value={{
devices,
finishedInitialLoad,
androidImages,
iOSRuntimes,
reload,
deviceManager: DeviceManager,
}}>
{children}
</DevicesContext.Provider>
);
const contextValue = useMemo(() => {
return {
devices,
finishedInitialLoad,
androidImages,
iOSRuntimes,
reload,
deviceManager: DeviceManager,
};
}, [devices, finishedInitialLoad, androidImages, iOSRuntimes, reload, DeviceManager]);

return <DevicesContext.Provider value={contextValue}>{children}</DevicesContext.Provider>;
}

export function useDevices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useState,
useEffect,
useCallback,
useMemo,
} from "react";
import { makeProxy } from "../utilities/rpc";
import {
Expand Down Expand Up @@ -57,10 +58,12 @@ export default function LaunchConfigProvider({ children }: PropsWithChildren) {
[config, setConfig]
);

const contextValue = useMemo(() => {
return { ...config, update, xcodeSchemes };
}, [config, update, xcodeSchemes]);

return (
<LaunchConfigContext.Provider value={{ ...config, update, xcodeSchemes }}>
{children}
</LaunchConfigContext.Provider>
<LaunchConfigContext.Provider value={contextValue}>{children}</LaunchConfigContext.Provider>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PropsWithChildren, useContext, createContext, useState, useEffect } from "react";
import { PropsWithChildren, useContext, createContext, useState, useEffect, useMemo } from "react";
import { makeProxy } from "../utilities/rpc";
import { DeviceSettings, ProjectInterface, ProjectState } from "../../common/Project";

Expand Down Expand Up @@ -61,11 +61,11 @@ export default function ProjectProvider({ children }: PropsWithChildren) {
};
}, []);

return (
<ProjectContext.Provider value={{ projectState, deviceSettings, project, hasActiveLicense }}>
{children}
</ProjectContext.Provider>
);
const contextValue = useMemo(() => {
return { projectState, deviceSettings, project, hasActiveLicense };
}, [projectState, deviceSettings, project, hasActiveLicense]);

return <ProjectContext.Provider value={contextValue}>{children}</ProjectContext.Provider>;
}

export function useProject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useState,
useEffect,
useCallback,
useMemo,
} from "react";
import { makeProxy } from "../utilities/rpc";
import { WorkspaceConfig, WorkspaceConfigProps } from "../../common/WorkspaceConfig";
Expand Down Expand Up @@ -49,8 +50,12 @@ export default function WorkspaceConfigProvider({ children }: PropsWithChildren)
[config, setConfig]
);

const contextValue = useMemo(() => {
return { ...config, update };
}, [config, update]);

return (
<WorkspaceConfigContext.Provider value={{ ...config, update }}>
<WorkspaceConfigContext.Provider value={contextValue}>
{children}
</WorkspaceConfigContext.Provider>
);
Expand Down
Loading